ConsoleLogListener.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using BepInEx.Configuration;
  3. namespace BepInEx.Logging
  4. {
  5. /// <summary>
  6. /// Logs entries using Unity specific outputs.
  7. /// </summary>
  8. public class ConsoleLogListener : ILogListener
  9. {
  10. public void LogEvent(object sender, LogEventArgs eventArgs)
  11. {
  12. if ((eventArgs.Level & ConfigConsoleDisplayedLevel.Value) == 0)
  13. return;
  14. ConsoleManager.SetConsoleColor(eventArgs.Level.GetConsoleColor());
  15. Log(eventArgs);
  16. ConsoleManager.SetConsoleColor(ConsoleColor.Gray);
  17. }
  18. private void Log(LogEventArgs eventArgs)
  19. {
  20. // Special case: if message comes from Unity log, it's already logged by Unity, in which case
  21. // we replay it only in the visible console
  22. if (eventArgs.Source is UnityLogSource)
  23. ConsoleManager.ConsoleStream?.Write(eventArgs.ToStringLine());
  24. else
  25. Console.Write(eventArgs.ToStringLine());
  26. }
  27. public void Dispose() { }
  28. private static readonly ConfigEntry<LogLevel> ConfigConsoleDisplayedLevel = ConfigFile.CoreConfig.Bind(
  29. "Logging.Console","LogLevels",
  30. LogLevel.Fatal | LogLevel.Error | LogLevel.Message | LogLevel.Info | LogLevel.Warning,
  31. "Which log levels to show in the console output.");
  32. }
  33. }