BaseLogger.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.IO;
  2. using System.Text;
  3. namespace BepInEx.Logging
  4. {
  5. public abstract class BaseLogger : TextWriter
  6. {
  7. public override Encoding Encoding { get; } = new UTF8Encoding(true);
  8. /// <summary>
  9. /// The handler for a entry logged event.
  10. /// </summary>
  11. /// <param name="entry">The text element of the log itself.</param>
  12. /// <param name="show">Whether or not it should be dislpayed to the user.</param>
  13. public delegate void EntryLoggedEventHandler(LogLevel level, object entry);
  14. /// <summary>
  15. /// The listener event for an entry being logged.
  16. /// </summary>
  17. public static event EntryLoggedEventHandler EntryLogged;
  18. public LogLevel DisplayedLevels = LogLevel.All;
  19. public virtual void Log(LogLevel level, object entry)
  20. {
  21. if ((DisplayedLevels & level) != LogLevel.None)
  22. {
  23. EntryLogged?.Invoke(level, entry);
  24. WriteLine($"[{level}] {entry}");
  25. }
  26. }
  27. public virtual void Log(object entry)
  28. {
  29. Log(LogLevel.Message, entry);
  30. }
  31. }
  32. }