zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Model/Core/Log/Log.cs

120 lines
2.8 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
#if NOT_CLIENT
using NLog;
#endif
namespace ET
{
public static class Log
{
public const int TraceLevel = 1;
public const int DebugLevel = 2;
public const int InfoLevel = 3;
public const int WarningLevel = 4;
public static ILog ILog { get; }
static Log()
{
#if SERVER
ILog = new NLogger("Server");
2021-04-10 19:49:32 +08:00
#elif UNITY_EDITOR
2021-04-08 20:09:59 +08:00
ILog = new UnityLogger();
#elif ROBOT
2021-04-10 19:49:32 +08:00
ILog = new CommonLogger();
2021-04-08 20:09:59 +08:00
#endif
}
public static bool CheckLogLevel(int level)
{
if (Game.Options == null)
{
return true;
}
return Game.Options.LogLevel <= level;
}
public static Action<string> DebugCallback;
2021-04-08 20:09:59 +08:00
public static Action<string> ErrorCallback;
public static Action<string> InfoCallBack;
2021-04-08 20:09:59 +08:00
public static void Trace(string msg)
{
if (!CheckLogLevel(DebugLevel))
{
return;
}
DebugCallback?.Invoke(msg);
2021-04-08 20:09:59 +08:00
StackTrace st = new StackTrace(1, true);
ILog.Trace($"{msg}\n{st}");
}
public static void Debug(string msg)
{
if (!CheckLogLevel(DebugLevel))
{
return;
}
DebugCallback?.Invoke(msg);
2021-04-08 20:09:59 +08:00
ILog.Debug(msg);
}
public static void Info(string msg)
{
if (!CheckLogLevel(InfoLevel))
{
return;
}
InfoCallBack?.Invoke(msg);
2021-04-08 20:09:59 +08:00
ILog.Info(msg);
}
public static void TraceInfo(string msg)
{
if (!CheckLogLevel(InfoLevel))
{
return;
}
StackTrace st = new StackTrace(1, true);
InfoCallBack?.Invoke($"{msg}\n{st}");
2021-04-08 20:09:59 +08:00
ILog.Trace($"{msg}\n{st}");
}
public static void Warning(string msg)
{
if (!CheckLogLevel(WarningLevel))
{
return;
}
InfoCallBack?.Invoke($"{msg}");
2021-04-08 20:09:59 +08:00
ILog.Warning(msg);
}
public static void Error(string msg)
{
ErrorCallback?.Invoke(msg);
ILog.Error($"{msg}");
}
public static void ErrorDetail(string msg)
{
StackTrace st = new StackTrace(1, true);
ErrorCallback?.Invoke($"{msg}\n{st}");
ILog.Error($"{msg}\n{st}");
}
public static void Error(Exception e)
{
string str = e.ToString();
ErrorCallback?.Invoke(str);
ILog.Error(str);
}
}
}