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.

271 lines
8.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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 debug message.
  52. /// </summary>
  53. Debug = 1,
  54. /// <summary>
  55. /// An informational message.
  56. /// </summary>
  57. Info = 2,
  58. /// <summary>
  59. /// A notice. More significant than Info, but less than a warning.
  60. /// </summary>
  61. Notice = 32,
  62. /// <summary>
  63. /// A warning message.
  64. /// </summary>
  65. Warning = 4,
  66. /// <summary>
  67. /// An error message.
  68. /// </summary>
  69. Error = 8,
  70. /// <summary>
  71. /// A critical error message.
  72. /// </summary>
  73. Critical = 16
  74. }
  75. /// <summary>
  76. /// An enum providing log level filters.
  77. /// </summary>
  78. [Flags]
  79. public enum LogLevel : byte
  80. {
  81. /// <summary>
  82. /// Allow no messages through.
  83. /// </summary>
  84. None = Level.None,
  85. /// <summary>
  86. /// Only shows Debug messages.
  87. /// </summary>
  88. DebugOnly = Level.Debug,
  89. /// <summary>
  90. /// Only shows info messages.
  91. /// </summary>
  92. InfoOnly = Level.Info,
  93. /// <summary>
  94. /// Only shows notice messages.
  95. /// </summary>
  96. NoticeOnly = Level.Notice,
  97. /// <summary>
  98. /// Only shows Warning messages.
  99. /// </summary>
  100. WarningOnly = Level.Warning,
  101. /// <summary>
  102. /// Only shows Error messages.
  103. /// </summary>
  104. ErrorOnly = Level.Error,
  105. /// <summary>
  106. /// Only shows Critical messages.
  107. /// </summary>
  108. CriticalOnly = Level.Critical,
  109. /// <summary>
  110. /// Shows all messages error and up.
  111. /// </summary>
  112. ErrorUp = ErrorOnly | CriticalOnly,
  113. /// <summary>
  114. /// Shows all messages warning and up.
  115. /// </summary>
  116. WarningUp = WarningOnly | ErrorUp,
  117. /// <summary>
  118. /// Shows all messages Notice and up.
  119. /// </summary>
  120. NoticeUp = WarningUp | NoticeOnly,
  121. /// <summary>
  122. /// Shows all messages info and up.
  123. /// </summary>
  124. InfoUp = InfoOnly | NoticeUp,
  125. /// <summary>
  126. /// Shows all messages.
  127. /// </summary>
  128. All = DebugOnly | InfoUp,
  129. /// <summary>
  130. /// Used for when the level is undefined.
  131. /// </summary>
  132. Undefined = Byte.MaxValue
  133. }
  134. /// <summary>
  135. /// A basic log function.
  136. /// </summary>
  137. /// <param name="level">the level of the message</param>
  138. /// <param name="message">the message to log</param>
  139. public abstract void Log(Level level, string message);
  140. /// <summary>
  141. /// A basic log function taking an exception to log.
  142. /// </summary>
  143. /// <param name="level">the level of the message</param>
  144. /// <param name="e">the exception to log</param>
  145. public virtual void Log(Level level, Exception e) => Log(level, e.ToString());
  146. /// <summary>
  147. /// Sends a debug message.
  148. /// Equivalent to `Log(Level.Debug, message);`
  149. /// </summary>
  150. /// <seealso cref="Log(Level, string)"/>
  151. /// <param name="message">the message to log</param>
  152. public virtual void Debug(string message) => Log(Level.Debug, message);
  153. /// <summary>
  154. /// Sends an exception as a debug message.
  155. /// Equivalent to `Log(Level.Debug, e);`
  156. /// </summary>
  157. /// <seealso cref="Log(Level, Exception)"/>
  158. /// <param name="e">the exception to log</param>
  159. public virtual void Debug(Exception e) => Log(Level.Debug, e);
  160. /// <summary>
  161. /// Sends an info message.
  162. /// Equivalent to `Log(Level.Info, message);`
  163. /// </summary>
  164. /// <seealso cref="Log(Level, string)"/>
  165. /// <param name="message">the message to log</param>
  166. public virtual void Info(string message) => Log(Level.Info, message);
  167. /// <summary>
  168. /// Sends an exception as an info message.
  169. /// Equivalent to `Log(Level.Info, e);`
  170. /// </summary>
  171. /// <seealso cref="Log(Level, Exception)"/>
  172. /// <param name="e">the exception to log</param>
  173. public virtual void Info(Exception e) => Log(Level.Info, e);
  174. /// <summary>
  175. /// Sends a notice message.
  176. /// Equivalent to `Log(Level.Notice, message);`
  177. /// </summary>
  178. /// <seealso cref="Log(Level, string)"/>
  179. /// <param name="message">the message to log</param>
  180. public virtual void Notice(string message) => Log(Level.Notice, message);
  181. /// <summary>
  182. /// Sends an exception as a notice message.
  183. /// Equivalent to `Log(Level.Notice, e);`
  184. /// </summary>
  185. /// <seealso cref="Log(Level, Exception)"/>
  186. /// <param name="e">the exception to log</param>
  187. public virtual void Notice(Exception e) => Log(Level.Notice, e);
  188. /// <summary>
  189. /// Sends a warning message.
  190. /// Equivalent to `Log(Level.Warning, message);`
  191. /// </summary>
  192. /// <seealso cref="Log(Level, string)"/>
  193. /// <param name="message">the message to log</param>
  194. public virtual void Warn(string message) => Log(Level.Warning, message);
  195. /// <summary>
  196. /// Sends an exception as a warning message.
  197. /// Equivalent to `Log(Level.Warning, e);`
  198. /// </summary>
  199. /// <seealso cref="Log(Level, Exception)"/>
  200. /// <param name="e">the exception to log</param>
  201. public virtual void Warn(Exception e) => Log(Level.Warning, e);
  202. /// <summary>
  203. /// Sends an error message.
  204. /// Equivalent to `Log(Level.Error, message);`
  205. /// </summary>
  206. /// <seealso cref="Log(Level, string)"/>
  207. /// <param name="message">the message to log</param>
  208. public virtual void Error(string message) => Log(Level.Error, message);
  209. /// <summary>
  210. /// Sends an exception as an error message.
  211. /// Equivalent to `Log(Level.Error, e);`
  212. /// </summary>
  213. /// <seealso cref="Log(Level, Exception)"/>
  214. /// <param name="e">the exception to log</param>
  215. public virtual void Error(Exception e) => Log(Level.Error, e);
  216. /// <summary>
  217. /// Sends a critical message.
  218. /// Equivalent to `Log(Level.Critical, message);`
  219. /// </summary>
  220. /// <seealso cref="Log(Level, string)"/>
  221. /// <param name="message">the message to log</param>
  222. public virtual void Critical(string message) => Log(Level.Critical, message);
  223. /// <summary>
  224. /// Sends an exception as a critical message.
  225. /// Equivalent to `Log(Level.Critical, e);`
  226. /// </summary>
  227. /// <seealso cref="Log(Level, Exception)"/>
  228. /// <param name="e">the exception to log</param>
  229. public virtual void Critical(Exception e) => Log(Level.Critical, e);
  230. }
  231. }