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.

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