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.

198 lines
7.2 KiB

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