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.

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