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.

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