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.

195 lines
7.1 KiB

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