BaseLogger.cs 1.2 KB

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