-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cs
94 lines (80 loc) · 3.38 KB
/
Logger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Runtime.InteropServices;
namespace HR
{
public class Logger {
protected class AnsiInjector {
private const int STD_OUTPUT_HANDLE = -11;
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
private const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
[DllImport("kernel32.dll")]
private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
public static extern uint GetLastError();
public static void Inject () {
var iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleMode(iStdOut, out uint outConsoleMode)) {
Console.WriteLine("Failed to get output console mode");
Console.ReadKey();
return;
}
outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
if (!SetConsoleMode(iStdOut, outConsoleMode)) {
Console.WriteLine($"Failed to set output console mode. Error code: {GetLastError()}");
Console.ReadKey();
return;
}
}
}
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AllocConsole();
public static void EnableANSI () {
AnsiInjector.Inject();
}
protected static bool _debug = false;
public static void EnableDebug (bool enable) {
_debug = enable;
}
protected static bool stylize = true;
public static void AllowStylization(bool allow) {
stylize = allow;
}
public static void DebugInfo (string message) {
if (!_debug) return;
Console.WriteLine($"[{DateTime.Now.ToString("hh:mm:ss")}][DInf]→ {message}");
}
public static void DebugWarn (string message) {
if (!_debug) return;
Console.WriteLine($"[{DateTime.Now.ToString("hh:mm:ss")}][DWrn]→ {message}");
}
public static void Info (string message, ConsoleColor color = ConsoleColor.Gray) {
Console.ForegroundColor = color;
Console.WriteLine ($"[{DateTime.Now.ToString("hh:mm:ss")}][Info]→ {message}");
Console.ResetColor();
}
public static void Warn (string message) {
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"[{DateTime.Now.ToString("hh:mm:ss")}][Warn]→ {message}");
Console.ResetColor();
}
public static void Error (string message) {
if (!Program.IsConsole)
{
MessageBox.Show(
message,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[{DateTime.Now.ToString("hh:mm:ss")}][Fail]→ {message}");
Console.ResetColor();
}
}
}