BaseLogger.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 event EntryLoggedEventHandler EntryLogged;
  18. public LogLevel DisplayedLevels = LogLevel.All;
  19. private object logLockObj = new object();
  20. public virtual void Log(LogLevel level, object entry)
  21. {
  22. if ((DisplayedLevels & level) != LogLevel.None)
  23. {
  24. lock (logLockObj)
  25. {
  26. EntryLogged?.Invoke(level, entry);
  27. WriteLine($"[{level}] {entry}");
  28. }
  29. }
  30. }
  31. public virtual void Log(object entry)
  32. {
  33. Log(LogLevel.Message, entry);
  34. }
  35. }
  36. }