ConsoleLogTarget.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Concurrent;
  3. namespace Ryujinx.Common.Logging
  4. {
  5. public class ConsoleLogTarget : ILogTarget
  6. {
  7. private static readonly ConcurrentDictionary<LogLevel, ConsoleColor> _logColors;
  8. private readonly ILogFormatter _formatter;
  9. static ConsoleLogTarget()
  10. {
  11. _logColors = new ConcurrentDictionary<LogLevel, ConsoleColor> {
  12. [ LogLevel.Stub ] = ConsoleColor.DarkGray,
  13. [ LogLevel.Info ] = ConsoleColor.White,
  14. [ LogLevel.Warning ] = ConsoleColor.Yellow,
  15. [ LogLevel.Error ] = ConsoleColor.Red
  16. };
  17. }
  18. public ConsoleLogTarget()
  19. {
  20. _formatter = new DefaultLogFormatter();
  21. }
  22. public void Log(object sender, LogEventArgs args)
  23. {
  24. if (_logColors.TryGetValue(args.Level, out ConsoleColor color))
  25. {
  26. Console.ForegroundColor = color;
  27. Console.WriteLine(_formatter.Format(args));
  28. Console.ResetColor();
  29. }
  30. else
  31. {
  32. Console.WriteLine(_formatter.Format(args));
  33. }
  34. }
  35. public void Dispose()
  36. {
  37. Console.ResetColor();
  38. }
  39. }
  40. }