PreloaderLogWriter.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. using BepInEx.Bootstrap;
  6. using BepInEx.ConsoleUtil;
  7. namespace BepInEx.Logging
  8. {
  9. /// <summary>
  10. /// A log writer specific to the <see cref="Preloader"/>.
  11. /// </summary>
  12. /// <inheritdoc cref="BaseLogger"/>
  13. public class PreloaderLogWriter : BaseLogger
  14. {
  15. /// <summary>
  16. /// The <see cref="System.Text.StringBuilder"/> which contains all logged entries, so it may be passed onto another log writer.
  17. /// </summary>
  18. public StringBuilder StringBuilder { get; protected set; } = new StringBuilder();
  19. /// <summary>
  20. /// Whether or not the log writer is redirecting console output, so it can be logged.
  21. /// </summary>
  22. public bool IsRedirectingConsole { get; protected set; }
  23. protected TextWriter stdout;
  24. protected LoggerTraceListener traceListener;
  25. private bool _enabled = false;
  26. /// <summary>
  27. /// Whether or not the log writer is writing and/or redirecting.
  28. /// </summary>
  29. public bool Enabled {
  30. get => _enabled;
  31. set
  32. {
  33. if (value)
  34. Enable();
  35. else
  36. Disable();
  37. }
  38. }
  39. /// <param name="redirectConsole">Whether or not to redirect the console standard output.</param>
  40. public PreloaderLogWriter(bool redirectConsole)
  41. {
  42. IsRedirectingConsole = redirectConsole;
  43. stdout = Console.Out;
  44. traceListener = new LoggerTraceListener(this);
  45. }
  46. /// <summary>
  47. /// Enables the log writer.
  48. /// </summary>
  49. public void Enable()
  50. {
  51. if (_enabled)
  52. return;
  53. if (IsRedirectingConsole)
  54. Console.SetOut(this);
  55. else
  56. Console.SetOut(TextWriter.Null);
  57. Trace.Listeners.Add(traceListener);
  58. _enabled = true;
  59. }
  60. /// <summary>
  61. /// Disables the log writer.
  62. /// </summary>
  63. public void Disable()
  64. {
  65. if (!_enabled)
  66. return;
  67. Console.SetOut(stdout);
  68. Trace.Listeners.Remove(traceListener);
  69. _enabled = false;
  70. }
  71. /// <summary>
  72. /// Logs an entry to the Logger instance.
  73. /// </summary>
  74. /// <param name="level">The level of the entry.</param>
  75. /// <param name="entry">The textual value of the entry.</param>
  76. public override void Log(LogLevel level, object entry)
  77. {
  78. Kon.ForegroundColor = level.GetConsoleColor();
  79. base.Log(level, entry);
  80. Kon.ForegroundColor = ConsoleColor.Gray;
  81. }
  82. public override void Write(char value)
  83. {
  84. StringBuilder.Append(value);
  85. stdout.Write(value);
  86. }
  87. public override void Write(string value)
  88. {
  89. StringBuilder.Append(value);
  90. stdout.Write(value);
  91. }
  92. protected override void Dispose(bool disposing)
  93. {
  94. Disable();
  95. StringBuilder.Length = 0;
  96. traceListener?.Dispose();
  97. traceListener = null;
  98. }
  99. public override string ToString()
  100. {
  101. return StringBuilder.ToString().Trim();
  102. }
  103. }
  104. }