LogLevel.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. namespace BepInEx.Logging
  3. {
  4. /// <summary>
  5. /// The level, or severity of a log entry.
  6. /// </summary>
  7. [Flags]
  8. public enum LogLevel
  9. {
  10. /// <summary>
  11. /// No level selected.
  12. /// </summary>
  13. None = 0,
  14. /// <summary>
  15. /// A fatal error has occurred, which cannot be recovered from.
  16. /// </summary>
  17. Fatal = 1,
  18. /// <summary>
  19. /// An error has occured, but can be recovered from.
  20. /// </summary>
  21. Error = 2,
  22. /// <summary>
  23. /// A warning has been produced, but does not necessarily mean that something wrong has happened.
  24. /// </summary>
  25. Warning = 4,
  26. /// <summary>
  27. /// An important message that should be displayed to the user.
  28. /// </summary>
  29. Message = 8,
  30. /// <summary>
  31. /// A message of low importance.
  32. /// </summary>
  33. Info = 16,
  34. /// <summary>
  35. /// A message that would likely only interest a developer.
  36. /// </summary>
  37. Debug = 32,
  38. /// <summary>
  39. /// All log levels.
  40. /// </summary>
  41. All = Fatal | Error | Warning | Message | Info | Debug
  42. }
  43. /// <summary>
  44. /// Helper methods for log level handling.
  45. /// </summary>
  46. public static class LogLevelExtensions
  47. {
  48. /// <summary>
  49. /// Gets the highest log level when there could potentially be multiple levels provided.
  50. /// </summary>
  51. /// <param name="levels">The log level(s).</param>
  52. /// <returns>The highest log level supplied.</returns>
  53. public static LogLevel GetHighestLevel(this LogLevel levels)
  54. {
  55. var enums = Enum.GetValues(typeof(LogLevel));
  56. Array.Sort(enums);
  57. foreach (LogLevel e in enums)
  58. {
  59. if ((levels & e) != LogLevel.None)
  60. return e;
  61. }
  62. return LogLevel.None;
  63. }
  64. /// <summary>
  65. /// Returns a translation of a log level to it's associated console colour.
  66. /// </summary>
  67. /// <param name="level">The log level(s).</param>
  68. /// <returns>A console color associated with the highest log level supplied.</returns>
  69. public static ConsoleColor GetConsoleColor(this LogLevel level)
  70. {
  71. level = GetHighestLevel(level);
  72. switch (level)
  73. {
  74. case LogLevel.Fatal:
  75. return ConsoleColor.Red;
  76. case LogLevel.Error:
  77. return ConsoleColor.DarkRed;
  78. case LogLevel.Warning:
  79. return ConsoleColor.Yellow;
  80. case LogLevel.Message:
  81. return ConsoleColor.White;
  82. case LogLevel.Info:
  83. default:
  84. return ConsoleColor.Gray;
  85. case LogLevel.Debug:
  86. return ConsoleColor.DarkGray;
  87. }
  88. }
  89. }
  90. }