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.

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