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.

182 lines
6.5 KiB

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