You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
10 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. using System;
  2. // ReSharper disable InconsistentNaming
  3. namespace IPA.Logging
  4. {
  5. /// <summary>
  6. /// The logger base class. Provides the format for console logs.
  7. /// </summary>
  8. public abstract class Logger
  9. {
  10. private static Logger _log;
  11. internal static Logger log
  12. {
  13. get
  14. {
  15. if (_log == null)
  16. _log = new StandardLogger("IPA");
  17. return _log;
  18. }
  19. }
  20. private static StandardLogger _stdout;
  21. internal static StandardLogger stdout
  22. {
  23. get
  24. {
  25. if (_stdout == null)
  26. _stdout = new StandardLogger("_");
  27. return _stdout;
  28. }
  29. }
  30. internal static Logger AntiMalware => log.GetChildLogger("AntiMalware");
  31. internal static Logger updater => log.GetChildLogger("Updater");
  32. internal static Logger libLoader => log.GetChildLogger("LibraryLoader");
  33. internal static Logger injector => log.GetChildLogger("Injector");
  34. internal static Logger loader => log.GetChildLogger("Loader");
  35. internal static Logger features => loader.GetChildLogger("Features");
  36. internal static Logger config => log.GetChildLogger("Config");
  37. internal static bool LogCreated => _log != null;
  38. /// <summary>
  39. /// The standard format for log messages.
  40. /// </summary>
  41. /// <value>the format for the standard loggers to print in</value>
  42. public static string LogFormat { get; protected internal set; } = "[{3} @ {2:HH:mm:ss} | {1}] {0}";
  43. /// <summary>
  44. /// An enum specifying the level of the message. Resembles Syslog.
  45. /// </summary>
  46. public enum Level : byte
  47. {
  48. /// <summary>
  49. /// No associated level. These never get shown.
  50. /// </summary>
  51. None = 0,
  52. /// <summary>
  53. /// A trace message. These are ignored <i>incredibly</i> early.
  54. /// </summary>
  55. Trace = 64,
  56. /// <summary>
  57. /// A debug message.
  58. /// </summary>
  59. Debug = 1,
  60. /// <summary>
  61. /// An informational message.
  62. /// </summary>
  63. Info = 2,
  64. /// <summary>
  65. /// A notice. More significant than Info, but less than a warning.
  66. /// </summary>
  67. Notice = 32,
  68. /// <summary>
  69. /// A warning message.
  70. /// </summary>
  71. Warning = 4,
  72. /// <summary>
  73. /// An error message.
  74. /// </summary>
  75. Error = 8,
  76. /// <summary>
  77. /// A critical error message.
  78. /// </summary>
  79. Critical = 16
  80. }
  81. /// <summary>
  82. /// An enum providing log level filters.
  83. /// </summary>
  84. [Flags]
  85. public enum LogLevel : byte
  86. {
  87. /// <summary>
  88. /// Allow no messages through.
  89. /// </summary>
  90. None = Level.None,
  91. /// <summary>
  92. /// Only shows Trace messages.
  93. /// </summary>
  94. TraceOnly = Level.Trace,
  95. /// <summary>
  96. /// Only shows Debug messages.
  97. /// </summary>
  98. DebugOnly = Level.Debug,
  99. /// <summary>
  100. /// Only shows info messages.
  101. /// </summary>
  102. InfoOnly = Level.Info,
  103. /// <summary>
  104. /// Only shows notice messages.
  105. /// </summary>
  106. NoticeOnly = Level.Notice,
  107. /// <summary>
  108. /// Only shows Warning messages.
  109. /// </summary>
  110. WarningOnly = Level.Warning,
  111. /// <summary>
  112. /// Only shows Error messages.
  113. /// </summary>
  114. ErrorOnly = Level.Error,
  115. /// <summary>
  116. /// Only shows Critical messages.
  117. /// </summary>
  118. CriticalOnly = Level.Critical,
  119. /// <summary>
  120. /// Shows all messages error and up.
  121. /// </summary>
  122. ErrorUp = ErrorOnly | CriticalOnly,
  123. /// <summary>
  124. /// Shows all messages warning and up.
  125. /// </summary>
  126. WarningUp = WarningOnly | ErrorUp,
  127. /// <summary>
  128. /// Shows all messages Notice and up.
  129. /// </summary>
  130. NoticeUp = WarningUp | NoticeOnly,
  131. /// <summary>
  132. /// Shows all messages info and up.
  133. /// </summary>
  134. InfoUp = InfoOnly | NoticeUp,
  135. /// <summary>
  136. /// Shows all messages debug and up.
  137. /// </summary>
  138. DebugUp = DebugOnly | InfoUp,
  139. /// <summary>
  140. /// Shows all messages.
  141. /// </summary>
  142. All = TraceOnly | DebugUp,
  143. /// <summary>
  144. /// Used for when the level is undefined.
  145. /// </summary>
  146. Undefined = byte.MaxValue
  147. }
  148. /// <summary>
  149. /// A basic log function.
  150. /// </summary>
  151. /// <param name="level">the level of the message</param>
  152. /// <param name="message">the message to log</param>
  153. public abstract void Log(Level level, string message);
  154. /// <summary>
  155. /// A basic log function taking an exception to log.
  156. /// </summary>
  157. /// <param name="level">the level of the message</param>
  158. /// <param name="e">the exception to log</param>
  159. public virtual void Log(Level level, Exception e) => Log(level, e.ToString());
  160. /// <summary>
  161. /// Sends a trace message.
  162. /// Equivalent to <c>Log(Level.Trace, message);</c>
  163. /// </summary>
  164. /// <seealso cref="Log(Level, string)"/>
  165. /// <param name="message">the message to log</param>
  166. public virtual void Trace(string message) => Log(Level.Trace, message);
  167. /// <summary>
  168. /// Sends an exception as a trace message.
  169. /// Equivalent to <c>Log(Level.Trace, e);</c>
  170. /// </summary>
  171. /// <seealso cref="Log(Level, Exception)"/>
  172. /// <param name="e">the exception to log</param>
  173. public virtual void Trace(Exception e) => Log(Level.Trace, e);
  174. /// <summary>
  175. /// Sends a debug message.
  176. /// Equivalent to <c>Log(Level.Debug, message);</c>
  177. /// </summary>
  178. /// <seealso cref="Log(Level, string)"/>
  179. /// <param name="message">the message to log</param>
  180. public virtual void Debug(string message) => Log(Level.Debug, message);
  181. /// <summary>
  182. /// Sends an exception as a debug message.
  183. /// Equivalent to <c>Log(Level.Debug, e);</c>
  184. /// </summary>
  185. /// <seealso cref="Log(Level, Exception)"/>
  186. /// <param name="e">the exception to log</param>
  187. public virtual void Debug(Exception e) => Log(Level.Debug, e);
  188. /// <summary>
  189. /// Sends an info message.
  190. /// Equivalent to <c>Log(Level.Info, message);</c>
  191. /// </summary>
  192. /// <seealso cref="Log(Level, string)"/>
  193. /// <param name="message">the message to log</param>
  194. public virtual void Info(string message) => Log(Level.Info, message);
  195. /// <summary>
  196. /// Sends an exception as an info message.
  197. /// Equivalent to <c>Log(Level.Info, e);</c>
  198. /// </summary>
  199. /// <seealso cref="Log(Level, Exception)"/>
  200. /// <param name="e">the exception to log</param>
  201. public virtual void Info(Exception e) => Log(Level.Info, e);
  202. /// <summary>
  203. /// Sends a notice message.
  204. /// Equivalent to <c>Log(Level.Notice, message);</c>
  205. /// </summary>
  206. /// <seealso cref="Log(Level, string)"/>
  207. /// <param name="message">the message to log</param>
  208. public virtual void Notice(string message) => Log(Level.Notice, message);
  209. /// <summary>
  210. /// Sends an exception as a notice message.
  211. /// Equivalent to <c>Log(Level.Notice, e);</c>
  212. /// </summary>
  213. /// <seealso cref="Log(Level, Exception)"/>
  214. /// <param name="e">the exception to log</param>
  215. public virtual void Notice(Exception e) => Log(Level.Notice, e);
  216. /// <summary>
  217. /// Sends a warning message.
  218. /// Equivalent to <c>Log(Level.Warning, message);</c>
  219. /// </summary>
  220. /// <seealso cref="Log(Level, string)"/>
  221. /// <param name="message">the message to log</param>
  222. public virtual void Warn(string message) => Log(Level.Warning, message);
  223. /// <summary>
  224. /// Sends an exception as a warning message.
  225. /// Equivalent to <c>Log(Level.Warning, e);</c>
  226. /// </summary>
  227. /// <seealso cref="Log(Level, Exception)"/>
  228. /// <param name="e">the exception to log</param>
  229. public virtual void Warn(Exception e) => Log(Level.Warning, e);
  230. /// <summary>
  231. /// Sends an error message.
  232. /// Equivalent to <c>Log(Level.Error, message);</c>
  233. /// </summary>
  234. /// <seealso cref="Log(Level, string)"/>
  235. /// <param name="message">the message to log</param>
  236. public virtual void Error(string message) => Log(Level.Error, message);
  237. /// <summary>
  238. /// Sends an exception as an error message.
  239. /// Equivalent to <c>Log(Level.Error, e);</c>
  240. /// </summary>
  241. /// <seealso cref="Log(Level, Exception)"/>
  242. /// <param name="e">the exception to log</param>
  243. public virtual void Error(Exception e) => Log(Level.Error, e);
  244. /// <summary>
  245. /// Sends a critical message.
  246. /// Equivalent to <c>Log(Level.Critical, message);</c>
  247. /// </summary>
  248. /// <seealso cref="Log(Level, string)"/>
  249. /// <param name="message">the message to log</param>
  250. public virtual void Critical(string message) => Log(Level.Critical, message);
  251. /// <summary>
  252. /// Sends an exception as a critical message.
  253. /// Equivalent to <c>Log(Level.Critical, e);</c>
  254. /// </summary>
  255. /// <seealso cref="Log(Level, Exception)"/>
  256. /// <param name="e">the exception to log</param>
  257. public virtual void Critical(Exception e) => Log(Level.Critical, e);
  258. }
  259. }