| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- 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();
- }
- }
- }
|