This repository has been archived on 2025-09-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
XericLibrary-OLD/Runtime/Logger/MergeDebug.cs

149 lines
2.8 KiB
C#

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
{
/// <summary>
/// Merged debug messages
/// </summary>
/// <remarks>
/// 合并调试消息,
/// 如果需要输出连续的一串消息,但又因为太长或太细碎导致调试信息破碎,
/// 就适合使用合并消息。
/// </remarks>
public class MergeDebug
{
#region
/// <summary>
/// 单次发送消息的限制长度
/// </summary>
private const int debugInfomationLengthLimit = 4000;
/// <summary>
/// 消息内容
/// </summary>
private StringBuilder messageContext;
#endregion
#region
public MergeDebug()
{
messageContext = new StringBuilder();
}
#endregion
#region
/// <summary>
/// Push the message content
/// </summary>
/// <remarks>
/// 压入消息内容
/// </remarks>
public void PushLog(string message)
{
messageContext.Append(message);
}
/// <summary>
/// Push the message content, with a line feed
/// </summary>
/// <remarks>
/// 压入消息内容,并在末尾追加一个换行符
/// </remarks>
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<string> 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++;
}
}
}
/// <summary>
/// Send information in a message type
/// </summary>
/// <remarks>
/// 以消息类型发送信息
/// </remarks>
public void Log()
{
PopLog(messageContext, a => Debug.Log(a));
messageContext.Clear();
}
/// <summary>
/// Send information in a Warning type
/// </summary>
/// <remarks>
/// 以警告类型发送信息
/// </remarks>
public void LogWarning()
{
PopLog(messageContext, a => Debug.LogWarning(a));
messageContext.Clear();
}
/// <summary>
/// Send information in a Error type
/// </summary>
/// <remarks>
/// 以错误消息类型发送信息
/// </remarks>
public void LogError()
{
PopLog(messageContext, a => Debug.LogError(a));
messageContext.Clear();
}
#endregion
}
}