BaseLogger.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace BepInEx.Logging
  5. {
  6. /// <summary>
  7. /// The base implementation of a logging class.
  8. /// </summary>
  9. public abstract class BaseLogger : TextWriter
  10. {
  11. protected BaseLogger()
  12. {
  13. DisplayedLevels = GetDefaultLogLevel();
  14. }
  15. /// <summary>
  16. /// The encoding that the underlying text writer should use. Defaults to UTF-8 BOM.
  17. /// </summary>
  18. public override Encoding Encoding { get; } = new UTF8Encoding(true);
  19. /// <summary>
  20. /// The handler for a entry logged event.
  21. /// </summary>
  22. /// <param name="entry">The text element of the log itself.</param>
  23. /// <param name="show">Whether or not it should be dislpayed to the user.</param>
  24. public delegate void EntryLoggedEventHandler(LogLevel level, object entry);
  25. /// <summary>
  26. /// The listener event for an entry being logged.
  27. /// </summary>
  28. public event EntryLoggedEventHandler EntryLogged;
  29. /// <summary>
  30. /// Retrieves the default log level to use for this logger.
  31. /// </summary>
  32. /// <returns>The default log level to use.</returns>
  33. protected virtual LogLevel GetDefaultLogLevel()
  34. {
  35. LogLevel lowest;
  36. try
  37. {
  38. lowest = (LogLevel)Enum.Parse(typeof(LogLevel), Config.GetEntry("logger-displayed-levels", nameof(LogLevel.Info), "BepInEx"));
  39. }
  40. catch
  41. {
  42. return LogLevel.All;
  43. }
  44. if (lowest == LogLevel.None)
  45. return LogLevel.None;
  46. LogLevel value = lowest;
  47. foreach (LogLevel e in Enum.GetValues(typeof(LogLevel)))
  48. {
  49. if (lowest > e)
  50. value |= e;
  51. }
  52. return value;
  53. }
  54. /// <summary>
  55. /// A filter which is used to specify which log levels are not ignored by the logger.
  56. /// </summary>
  57. public LogLevel DisplayedLevels { get; set; }
  58. private readonly object logLockObj = new object();
  59. /// <summary>
  60. /// Logs an entry to the Logger instance.
  61. /// </summary>
  62. /// <param name="level">The level of the entry.</param>
  63. /// <param name="entry">The textual value of the entry.</param>
  64. public virtual void Log(LogLevel level, object entry)
  65. {
  66. if ((DisplayedLevels & level) != LogLevel.None)
  67. {
  68. lock (logLockObj)
  69. {
  70. EntryLogged?.Invoke(level, entry);
  71. WriteLine($"[{level.GetHighestLevel()}] {entry}");
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// Logs an entry to the Logger instance, with a <see cref="LogLevel"/> of Message.
  77. /// </summary>
  78. /// <param name="entry">The text value of this log entry.</param>
  79. public virtual void Log(object entry)
  80. {
  81. Log(LogLevel.Message, entry);
  82. }
  83. }
  84. }