2024-04-28 13:55:19 +00:00
|
|
|
|
using System;
|
2024-07-02 15:16:04 +00:00
|
|
|
|
using System.Collections.Generic;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
using System.Text;
|
2024-07-15 14:36:48 +00:00
|
|
|
|
using ColdMint.scripts.openObserve;
|
2024-05-24 14:58:52 +00:00
|
|
|
|
using ColdMint.scripts.utils;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
using Godot;
|
2024-07-15 14:36:48 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using OpenTelemetry.Exporter;
|
|
|
|
|
using OpenTelemetry.Logs;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.debug;
|
|
|
|
|
|
2024-05-08 10:22:04 +00:00
|
|
|
|
public static class LogCat
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-07-02 15:16:04 +00:00
|
|
|
|
public static class LogLabel
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Default log label</para>
|
|
|
|
|
/// <para>默认的日志标签</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string Default = "Default";
|
|
|
|
|
|
2024-07-17 12:04:13 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>LookForWeaponProcessor</para>
|
|
|
|
|
/// <para>查找武器处理器</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string LookForWeaponProcessor = "LookForWeaponProcessor";
|
|
|
|
|
|
2024-07-02 15:16:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>PatrolStateProcessor</para>
|
|
|
|
|
/// <para>巡逻状态处理器</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string PatrolStateProcessor = "PatrolStateProcessor";
|
2024-07-06 14:55:07 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>CampManager</para>
|
|
|
|
|
/// <para>阵营管理器</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string CampManager = "CampManager";
|
2024-07-15 14:36:48 +00:00
|
|
|
|
|
2024-07-06 14:55:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>State context</para>
|
|
|
|
|
/// <para>状态上下文</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string StateContext = "StateContext";
|
2024-07-15 14:36:48 +00:00
|
|
|
|
|
2024-07-06 14:55:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>StateMachineTemplate</para>
|
|
|
|
|
/// <para>状态机模板</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string StateMachineTemplate = "StateMachineTemplate";
|
2024-07-15 14:36:48 +00:00
|
|
|
|
|
2024-07-06 14:55:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Pursuit enemy processor</para>
|
|
|
|
|
/// <para>追击敌人处理器</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string ChaseStateProcessor = "ChaseStateProcessor";
|
2024-07-10 15:23:04 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>BubbleMarker</para>
|
|
|
|
|
/// <para>气泡标记</para>
|
|
|
|
|
/// </summary>
|
2024-07-15 14:36:48 +00:00
|
|
|
|
public const string BubbleMarker = "BubbleMarker";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>LogCollector</para>
|
|
|
|
|
/// <para>日志收集器</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string LogCollector = "LogCollector";
|
2024-07-02 15:16:04 +00:00
|
|
|
|
}
|
2024-07-06 14:55:07 +00:00
|
|
|
|
|
2024-07-02 15:16:04 +00:00
|
|
|
|
|
2024-05-24 14:58:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Information log level</para>
|
|
|
|
|
/// <para>信息日志等级</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const int InfoLogLevel = 0;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Warning level</para>
|
|
|
|
|
/// <para>警告等级</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const int WarningLogLevel = 1;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Error log level</para>
|
|
|
|
|
/// <para>错误日志等级</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const int ErrorLogLevel = 2;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///<para>Disable all logs</para>
|
|
|
|
|
///<para>禁用所有日志</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const int DisableAllLogLevel = int.MaxValue;
|
|
|
|
|
|
|
|
|
|
private static int _minLogLevel = InfoLogLevel;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Set the minimum log level</para>
|
|
|
|
|
/// <para>设置最小日志等级</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
///<para>Set a value for it so that LogCat only prints logs with a level higher than the set value.</para>
|
|
|
|
|
///<para>为其设置一个值,使LogCat仅打印等级高于设定值的日志。</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static int MinLogLevel
|
|
|
|
|
{
|
|
|
|
|
get => _minLogLevel;
|
|
|
|
|
set => _minLogLevel = value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-15 14:36:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Whether to upload logs that need to be formatted by default</para>
|
|
|
|
|
/// <para>是否默认上传需要格式化的日志</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool UploadFormat { get; set; } = true;
|
|
|
|
|
|
2024-05-08 10:22:04 +00:00
|
|
|
|
private static readonly StringBuilder StringBuilder = new StringBuilder();
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
2024-07-02 15:16:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Disabled log label</para>
|
|
|
|
|
/// <para>禁用的日志标签</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static HashSet<string> DisabledLogLabels { get; } = [];
|
2024-07-06 14:55:07 +00:00
|
|
|
|
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
2024-07-15 14:36:48 +00:00
|
|
|
|
public static void Init()
|
|
|
|
|
{
|
|
|
|
|
var loggerFactory = LoggerFactory.Create(builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.AddOpenTelemetry(options => { options.AddOtlpExporter(OtlpExporterOptions); });
|
|
|
|
|
});
|
|
|
|
|
var logger = loggerFactory.CreateLogger("323");
|
|
|
|
|
logger.Log(LogLevel.Debug, "你好");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void OtlpExporterOptions(OtlpExporterOptions options)
|
|
|
|
|
{
|
|
|
|
|
options.Protocol = OtlpExportProtocol.HttpProtobuf;
|
|
|
|
|
options.Endpoint = new Uri("http://test.coldmint.top/api/default/traces");
|
|
|
|
|
options.Headers =
|
|
|
|
|
"Authorization=Basic cm9vdEBleGFtcGxlLmNvbTp5V0kwVzZYcWhteTBzQml3,organization=default,stream-name=default";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-07-02 15:16:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Disable log Label</para>
|
|
|
|
|
/// <para>禁用某个日志标签</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="label">
|
|
|
|
|
///<para>label</para>
|
|
|
|
|
///<para>标签名称</para>
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
///<para>Returns whether the function is disabled successfully</para>
|
|
|
|
|
///<para>返回是否禁用成功</para>
|
|
|
|
|
/// </returns>
|
|
|
|
|
public static bool DisableLogLabel(string label)
|
|
|
|
|
{
|
|
|
|
|
return DisabledLogLabels.Add(label);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Whether a label is enabled</para>
|
|
|
|
|
/// <para>某个标签是否处于启用状态</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="label">
|
|
|
|
|
///<para>label</para>
|
|
|
|
|
///<para>标签名称</para>
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
///<para>Whether enabled</para>
|
|
|
|
|
///<para>是否处于启用</para>
|
|
|
|
|
/// </returns>
|
|
|
|
|
public static bool IsEnabledLogLabel(string label)
|
|
|
|
|
{
|
|
|
|
|
return !DisabledLogLabels.Contains(label);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>EnableLogLabel</para>
|
|
|
|
|
/// <para>启用某个日志标签</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="label">
|
|
|
|
|
///<para>label</para>
|
|
|
|
|
/// <para>标签名称</para>
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
///<para>Returns whether the function is enabled successfully</para>
|
|
|
|
|
///<para>返回是否启用成功</para>
|
|
|
|
|
/// </returns>
|
|
|
|
|
public static bool EnableLogLabel(string label)
|
|
|
|
|
{
|
|
|
|
|
return DisabledLogLabels.Remove(label);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static StringBuilder HandleMessage(int level, string message, string label)
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-05-08 10:22:04 +00:00
|
|
|
|
StringBuilder.Clear();
|
2024-05-24 14:58:52 +00:00
|
|
|
|
switch (level)
|
|
|
|
|
{
|
|
|
|
|
case InfoLogLevel:
|
|
|
|
|
StringBuilder.Append("INFO");
|
|
|
|
|
break;
|
|
|
|
|
case WarningLogLevel:
|
|
|
|
|
StringBuilder.Append("WARNING");
|
|
|
|
|
break;
|
|
|
|
|
case ErrorLogLevel:
|
|
|
|
|
StringBuilder.Append("ERROR");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
StringBuilder.Append("INFO");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-05-26 03:10:21 +00:00
|
|
|
|
|
2024-07-02 15:16:04 +00:00
|
|
|
|
StringBuilder.Append(DateTime.Now.ToString(" yyyy-M-d HH:mm:ss "));
|
|
|
|
|
StringBuilder.Append(label);
|
|
|
|
|
StringBuilder.Append(" :");
|
2024-06-16 07:28:16 +00:00
|
|
|
|
var key = $"log_{message}";
|
|
|
|
|
var translationResult = TranslationServerUtils.Translate(key);
|
|
|
|
|
StringBuilder.Append(translationResult == key ? message : translationResult);
|
2024-05-08 10:22:04 +00:00
|
|
|
|
return StringBuilder;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Print log</para>
|
|
|
|
|
/// <para>打印日志</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">
|
|
|
|
|
///<para>message</para>
|
|
|
|
|
///<para>消息</para>
|
|
|
|
|
/// <para>This message supports localized output, assuming there is already a translation key, Hello = 你好, passing hello will output 你好.</para>
|
|
|
|
|
/// <para>这个消息支持本地化输出,假设已存在翻译key,Hello = 你好,传入Hello则会输出你好。</para>
|
|
|
|
|
/// </param>
|
2024-07-02 15:16:04 +00:00
|
|
|
|
/// <param name="label">
|
|
|
|
|
/// </param>
|
2024-07-15 14:36:48 +00:00
|
|
|
|
/// <param name="upload">
|
|
|
|
|
/// </param>
|
|
|
|
|
public static void Log(string message, string label = LogLabel.Default, bool upload = true)
|
|
|
|
|
{
|
|
|
|
|
PrintLog(InfoLogLevel, HandleMessage(InfoLogLevel, message, label).ToString(), label, upload);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void PrintLog(int level, string concreteLog, string label, bool upload)
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-07-02 15:16:04 +00:00
|
|
|
|
if (!IsEnabledLogLabel(label))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 14:58:52 +00:00
|
|
|
|
if (_minLogLevel > InfoLogLevel)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-15 14:36:48 +00:00
|
|
|
|
if (LogCollector.CanUploadLog && upload)
|
|
|
|
|
{
|
|
|
|
|
var logData = new LogData
|
|
|
|
|
{
|
|
|
|
|
Level = level,
|
|
|
|
|
Message = concreteLog,
|
|
|
|
|
AppId = "none"
|
|
|
|
|
};
|
|
|
|
|
LogCollector.Push(logData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GD.Print(concreteLog);
|
2024-04-28 13:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Print error log</para>
|
|
|
|
|
/// <para>打印错误日志</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">
|
|
|
|
|
///<para>message</para>
|
|
|
|
|
///<para>消息</para>
|
|
|
|
|
/// <para>This message supports localized output, assuming there is already a translation key, Hello = 你好, passing hello will output 你好.</para>
|
|
|
|
|
/// <para>这个消息支持本地化输出,假设已存在翻译key,Hello = 你好,传入Hello则会输出你好。</para>
|
|
|
|
|
/// </param>
|
2024-07-02 15:16:04 +00:00
|
|
|
|
/// <param name="label"></param>
|
2024-07-15 14:36:48 +00:00
|
|
|
|
/// <param name="upload"></param>
|
|
|
|
|
public static void LogError(string message, string label = LogLabel.Default, bool upload = true)
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-07-15 14:36:48 +00:00
|
|
|
|
PrintLog(ErrorLogLevel, HandleMessage(ErrorLogLevel, message, label).ToString(), label, upload);
|
2024-05-24 14:58:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-15 14:36:48 +00:00
|
|
|
|
public static void LogWarning(string message, string label = LogLabel.Default, bool upload = true)
|
2024-05-24 14:58:52 +00:00
|
|
|
|
{
|
2024-07-15 14:36:48 +00:00
|
|
|
|
PrintLog(WarningLogLevel, HandleMessage(WarningLogLevel, message, label).ToString(), label, upload);
|
2024-04-28 13:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-15 14:36:48 +00:00
|
|
|
|
public static void LogErrorWithFormat(string message, string label, bool upload, params object?[] args)
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-07-15 14:36:48 +00:00
|
|
|
|
PrintLog(WarningLogLevel, string.Format(HandleMessage(ErrorLogLevel, message, label).ToString(), args), label,
|
|
|
|
|
upload);
|
2024-04-28 13:55:19 +00:00
|
|
|
|
}
|
2024-05-24 14:58:52 +00:00
|
|
|
|
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
2024-07-15 14:36:48 +00:00
|
|
|
|
public static void LogWithFormat(string message, string label, bool upload, params object?[] args)
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-07-15 14:36:48 +00:00
|
|
|
|
PrintLog(InfoLogLevel, string.Format(HandleMessage(InfoLogLevel, message, label).ToString(), args), label,
|
|
|
|
|
upload);
|
2024-04-28 13:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-15 14:36:48 +00:00
|
|
|
|
public static void LogWarningWithFormat(string message, bool upload, string label, params object?[] args)
|
2024-05-26 03:10:21 +00:00
|
|
|
|
{
|
2024-07-15 14:36:48 +00:00
|
|
|
|
PrintLog(WarningLogLevel, string.Format(HandleMessage(WarningLogLevel, message, label).ToString(), args), label,
|
|
|
|
|
upload);
|
2024-05-26 03:10:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 14:56:27 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>This method is called when an exception is caught</para>
|
|
|
|
|
/// <para>当捕获异常后调用此方法</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
2024-07-02 15:16:04 +00:00
|
|
|
|
/// <param name="label"></param>
|
|
|
|
|
public static void WhenCaughtException(Exception e, string label = LogLabel.Default)
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
2024-07-02 15:16:04 +00:00
|
|
|
|
if (!IsEnabledLogLabel(label))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 14:56:27 +00:00
|
|
|
|
//Log an exception here or send it to the server.
|
|
|
|
|
//请在这里记录异常或将异常发送至服务器。
|
2024-07-15 14:36:48 +00:00
|
|
|
|
PrintLog(ErrorLogLevel,
|
|
|
|
|
HandleMessage(ErrorLogLevel, e.Message, label).Append('\n').Append(e.StackTrace).ToString(), label, true);
|
2024-04-28 13:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|