using System; using System.Diagnostics; using System.IO; using System.Net; using System.Runtime.CompilerServices; using System.Text; using System.Text.RegularExpressions; #if NOT_UNITY using NLog; #endif namespace ET { public static class Log { public static ILog ILog { get; set; } = new SplitFilesLogger(); public static bool Verbose { get; set; } = true; private const int TraceLevel = 1; private const int DebugLevel = 2; private const int InfoLevel = 3; private const int WarningLevel = 4; static StringBuilder sb = new StringBuilder(); private static bool CheckLogLevel(int level) { return Options.Instance.LogLevel <= level; } public static void Trace(string msg, [CallerFilePath] string file=null, [CallerLineNumber] int line=0, [CallerMemberName] string member=null) { if (!CheckLogLevel(DebugLevel)) { return; } StackTrace st = new StackTrace(1, true); msg = Verbose ? FormatVerbose(msg, file, line, member) : msg; ILog.Trace($"[T]{msg}\n{st}"); } public static void Debug(string msg, [CallerFilePath] string file=null, [CallerLineNumber] int line=0, [CallerMemberName] string member=null) { if (!CheckLogLevel(DebugLevel)) { return; } msg = Verbose ? FormatVerbose(msg, file, line, member) : msg; ILog.Debug($"[D]{msg}"); } public static void Info(string msg, [CallerFilePath] string file=null, [CallerLineNumber] int line=0, [CallerMemberName] string member=null) { if (!CheckLogLevel(InfoLevel)) { return; } msg = Verbose ? FormatVerbose(msg, file, line, member) : msg; ILog.Info($"[I]{msg}"); } public static void TraceInfo(string msg, [CallerFilePath] string file=null, [CallerLineNumber] int line=0, [CallerMemberName] string member=null) { if (!CheckLogLevel(InfoLevel)) { return; } StackTrace st = new StackTrace(1, true); msg = Verbose ? FormatVerbose(msg, file, line, member) : msg; ILog.Trace($"[I]{msg}\n{st}"); } public static void Warning(string msg, [CallerFilePath] string file=null, [CallerLineNumber] int line=0, [CallerMemberName] string member=null) { if (!CheckLogLevel(WarningLevel)) { return; } msg = Verbose ? FormatVerbose(msg, file, line, member) : msg; ILog.Warning($"[W]{msg}"); } public static void Error(string msg, [CallerFilePath] string file=null, [CallerLineNumber] int line=0, [CallerMemberName] string member=null) { StackTrace st = new StackTrace(1, true); msg = Verbose ? FormatVerbose(msg, file, line, member) : msg; ILog.Error($"[E]{msg}\n{st}"); } public static void Error(Exception e) { if (e.Data.Contains("StackTrace")) { ILog.Error($"{e.Data["StackTrace"]}\n{e}"); return; } string str = e.ToString(); ILog.Error($"[E]{str}"); } public static void Trace(string message, params object[] args) { if (!CheckLogLevel(TraceLevel)) { return; } StackTrace st = new StackTrace(1, true); ILog.Trace($"[T]{string.Format(message, args)}\n{st}"); } public static void Warning(string message, params object[] args) { if (!CheckLogLevel(WarningLevel)) { return; } ILog.Warning(string.Format(message, args)); } public static void Info(string message, params object[] args) { if (!CheckLogLevel(InfoLevel)) { return; } ILog.Info(string.Format(message, args)); } public static void Debug(string message, params object[] args) { if (!CheckLogLevel(DebugLevel)) { return; } ILog.Debug(string.Format(message, args)); } public static void Error(string message, params object[] args) { StackTrace st = new StackTrace(1, true); string s = string.Format(message, args) + '\n' + st; ILog.Error(s); } public static void Console(string message, [CallerFilePath] string file=null, [CallerLineNumber] int line=0, [CallerMemberName] string member=null) { if (Options.Instance.Console == 1) { System.Console.WriteLine(message); } message = Verbose ? FormatVerbose(message, file, line, member) : message; ILog.Debug($"[C]{message}"); } public static void Console(string message, params object[] args) { string s = string.Format(message, args); if (Options.Instance.Console == 1) { System.Console.WriteLine(s); } ILog.Debug(s); } static string FormatVerbose (string msg, string file, int line, string member) { Match match = Regex.Match(file, @".+[/\\](.+)"); return sb.Clear() .Append($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] ") .Append($"[{match.Groups[1].Value}] ") .Append($"[{line}] ") .AppendLine($"[{member}]") .Append(msg).ToString(); } } }