You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using IPA.Logging;
  8. namespace IPA.Logging.Printers
  9. {
  10. /// <summary>
  11. /// Prints a pretty message to the console.
  12. /// </summary>
  13. public class ColoredConsolePrinter : LogPrinter
  14. {
  15. Logger.LogLevel filter = Logger.LogLevel.All;
  16. /// <summary>
  17. /// A filter for this specific printer.
  18. /// </summary>
  19. public override Logger.LogLevel Filter { get => filter; set => filter = value; }
  20. /// <summary>
  21. /// The color to print messages as.
  22. /// </summary>
  23. public ConsoleColor Color { get; set; } = Console.ForegroundColor;
  24. /// <summary>
  25. /// Prints an entry to the associated file.
  26. /// </summary>
  27. /// <param name="level">the <see cref="Logger.Level"/> of the message</param>
  28. /// <param name="time">the <see cref="DateTime"/> the message was recorded at</param>
  29. /// <param name="logName">the name of the log that sent the message</param>
  30. /// <param name="message">the message to print</param>
  31. public override void Print(Logger.Level level, DateTime time, string logName, string message)
  32. {
  33. if (((byte)level & (byte)StandardLogger.PrintFilter) == 0) return;
  34. Console.ForegroundColor = Color;
  35. foreach (var line in message.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
  36. Console.WriteLine(string.Format(Logger.LogFormat, line, logName, time, level.ToString().ToUpper()));
  37. Console.ResetColor();
  38. }
  39. }
  40. }