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.

197 lines
7.1 KiB

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