mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] TEngineServer
[+] TEngineServer
This commit is contained in:
17
Assets/GameScripts/DotNet/Core/Log/ILog.cs
Normal file
17
Assets/GameScripts/DotNet/Core/Log/ILog.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace TEngine
|
||||
{
|
||||
public interface ILog
|
||||
{
|
||||
void Trace(string message);
|
||||
void Warning(string message);
|
||||
void Info(string message);
|
||||
void Debug(string message);
|
||||
void Error(string message);
|
||||
void Fatal(string message);
|
||||
void Trace(string message, params object[] args);
|
||||
void Warning(string message, params object[] args);
|
||||
void Info(string message, params object[] args);
|
||||
void Debug(string message, params object[] args);
|
||||
void Error(string message, params object[] args);
|
||||
}
|
||||
}
|
11
Assets/GameScripts/DotNet/Core/Log/ILog.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Log/ILog.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b90975fb800e1a745a323955fa71f3a2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
141
Assets/GameScripts/DotNet/Core/Log/Log.cs
Normal file
141
Assets/GameScripts/DotNet/Core/Log/Log.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
#if TENGINE_NET
|
||||
namespace TEngine
|
||||
{
|
||||
public static class Log
|
||||
{
|
||||
static Log()
|
||||
{
|
||||
LogCore.Instance.ILog = new NLog("Server");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打印追朔级别日志,用于记录追朔类日志信息。
|
||||
/// </summary>
|
||||
/// <param name="msg">日志内容。</param>
|
||||
public static void Trace(string msg)
|
||||
{
|
||||
LogCore.Instance.Trace(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打印调试级别日志,用于记录调试类日志信息。
|
||||
/// </summary>
|
||||
/// <param name="msg">日志内容。</param>
|
||||
public static void Debug(string msg)
|
||||
{
|
||||
LogCore.Instance.Debug(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打印信息级别日志,用于记录信息类日志信息。
|
||||
/// </summary>
|
||||
/// <param name="msg">日志内容。</param>
|
||||
public static void Info(string msg)
|
||||
{
|
||||
LogCore.Instance.Info(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打印追朔级别日志,用于记录追朔类日志信息。
|
||||
/// </summary>
|
||||
/// <param name="msg">日志内容。</param>
|
||||
public static void TraceInfo(string msg)
|
||||
{
|
||||
LogCore.Instance.Trace(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打印追警告日志,用于记录警告类日志信息。
|
||||
/// </summary>
|
||||
/// <param name="msg">日志内容。</param>
|
||||
public static void Warning(string msg)
|
||||
{
|
||||
LogCore.Instance.Warning(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打印错误级别日志,建议在发生功能逻辑错误,但尚不会导致游戏崩溃或异常时使用。
|
||||
/// </summary>
|
||||
/// <param name="msg">日志内容。</param>
|
||||
public static void Error(string msg)
|
||||
{
|
||||
LogCore.Instance.Error(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打印错误级别日志,建议在发生功能逻辑错误,但尚不会导致游戏崩溃或异常时使用。
|
||||
/// </summary>
|
||||
/// <param name="exception">异常内容。</param>
|
||||
public static void Error(Exception exception)
|
||||
{
|
||||
LogCore.Instance.Error(exception);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打印严重错误级别日志,建议在发生严重错误,可能导致游戏崩溃或异常时使用,此时应尝试重启进程或重建服务框架。
|
||||
/// </summary>
|
||||
/// <param name="msg">日志内容。</param>
|
||||
public static void Fatal(string msg)
|
||||
{
|
||||
LogCore.Instance.Fatal(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打印严重错误级别日志,建议在发生严重错误,可能导致游戏崩溃或异常时使用,此时应尝试重启进程或重建服务框架。
|
||||
/// </summary>
|
||||
/// <param name="exception">常内容。</param>
|
||||
public static void Fatal(Exception exception)
|
||||
{
|
||||
LogCore.Instance.Fatal(exception.Message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断言严重错误级别日志,建议在发生严重错误。
|
||||
/// </summary>
|
||||
/// <param name="condition">断言条件</param>
|
||||
/// <param name="msg"></param>
|
||||
public static void Assert(bool condition, string msg = "")
|
||||
{
|
||||
if (!condition)
|
||||
{
|
||||
if (string.IsNullOrEmpty(msg))
|
||||
{
|
||||
msg = AssertError;
|
||||
}
|
||||
|
||||
LogCore.Instance.Error(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断言默认日志。
|
||||
/// </summary>
|
||||
public const string AssertError = "ASSERT FAILD";
|
||||
|
||||
public static void Trace(ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler message)
|
||||
{
|
||||
LogCore.Instance.Trace(message.ToStringAndClear());
|
||||
}
|
||||
|
||||
public static void Warning(ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler message)
|
||||
{
|
||||
LogCore.Instance.Warning(message.ToStringAndClear());
|
||||
}
|
||||
|
||||
public static void Info(ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler message)
|
||||
{
|
||||
LogCore.Instance.Info(message.ToStringAndClear());
|
||||
}
|
||||
|
||||
public static void Debug(ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler message)
|
||||
{
|
||||
LogCore.Instance.Debug(message.ToStringAndClear());
|
||||
}
|
||||
|
||||
public static void Error(ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler message)
|
||||
{
|
||||
LogCore.Instance.Error(message.ToStringAndClear());
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
11
Assets/GameScripts/DotNet/Core/Log/Log.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Log/Log.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7794bf5b314b88d409d6113935a0d1f3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
267
Assets/GameScripts/DotNet/Core/Log/LogCore.cs
Normal file
267
Assets/GameScripts/DotNet/Core/Log/LogCore.cs
Normal file
@@ -0,0 +1,267 @@
|
||||
#if TENGINE_NET
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using TEngine.Core;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
public class LogCore : Singleton<LogCore>
|
||||
{
|
||||
private ILog iLog;
|
||||
|
||||
public ILog ILog
|
||||
{
|
||||
set => iLog = value;
|
||||
}
|
||||
|
||||
public enum LogLevel
|
||||
{
|
||||
INFO,
|
||||
DEBUG,
|
||||
ASSERT,
|
||||
WARNING,
|
||||
ERROR,
|
||||
EXCEPTION,
|
||||
}
|
||||
|
||||
private LogLevel _filterLevel = LogLevel.INFO;
|
||||
private StringBuilder _stringBuilder = new StringBuilder();
|
||||
|
||||
private const int TraceLevel = 1;
|
||||
private const int DebugLevel = 2;
|
||||
private const int InfoLevel = 3;
|
||||
private const int WarningLevel = 4;
|
||||
|
||||
private bool CheckLogLevel(int level)
|
||||
{
|
||||
if (Define.Options == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return Define.Options.LogLevel <= level;
|
||||
}
|
||||
|
||||
public void Trace(string msg)
|
||||
{
|
||||
if (!CheckLogLevel(DebugLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StackTrace st = new StackTrace(2, true);
|
||||
string log = $"{msg}\n{st}";
|
||||
this.Log(LogLevel.INFO, log);
|
||||
this.iLog.Trace(log);
|
||||
}
|
||||
|
||||
public void Debug(string msg)
|
||||
{
|
||||
if (!CheckLogLevel(DebugLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
this.Log(LogLevel.DEBUG, msg);
|
||||
this.iLog.Debug(msg);
|
||||
}
|
||||
|
||||
public void Info(string msg)
|
||||
{
|
||||
if (!CheckLogLevel(InfoLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
this.Log(LogLevel.INFO, msg);
|
||||
this.iLog.Info(msg);
|
||||
}
|
||||
|
||||
public void TraceInfo(string msg)
|
||||
{
|
||||
if (!CheckLogLevel(InfoLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StackTrace st = new StackTrace(2, true);
|
||||
string log = $"{msg}\n{st}";
|
||||
this.Log(LogLevel.INFO, msg);
|
||||
this.iLog.Trace(log);
|
||||
}
|
||||
|
||||
public void Warning(string msg)
|
||||
{
|
||||
if (!CheckLogLevel(WarningLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
this.Log(LogLevel.WARNING, msg);
|
||||
this.iLog.Warning(msg);
|
||||
}
|
||||
|
||||
public void Error(string msg)
|
||||
{
|
||||
StackTrace st = new StackTrace(2, true);
|
||||
string log = $"{msg}\n{st}";
|
||||
this.Log(LogLevel.ERROR, log);
|
||||
this.iLog.Error(log);
|
||||
}
|
||||
|
||||
public void Error(Exception e)
|
||||
{
|
||||
if (e.Data.Contains("StackTrace"))
|
||||
{
|
||||
this.iLog.Error($"{e.Data["StackTrace"]}\n{e}");
|
||||
return;
|
||||
}
|
||||
|
||||
string str = e.ToString();
|
||||
this.Log(LogLevel.ERROR, str);
|
||||
this.iLog.Error(str);
|
||||
}
|
||||
|
||||
public void Fatal(string msg)
|
||||
{
|
||||
StackTrace st = new StackTrace(2, true);
|
||||
this.iLog.Fatal($"{msg}\n{st}");
|
||||
}
|
||||
|
||||
public void Trace(string message, params object[] args)
|
||||
{
|
||||
if (!CheckLogLevel(TraceLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StackTrace st = new StackTrace(2, true);
|
||||
this.iLog.Trace($"{string.Format(message, args)}\n{st}");
|
||||
}
|
||||
|
||||
public void Warning(string message, params object[] args)
|
||||
{
|
||||
if (!CheckLogLevel(WarningLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.iLog.Warning(string.Format(message, args));
|
||||
}
|
||||
|
||||
public void Info(string message, params object[] args)
|
||||
{
|
||||
if (!CheckLogLevel(InfoLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.iLog.Info(string.Format(message, args));
|
||||
}
|
||||
|
||||
public void Debug(string message, params object[] args)
|
||||
{
|
||||
if (!CheckLogLevel(DebugLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.iLog.Debug(string.Format(message, args));
|
||||
}
|
||||
|
||||
public void Error(string message, params object[] args)
|
||||
{
|
||||
StackTrace st = new StackTrace(2, true);
|
||||
string s = string.Format(message, args) + '\n' + st;
|
||||
this.iLog.Error(s);
|
||||
}
|
||||
|
||||
private StringBuilder GetFormatString(LogLevel logLevel, string logString)
|
||||
{
|
||||
_stringBuilder.Clear();
|
||||
switch (logLevel)
|
||||
{
|
||||
case LogLevel.DEBUG:
|
||||
_stringBuilder.AppendFormat(
|
||||
"[TEngine][DEBUG][{0:yyyy-MM-dd HH:mm:ss fff}] - {1}", DateTime.Now, logString);
|
||||
break;
|
||||
case LogLevel.INFO:
|
||||
_stringBuilder.AppendFormat(
|
||||
"[TEngine][INFO][{0:yyyy-MM-dd HH:mm:ss fff}] - {1}", DateTime.Now, logString);
|
||||
break;
|
||||
case LogLevel.ASSERT:
|
||||
_stringBuilder.AppendFormat(
|
||||
"[TEngine][ASSERT][{0:yyyy-MM-dd HH:mm:ss fff}] - {1}", DateTime.Now, logString);
|
||||
break;
|
||||
case LogLevel.WARNING:
|
||||
_stringBuilder.AppendFormat(
|
||||
"[TEngine][WARNING][{0:yyyy-MM-dd HH:mm:ss fff}] - {1}", DateTime.Now,
|
||||
logString);
|
||||
break;
|
||||
case LogLevel.ERROR:
|
||||
_stringBuilder.AppendFormat(
|
||||
"[TEngine][ERROR][{0:yyyy-MM-dd HH:mm:ss fff}] - {1}", DateTime.Now, logString);
|
||||
break;
|
||||
case LogLevel.EXCEPTION:
|
||||
_stringBuilder.AppendFormat(
|
||||
"[TEngine][EXCEPTION][{0:yyyy-MM-dd HH:mm:ss fff}] - {1}", DateTime.Now, logString);
|
||||
break;
|
||||
}
|
||||
|
||||
return _stringBuilder;
|
||||
}
|
||||
|
||||
private void Log(LogLevel type, string logString)
|
||||
{
|
||||
if (type < _filterLevel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder infoBuilder = GetFormatString(type, logString);
|
||||
|
||||
if (type == LogLevel.ERROR || type == LogLevel.WARNING || type == LogLevel.EXCEPTION)
|
||||
{
|
||||
StackFrame[] sf = new StackTrace().GetFrames();
|
||||
for (int i = 0; i < sf.Length; i++)
|
||||
{
|
||||
StackFrame frame = sf[i];
|
||||
string declaringTypeName = frame.GetMethod()?.DeclaringType.FullName;
|
||||
string methodName = frame.GetMethod()?.Name;
|
||||
infoBuilder.AppendFormat("[{0}::{1}\n", declaringTypeName, methodName);
|
||||
}
|
||||
}
|
||||
|
||||
string logStr = infoBuilder.ToString();
|
||||
|
||||
if (type == LogLevel.INFO)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.WriteLine(logStr);
|
||||
}
|
||||
else if (type == LogLevel.DEBUG)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine(logStr);
|
||||
}
|
||||
else if (type == LogLevel.WARNING)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
||||
Console.WriteLine(logStr);
|
||||
}
|
||||
else if (type == LogLevel.ASSERT)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(logStr);
|
||||
}
|
||||
else if (type == LogLevel.ERROR)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(logStr);
|
||||
}
|
||||
else if (type == LogLevel.EXCEPTION)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Magenta;
|
||||
Console.WriteLine(logStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
11
Assets/GameScripts/DotNet/Core/Log/LogCore.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Log/LogCore.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a0e8dec854eecb448be2f1bf80fc3d3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
75
Assets/GameScripts/DotNet/Core/Log/NLog.cs
Normal file
75
Assets/GameScripts/DotNet/Core/Log/NLog.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
#if TENGINE_NET
|
||||
using NLog;
|
||||
|
||||
namespace TEngine;
|
||||
|
||||
public class NLog : ILog
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public NLog(string name)
|
||||
{
|
||||
_logger = LogManager.GetLogger(name);
|
||||
}
|
||||
|
||||
public void Trace(string message)
|
||||
{
|
||||
_logger.Trace(message);
|
||||
}
|
||||
|
||||
public void Warning(string message)
|
||||
{
|
||||
_logger.Warn(message);
|
||||
}
|
||||
|
||||
public void Info(string message)
|
||||
{
|
||||
_logger.Info(message);
|
||||
}
|
||||
|
||||
public void Debug(string message)
|
||||
{
|
||||
_logger.Debug(message);
|
||||
}
|
||||
|
||||
public void Error(string message)
|
||||
{
|
||||
_logger.Error(message);
|
||||
}
|
||||
|
||||
public void Fatal(string message)
|
||||
{
|
||||
_logger.Fatal(message);
|
||||
}
|
||||
|
||||
public void Trace(string message, params object[] args)
|
||||
{
|
||||
_logger.Trace(message, args);
|
||||
}
|
||||
|
||||
public void Warning(string message, params object[] args)
|
||||
{
|
||||
_logger.Warn(message, args);
|
||||
}
|
||||
|
||||
public void Info(string message, params object[] args)
|
||||
{
|
||||
_logger.Info(message, args);
|
||||
}
|
||||
|
||||
public void Debug(string message, params object[] args)
|
||||
{
|
||||
_logger.Debug(message, args);
|
||||
}
|
||||
|
||||
public void Error(string message, params object[] args)
|
||||
{
|
||||
_logger.Error(message, args);
|
||||
}
|
||||
|
||||
public void Fatal(string message, params object[] args)
|
||||
{
|
||||
_logger.Fatal(message, args);
|
||||
}
|
||||
}
|
||||
#endif
|
11
Assets/GameScripts/DotNet/Core/Log/NLog.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Log/NLog.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9dee8b48fb3d80418d31ed532a904f0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user