LogLevel.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. public static class LogLevelExtensions
  44. {
  45. /// <summary>
  46. /// Gets the highest log level when there could potentially be multiple levels provided.
  47. /// </summary>
  48. /// <param name="levels">The log level(s).</param>
  49. /// <returns>The highest log level supplied.</returns>
  50. public static LogLevel GetHighestLevel(this LogLevel levels)
  51. {
  52. var enums = Enum.GetValues(typeof(LogLevel));
  53. Array.Sort(enums);
  54. foreach (LogLevel e in enums)
  55. {
  56. if ((levels & e) != LogLevel.None)
  57. return e;
  58. }
  59. return LogLevel.None;
  60. }
  61. /// <summary>
  62. /// Returns a translation of a log level to it's associated console colour.
  63. /// </summary>
  64. /// <param name="level">The log level(s).</param>
  65. /// <returns>A console color associated with the highest log level supplied.</returns>
  66. public static ConsoleColor GetConsoleColor(this LogLevel level)
  67. {
  68. level = GetHighestLevel(level);
  69. switch (level)
  70. {
  71. case LogLevel.Fatal:
  72. return ConsoleColor.Red;
  73. case LogLevel.Error:
  74. return ConsoleColor.DarkRed;
  75. case LogLevel.Warning:
  76. return ConsoleColor.Yellow;
  77. case LogLevel.Message:
  78. return ConsoleColor.White;
  79. case LogLevel.Info:
  80. default:
  81. return ConsoleColor.Gray;
  82. case LogLevel.Debug:
  83. return ConsoleColor.DarkGray;
  84. }
  85. }
  86. }
  87. }