Log.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Net;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. #if NOT_UNITY
  9. using NLog;
  10. #endif
  11. namespace ET
  12. {
  13. public static class Log
  14. {
  15. public static ILog ILog { get; set; } = new SplitFilesLogger();
  16. public static bool Verbose { get; set; } = true;
  17. private const int TraceLevel = 1;
  18. private const int DebugLevel = 2;
  19. private const int InfoLevel = 3;
  20. private const int WarningLevel = 4;
  21. static StringBuilder sb = new StringBuilder();
  22. private static bool CheckLogLevel(int level)
  23. {
  24. return Options.Instance.LogLevel <= level;
  25. }
  26. public static void Trace(string msg,
  27. [CallerFilePath] string file=null,
  28. [CallerLineNumber] int line=0,
  29. [CallerMemberName] string member=null)
  30. {
  31. if (!CheckLogLevel(DebugLevel))
  32. {
  33. return;
  34. }
  35. StackTrace st = new StackTrace(1, true);
  36. msg = Verbose ? FormatVerbose(msg, file, line, member) : msg;
  37. ILog.Trace($"[T]{msg}\n{st}");
  38. }
  39. public static void Debug(string msg,
  40. [CallerFilePath] string file=null,
  41. [CallerLineNumber] int line=0,
  42. [CallerMemberName] string member=null)
  43. {
  44. if (!CheckLogLevel(DebugLevel))
  45. {
  46. return;
  47. }
  48. msg = Verbose ? FormatVerbose(msg, file, line, member) : msg;
  49. ILog.Debug($"[D]{msg}");
  50. }
  51. public static void Info(string msg,
  52. [CallerFilePath] string file=null,
  53. [CallerLineNumber] int line=0,
  54. [CallerMemberName] string member=null)
  55. {
  56. if (!CheckLogLevel(InfoLevel))
  57. {
  58. return;
  59. }
  60. msg = Verbose ? FormatVerbose(msg, file, line, member) : msg;
  61. ILog.Info($"[I]{msg}");
  62. }
  63. public static void TraceInfo(string msg,
  64. [CallerFilePath] string file=null,
  65. [CallerLineNumber] int line=0,
  66. [CallerMemberName] string member=null)
  67. {
  68. if (!CheckLogLevel(InfoLevel))
  69. {
  70. return;
  71. }
  72. StackTrace st = new StackTrace(1, true);
  73. msg = Verbose ? FormatVerbose(msg, file, line, member) : msg;
  74. ILog.Trace($"[I]{msg}\n{st}");
  75. }
  76. public static void Warning(string msg,
  77. [CallerFilePath] string file=null,
  78. [CallerLineNumber] int line=0,
  79. [CallerMemberName] string member=null)
  80. {
  81. if (!CheckLogLevel(WarningLevel))
  82. {
  83. return;
  84. }
  85. msg = Verbose ? FormatVerbose(msg, file, line, member) : msg;
  86. ILog.Warning($"[W]{msg}");
  87. }
  88. public static void Error(string msg,
  89. [CallerFilePath] string file=null,
  90. [CallerLineNumber] int line=0,
  91. [CallerMemberName] string member=null)
  92. {
  93. StackTrace st = new StackTrace(1, true);
  94. msg = Verbose ? FormatVerbose(msg, file, line, member) : msg;
  95. ILog.Error($"[E]{msg}\n{st}");
  96. }
  97. public static void Error(Exception e)
  98. {
  99. if (e.Data.Contains("StackTrace"))
  100. {
  101. ILog.Error($"{e.Data["StackTrace"]}\n{e}");
  102. return;
  103. }
  104. string str = e.ToString();
  105. ILog.Error($"[E]{str}");
  106. }
  107. public static void Trace(string message, params object[] args)
  108. {
  109. if (!CheckLogLevel(TraceLevel))
  110. {
  111. return;
  112. }
  113. StackTrace st = new StackTrace(1, true);
  114. ILog.Trace($"[T]{string.Format(message, args)}\n{st}");
  115. }
  116. public static void Warning(string message, params object[] args)
  117. {
  118. if (!CheckLogLevel(WarningLevel))
  119. {
  120. return;
  121. }
  122. ILog.Warning(string.Format(message, args));
  123. }
  124. public static void Info(string message, params object[] args)
  125. {
  126. if (!CheckLogLevel(InfoLevel))
  127. {
  128. return;
  129. }
  130. ILog.Info(string.Format(message, args));
  131. }
  132. public static void Debug(string message, params object[] args)
  133. {
  134. if (!CheckLogLevel(DebugLevel))
  135. {
  136. return;
  137. }
  138. ILog.Debug(string.Format(message, args));
  139. }
  140. public static void Error(string message, params object[] args)
  141. {
  142. StackTrace st = new StackTrace(1, true);
  143. string s = string.Format(message, args) + '\n' + st;
  144. ILog.Error(s);
  145. }
  146. public static void Console(string message,
  147. [CallerFilePath] string file=null,
  148. [CallerLineNumber] int line=0,
  149. [CallerMemberName] string member=null)
  150. {
  151. if (Options.Instance.Console == 1)
  152. {
  153. System.Console.WriteLine(message);
  154. }
  155. message = Verbose ? FormatVerbose(message, file, line, member) : message;
  156. ILog.Debug($"[C]{message}");
  157. }
  158. public static void Console(string message, params object[] args)
  159. {
  160. string s = string.Format(message, args);
  161. if (Options.Instance.Console == 1)
  162. {
  163. System.Console.WriteLine(s);
  164. }
  165. ILog.Debug(s);
  166. }
  167. static string FormatVerbose (string msg, string file, int line, string member)
  168. {
  169. Match match = Regex.Match(file, @".+[/\\](.+)");
  170. return sb.Clear()
  171. .Append($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] ")
  172. .Append($"[{match.Groups[1].Value}] ")
  173. .Append($"[{line}] ")
  174. .AppendLine($"[{member}]")
  175. .Append(msg).ToString();
  176. }
  177. }
  178. }