This repository was archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverwatchLog.cs
147 lines (133 loc) · 3.99 KB
/
OverwatchLog.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// OverwatchLog
// Shepherd0619
// An enhanced logger for Unity.
using System;
using System.Text;
public enum LogColor
{
NONE = 0,
BLUE,
YELLOW,
RED,
GREEN,
AQUA,
WHITE,
}
public enum OverwatchLogType
{
Log = 1,
Warning,
Error,
Assert,
TrueLog,
}
public class OverwatchLog
{
#if UNITY_EDITOR
public static OverwatchLogType MinLogType = OverwatchLogType.Log;
#else
public static OverwatchLogType MinLogType = OverwatchLogType.Log;
#endif
// 检查日志类型是否需要显示
private static bool IsShow(OverwatchLogType type)
{
int ret = (int)type - (int)MinLogType;
if (ret < 0) return false;
return true;
}
// 根据颜色枚举返回对应的颜色标签字符串
private static string ColorString(LogColor color)
{
string ret = null;
switch (color)
{
case LogColor.BLUE:
ret = "<color=blue>";
break;
case LogColor.YELLOW:
ret = "<color=yellow>";
break;
case LogColor.RED:
ret = "<color=red>";
break;
case LogColor.GREEN:
ret = "<color=green>";
break;
case LogColor.AQUA:
ret = "<color=aqua>";
break;
case LogColor.WHITE:
ret = "<color=white>";
break;
}
return ret;
}
// 内部日志打印方法
static void LogInner(OverwatchLogType type, string msg, LogColor color, UnityEngine.Object context)
{
if (!IsShow(type)) return;
var stringBuilder = new StringBuilder();
stringBuilder.Append(msg ?? "Null");
#if UNITY_EDITOR
string colorString = ColorString(color);
if (colorString != null)
{
stringBuilder.Insert(0, colorString);
stringBuilder.Append("</color>");
}
#endif
stringBuilder.Insert(0,
string.Format("Unity {2}<{0} - {1:HH:mm:ss.fff}> ",
System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now,
type.ToString()));
var message = stringBuilder;
switch (type)
{
case OverwatchLogType.TrueLog:
case OverwatchLogType.Log:
UnityEngine.Debug.Log(message, context);
break;
case OverwatchLogType.Error:
UnityEngine.Debug.LogError(message, context);
break;
case OverwatchLogType.Warning:
UnityEngine.Debug.LogWarning(message, context);
break;
case OverwatchLogType.Assert:
UnityEngine.Debug.LogAssertion(message, context);
break;
default:
break;
}
}
// 打印普通日志
public static void Log(string log, UnityEngine.Object context = null)
{
if (!IsShow(OverwatchLogType.Log)) return;
LogInner(OverwatchLogType.Log, log, LogColor.NONE, context);
}
// 打印警告日志
public static void Warn(string log, UnityEngine.Object context = null)
{
if (!IsShow(OverwatchLogType.Warning)) return;
LogInner(OverwatchLogType.Warning, log, LogColor.NONE, context);
}
// 打印错误日志
public static void Error(string log, UnityEngine.Object context = null)
{
if (!IsShow(OverwatchLogType.Error)) return;
LogInner(OverwatchLogType.Error, log, LogColor.NONE, context);
}
// 打印异常日志
public static void LogException(Exception ex, UnityEngine.Object context = null)
{
if (!IsShow(OverwatchLogType.Error)) return;
LogInner(OverwatchLogType.Error, ex.ToString(), LogColor.NONE, context);
}
// 打印断言日志
public static void Assert(bool condition, string log = "")
{
if (!IsShow(OverwatchLogType.Assert)) return;
LogInner(OverwatchLogType.Assert, log, LogColor.NONE, null);
}
}