PreloaderLogWriter.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. namespace BepInEx.Logger
  6. {
  7. public class PreloaderLogWriter : BaseLogger
  8. {
  9. public StringBuilder StringBuilder = new StringBuilder();
  10. protected TextWriter stdout;
  11. protected LoggerTraceListener traceListener;
  12. private bool _enabled = false;
  13. public bool Enabled {
  14. get => _enabled;
  15. set
  16. {
  17. if (value)
  18. Enable();
  19. else
  20. Disable();
  21. }
  22. }
  23. public PreloaderLogWriter()
  24. {
  25. stdout = Console.Out;
  26. traceListener = new LoggerTraceListener(this);
  27. }
  28. public void Enable()
  29. {
  30. if (Enabled)
  31. return;
  32. Console.SetOut(this);
  33. Trace.Listeners.Add(traceListener);
  34. _enabled = true;
  35. }
  36. public void Disable()
  37. {
  38. if (!Enabled)
  39. return;
  40. Console.SetOut(stdout);
  41. Trace.Listeners.Remove(traceListener);
  42. _enabled = false;
  43. }
  44. public override void Write(char value)
  45. {
  46. StringBuilder.Append(value);
  47. }
  48. public override void Write(string value)
  49. {
  50. StringBuilder.Append(value);
  51. }
  52. protected override void Dispose(bool disposing)
  53. {
  54. Disable();
  55. StringBuilder.Length = 0;
  56. base.Dispose(disposing);
  57. }
  58. public override string ToString()
  59. {
  60. return StringBuilder.ToString().Trim();
  61. }
  62. }
  63. }