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.

107 lines
3.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace IPA.Logging.Printers
  4. {
  5. /// <summary>
  6. /// Prints a pretty message to the console.
  7. /// </summary>
  8. public class ColoredConsolePrinter : LogPrinter
  9. {
  10. private Logger.LogLevel filter = Logger.LogLevel.All;
  11. /// <summary>
  12. /// A filter for this specific printer.
  13. /// </summary>
  14. public override Logger.LogLevel Filter { get => filter; set => filter = value; }
  15. /// <summary>
  16. /// The color to print messages as.
  17. /// </summary>
  18. public ConsoleColor Color { get; set; } = Console.ForegroundColor;
  19. /// <summary>
  20. /// Prints an entry to the console window.
  21. /// </summary>
  22. /// <param name="level">the <see cref="Logger.Level"/> of the message</param>
  23. /// <param name="time">the <see cref="DateTime"/> the message was recorded at</param>
  24. /// <param name="logName">the name of the log that sent the message</param>
  25. /// <param name="message">the message to print</param>
  26. public override void Print(Logger.Level level, DateTime time, string logName, string message)
  27. {
  28. if (((byte)level & (byte)StandardLogger.PrintFilter) == 0) return;
  29. EnsureDefaultsPopulated(WinConsole.OutHandle);
  30. SetColor(Color, WinConsole.OutHandle);
  31. foreach (var line in message.Split(new[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
  32. WinConsole.ConOut.WriteLine(Logger.LogFormat, line, logName, time, level.ToString().ToUpper());
  33. ResetColor(WinConsole.OutHandle);
  34. }
  35. private static bool _haveReadDefaultColors;
  36. private static short _defaultColors;
  37. private void EnsureDefaultsPopulated(IntPtr handle, bool force = false)
  38. {
  39. if (!_haveReadDefaultColors | force)
  40. {
  41. GetConsoleScreenBufferInfo(handle, out var info);
  42. _defaultColors = (short)(info.Attribute & ~15);
  43. _haveReadDefaultColors = true;
  44. }
  45. }
  46. private void ResetColor(IntPtr handle)
  47. {
  48. GetConsoleScreenBufferInfo(handle, out var info);
  49. var otherAttrs = (short)(info.Attribute & ~15);
  50. SetConsoleTextAttribute(handle, (short)(otherAttrs | _defaultColors));
  51. }
  52. private void SetColor(ConsoleColor col, IntPtr handle)
  53. {
  54. GetConsoleScreenBufferInfo(handle, out var info);
  55. var attr = GetAttrForeground(info.Attribute, col);
  56. SetConsoleTextAttribute(handle, attr);
  57. }
  58. private static short GetAttrForeground(int attr, ConsoleColor color)
  59. {
  60. attr &= ~15;
  61. return (short)(attr | (int)color);
  62. }
  63. // ReSharper disable NotAccessedField.Local
  64. #pragma warning disable 649
  65. private struct Coordinate
  66. {
  67. public short X;
  68. public short Y;
  69. }
  70. private struct SmallRect
  71. {
  72. public short Left;
  73. public short Top;
  74. public short Right;
  75. public short Bottom;
  76. }
  77. private struct ConsoleScreenBufferInfo
  78. {
  79. public Coordinate Size;
  80. public Coordinate CursorPosition;
  81. public short Attribute;
  82. public SmallRect Window;
  83. public Coordinate MaxWindowSize;
  84. }
  85. #pragma warning restore 649
  86. // ReSharper restore NotAccessedField.Local
  87. [DllImport("kernel32.dll", EntryPoint = "GetConsoleScreenBufferInfo", SetLastError = true, CharSet = CharSet.Unicode)]
  88. private static extern bool GetConsoleScreenBufferInfo(IntPtr handle, out ConsoleScreenBufferInfo info);
  89. [DllImport("kernel32.dll", EntryPoint = "SetConsoleTextAttribute", SetLastError = true, CharSet = CharSet.Unicode)]
  90. private static extern bool SetConsoleTextAttribute(IntPtr handle, short attribute);
  91. }
  92. }