forked from Xkein/YRDynamicPatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
127 lines (106 loc) · 4.16 KB
/
Logger.cs
File metadata and controls
127 lines (106 loc) · 4.16 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DynamicPatcher
{
/// <summary>Represents the log behavior for DynamicPatcher.</summary>
public class Logger
{
/// <summary>Represents the method that will handle log message.</summary>
public delegate void WriteLineDelegate(string str);
/// <summary>Invoked when log the message.</summary>
static public WriteLineDelegate WriteLine { get; set; }
/// <summary>Write format string to logger.</summary>
static public void Log(string format, params object[] args)
{
string str = string.Format(format, args);
Logger.Log(str);
}
/// <summary>Write string to logger.</summary>
static public void Log(string str)
{
WriteLine.Invoke(str);
}
/// <summary>Write object to logger.</summary>
static public void Log(object obj)
{
Logger.Log(obj.ToString());
}
/// <summary>Get if PrintException has invoked.</summary>
public static bool HasException { get; set; } = false;
/// <summary>Print exception and its InnerException recursively.</summary>
public static void PrintException(Exception e)
{
HasException = true;
PrintExceptionBase(e);
Logger.Log("");
}
private static void PrintExceptionBase(Exception e)
{
Logger.LogError("{0} info: ", e.GetType().FullName);
Logger.LogError("Message: " + e.Message);
Logger.LogError("Source: " + e.Source);
Logger.LogError("TargetSite.Name: " + e.TargetSite?.Name);
Logger.LogError("Stacktrace: " + e.StackTrace);
if (e.InnerException != null)
{
PrintException(e.InnerException);
}
}
private static object color_locker = new object();
/// <summary>Write string to logger with color.</summary>
public static void LogWithColor(string str, ConsoleColor color)
{
LogWithColor(str, color, Console.BackgroundColor);
}
/// <summary>Write string to logger with ForegroundColor and BackgroundColor.</summary>
public static void LogWithColor(string str, ConsoleColor fgColor, ConsoleColor bgColor)
{
lock (color_locker)
{
ConsoleColor originFgColor = Console.ForegroundColor;
ConsoleColor originBgColor = Console.BackgroundColor;
Console.ForegroundColor = fgColor;
Console.BackgroundColor = bgColor;
Logger.Log(str);
Console.ForegroundColor = originFgColor;
Console.BackgroundColor = originBgColor;
}
}
/// <summary>Write format string to logger with error state.</summary>
static public void LogError(string format, params object[] args)
{
string str = string.Format(format, args);
Logger.LogError(str);
}
/// <summary>Write string to logger with error state.</summary>
static public void LogError(string str)
{
Logger.LogWithColor("[Error] " + str, ConsoleColor.Red);
}
/// <summary>Write object to logger with error state.</summary>
static public void LogError(object obj)
{
Logger.LogError(obj.ToString());
}
/// <summary>Write format string to logger with warning state.</summary>
static public void LogWarning(string format, params object[] args)
{
string str = string.Format(format, args);
Logger.LogWarning(str);
}
/// <summary>Write string to logger with warning state.</summary>
static public void LogWarning(string str)
{
Logger.LogWithColor("[Warning] " + str, ConsoleColor.Yellow);
}
/// <summary>Write object to logger with warning state.</summary>
static public void LogWarning(object obj)
{
Logger.LogWarning(obj.ToString());
}
}
}