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.

299 lines
9.8 KiB

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