using log4net.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using static Codice.CM.WorkspaceServer.WorkspaceTreeDataStore;
namespace XericLibrary.Runtime.MacroLibrary
{
///
/// Merged debug messages
///
///
/// 合并调试消息,
/// 如果需要输出连续的一串消息,但又因为太长或太细碎导致调试信息破碎,
/// 就适合使用合并消息。
///
public class MergeDebug
{
#region 字段属性
///
/// 单次发送消息的限制长度
///
private const int debugInfomationLengthLimit = 4000;
///
/// 消息内容
///
private StringBuilder messageContext;
#endregion
#region 构造函数
public MergeDebug()
{
messageContext = new StringBuilder();
}
#endregion
#region 输入消息
///
/// Push the message content
///
///
/// 压入消息内容
///
public void PushLog(string message)
{
messageContext.Append(message);
}
///
/// Push the message content, with a line feed
///
///
/// 压入消息内容,并在末尾追加一个换行符
///
public void PushLogLine(string message)
{
messageContext.AppendLine(message);
}
#endregion
#region 输出消息
private static bool IsNewLine(char c)
{
return c == '\n' || c == '\r';
}
private static void PopLog(StringBuilder sb, Action debugFunc)
{
//debugInfomationLengthLimit
var context = sb.ToString();
int startIndex = 0;
int endIndex = 0;
while(startIndex < context.Length)
{
endIndex = Math.Min(startIndex + debugInfomationLengthLimit, context.Length);
while(endIndex > startIndex && !IsNewLine(context[endIndex - 1]))
{
endIndex--;
}
string line = context.Substring(startIndex, endIndex - startIndex);
debugFunc(line);
startIndex = endIndex;
while(startIndex < context.Length && IsNewLine(context[startIndex]))
{
startIndex++;
}
}
}
///
/// Send information in a message type
///
///
/// 以消息类型发送信息
///
public void Log()
{
PopLog(messageContext, a => Debug.Log(a));
messageContext.Clear();
}
///
/// Send information in a Warning type
///
///
/// 以警告类型发送信息
///
public void LogWarning()
{
PopLog(messageContext, a => Debug.LogWarning(a));
messageContext.Clear();
}
///
/// Send information in a Error type
///
///
/// 以错误消息类型发送信息
///
public void LogError()
{
PopLog(messageContext, a => Debug.LogError(a));
messageContext.Clear();
}
#endregion
}
}