|
|
@ -1,4 +1,5 @@ |
|
|
|
using System; |
|
|
|
using System.Runtime.InteropServices; |
|
|
|
|
|
|
|
namespace IPA.Logging.Printers |
|
|
|
{ |
|
|
@ -7,7 +8,7 @@ namespace IPA.Logging.Printers |
|
|
|
/// </summary>
|
|
|
|
public class ColoredConsolePrinter : LogPrinter |
|
|
|
{ |
|
|
|
Logger.LogLevel filter = Logger.LogLevel.All; |
|
|
|
private Logger.LogLevel filter = Logger.LogLevel.All; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A filter for this specific printer.
|
|
|
@ -19,7 +20,7 @@ namespace IPA.Logging.Printers |
|
|
|
public ConsoleColor Color { get; set; } = Console.ForegroundColor; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Prints an entry to the associated file.
|
|
|
|
/// Prints an entry to the console window.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="level">the <see cref="Logger.Level"/> of the message</param>
|
|
|
|
/// <param name="time">the <see cref="DateTime"/> the message was recorded at</param>
|
|
|
@ -28,10 +29,79 @@ namespace IPA.Logging.Printers |
|
|
|
public override void Print(Logger.Level level, DateTime time, string logName, string message) |
|
|
|
{ |
|
|
|
if (((byte)level & (byte)StandardLogger.PrintFilter) == 0) return; |
|
|
|
Console.ForegroundColor = Color; |
|
|
|
EnsureDefaultsPopulated(WinConsole.OutHandle); |
|
|
|
SetColor(Color, WinConsole.OutHandle); |
|
|
|
foreach (var line in message.Split(new[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) |
|
|
|
Console.WriteLine(Logger.LogFormat, line, logName, time, level.ToString().ToUpper()); |
|
|
|
Console.ResetColor(); |
|
|
|
WinConsole.ConOut.WriteLine(Logger.LogFormat, line, logName, time, level.ToString().ToUpper()); |
|
|
|
ResetColor(WinConsole.OutHandle); |
|
|
|
} |
|
|
|
|
|
|
|
private static bool _haveReadDefaultColors; |
|
|
|
private static short _defaultColors; |
|
|
|
|
|
|
|
private void EnsureDefaultsPopulated(IntPtr handle, bool force = false) |
|
|
|
{ |
|
|
|
if (!_haveReadDefaultColors | force) |
|
|
|
{ |
|
|
|
GetConsoleScreenBufferInfo(handle, out var info); |
|
|
|
_defaultColors = (short)(info.Attribute & ~15); |
|
|
|
_haveReadDefaultColors = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void ResetColor(IntPtr handle) |
|
|
|
{ |
|
|
|
GetConsoleScreenBufferInfo(handle, out var info); |
|
|
|
var otherAttrs = (short)(info.Attribute & ~15); |
|
|
|
SetConsoleTextAttribute(handle, (short)(otherAttrs | _defaultColors)); |
|
|
|
} |
|
|
|
|
|
|
|
private void SetColor(ConsoleColor col, IntPtr handle) |
|
|
|
{ |
|
|
|
GetConsoleScreenBufferInfo(handle, out var info); |
|
|
|
var attr = GetAttrForeground(info.Attribute, col); |
|
|
|
SetConsoleTextAttribute(handle, attr); |
|
|
|
} |
|
|
|
|
|
|
|
private static short GetAttrForeground(int attr, ConsoleColor color) |
|
|
|
{ |
|
|
|
attr &= ~15; |
|
|
|
return (short)(attr | (int)color); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ReSharper disable NotAccessedField.Local
|
|
|
|
#pragma warning disable 649
|
|
|
|
private struct Coordinate |
|
|
|
{ |
|
|
|
public short X; |
|
|
|
public short Y; |
|
|
|
} |
|
|
|
|
|
|
|
private struct SmallRect |
|
|
|
{ |
|
|
|
public short Left; |
|
|
|
public short Top; |
|
|
|
public short Right; |
|
|
|
public short Bottom; |
|
|
|
} |
|
|
|
|
|
|
|
private struct ConsoleScreenBufferInfo |
|
|
|
{ |
|
|
|
public Coordinate Size; |
|
|
|
public Coordinate CursorPosition; |
|
|
|
public short Attribute; |
|
|
|
public SmallRect Window; |
|
|
|
public Coordinate MaxWindowSize; |
|
|
|
} |
|
|
|
#pragma warning restore 649
|
|
|
|
// ReSharper restore NotAccessedField.Local
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("kernel32.dll", EntryPoint = "GetConsoleScreenBufferInfo", SetLastError = true, CharSet = CharSet.Unicode)] |
|
|
|
private static extern bool GetConsoleScreenBufferInfo(IntPtr handle, out ConsoleScreenBufferInfo info); |
|
|
|
|
|
|
|
[DllImport("kernel32.dll", EntryPoint = "SetConsoleTextAttribute", SetLastError = true, CharSet = CharSet.Unicode)] |
|
|
|
private static extern bool SetConsoleTextAttribute(IntPtr handle, short attribute); |
|
|
|
} |
|
|
|
} |