forked from loongly/PureScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugHelper.cs
More file actions
241 lines (214 loc) · 7.3 KB
/
Copy pathDebugHelper.cs
File metadata and controls
241 lines (214 loc) · 7.3 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using UnityEngine;
using System;
using System.IO;
using System.Text;
public class DebugHelper
{
public const string DEBUG_TAG_KEY = "debug_tag";
private static string sCurTag;
static StreamWriter LogWriter;
// [Conditional("UNITY_EDITOR"), Conditional("DEBUG"), Conditional("Debug")]
public static void InitTag(string tag = null)
{
if(tag == null)
tag = PlayerPrefs.GetString(DEBUG_TAG_KEY, "");
sCurTag = tag;
}
private static bool CheckTag(string tag)
{
return string.IsNullOrEmpty(sCurTag) || sCurTag.Equals(tag);
}
public static void InitLog(bool debug)
{
Debug.LogError("init log: " + (LogWriter == null && debug));
if (LogWriter == null && debug)
{//debug模式下开启一个实时文件写入
string date = DateTime.Now.ToString("yyyy-MM-dd");
string filePath = GetLogPath() + "../unity." + date + ".log";
Debug.LogError("log path: " + filePath);
LogWriter = new StreamWriter(filePath, true);
LogWriter.WriteLine(date + " - " + DateTime.Now.TimeOfDay);
}
Application.logMessageReceivedThreaded += OnLog;
System.AppDomain.CurrentDomain.UnhandledException += new System.UnhandledExceptionEventHandler(OnDebugLogCallbackHandler);
}
static private void OnDebugLogCallbackHandler(object sender, UnhandledExceptionEventArgs e)
{
Debug.LogError("## Unhandled Exception: " + e.ExceptionObject);
}
static void OnLog(string condition, string stackTrace, LogType type)
{
if (LogWriter != null)
{//debug模式下开启一个实时文件写入
LogToFile(condition);
}
}
// [Conditional("UNITY_EDITOR"), Conditional("DEBUG"), Conditional("Debug")]
public static void LogMsg(string tag, object msg)
{
if (CheckTag(tag))
UnityEngine.Debug.Log(tag +" | "+ msg); //Time.frameCount +" "+
//if (SaveToFile && !string.IsNullOrEmpty(sCurTag))
// LogToFile(sCurTag, msg.ToString());
}
/// <summary>
/// 这个用作记录流程/步骤,单独一个写文件,流程中断时可以用来查看中断位置
/// </summary>
/// <param name="tag"></param>
/// <param name="msg"></param>
// [Conditional("UNITY_EDITOR"), Conditional("DEBUG"), Conditional("Debug")]
public static void LogStep(string tag, object msg)
{
tag = "<STEP-" + tag + "> " ;
LogWarning(tag, msg);
// LogToFile("STEP.log", msgStr);
}
// [Conditional("UNITY_EDITOR"), Conditional("DEBUG"), Conditional("Debug")]
public static void LogWarning(string tag, object message)
{
if (CheckTag(tag))
UnityEngine.Debug.LogWarning(tag + " | " + message);
}
//[Conditional("UNITY_EDITOR"), Conditional("DEBUG"), Conditional("Debug")]
public static void LogError(string tag,object message)
{
// if (CheckTag(tag))
UnityEngine.Debug.LogError(Time.frameCount + " " + tag + " | " + message);
}
#region old interface
// [Conditional("UNITY_EDITOR"), Conditional("DEBUG"), Conditional("Debug")]
public static void LogError(object message)
{
// if (CheckTag(""))
UnityEngine.Debug.LogError(message);
}
// [Conditional("UNITY_EDITOR"), Conditional("DEBUG"), Conditional("Debug")]
public static void LogWarning(object message)
{
if (CheckTag(""))
UnityEngine.Debug.LogWarning(message.ToString());
}
//[Conditional("UNITY_EDITOR"), Conditional("DEBUG"), Conditional("Debug")]
public static void Log(object message)
{
if (CheckTag(""))
UnityEngine.Debug.Log(message.ToString());
}
#endregion
public static void Break(string tag = null)
{
if (CheckTag(tag))
UnityEngine.Debug.Break();
}
public static void ClearDeveloperConsole()
{
UnityEngine.Debug.ClearDeveloperConsole();
}
public static void DebugBreak(string tag = null)
{
if (CheckTag(tag))
UnityEngine.Debug.DebugBreak();
}
//
// Summary:
// Draws a line between specified start and end points.
public static void DrawLine(Vector3 start, Vector3 end)
{
UnityEngine.Debug.DrawLine(start, end);
}
//
// Summary:
// Draws a line between specified start and end points.
public static void DrawLine(Vector3 start, Vector3 end, Color color)
{
UnityEngine.Debug.DrawLine(start, end, color);
}
//
// Summary:
// Draws a line between specified start and end points.
public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration)
{
UnityEngine.Debug.DrawLine(start, end, color, duration);
}
//
// Summary:
// Draws a line between specified start and end points.
public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration, bool depthTest)
{
UnityEngine.Debug.DrawLine(start, end, color, duration, depthTest);
}
//
// Summary:
// Draws a line from start to start + dir in world coordinates.
public static void DrawRay(Vector3 start, Vector3 dir)
{
UnityEngine.Debug.DrawRay(start, dir);
}
//
// Summary:
// Draws a line from start to start + dir in world coordinates.
public static void DrawRay(Vector3 start, Vector3 dir, Color color)
{
UnityEngine.Debug.DrawRay(start, dir, color);
}
//
// Summary:
// Draws a line from start to start + dir in world coordinates.
public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration)
{
UnityEngine.Debug.DrawRay(start, dir, color, duration);
}
//
// Summary:
// Draws a line from start to start + dir in world coordinates.
public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration, bool depthTest)
{
UnityEngine.Debug.DrawRay(start, dir, color, duration, depthTest);
}
static string CachePath = null;
public static string GetLogPath()
{
if (CachePath == null)
{
#if UNITY_EDITOR
CachePath = "D:/log/unity/";
#else
CachePath = Application.persistentDataPath +"/log/";
#endif
if (!Directory.Exists(CachePath))
Directory.CreateDirectory(CachePath);
}
return CachePath;
}
/*static FileStream sLogSteam;
static void initSteam()
{
string path = GetCachePath() + "log.txt";
sLogSteam = File.Open(path);
}
public static void toFile(string str)
{
if (sLogSteam == null)
initSteam();
}*/
static void LogToFile(string str)
{
lock (LogWriter)
{
LogWriter.WriteLine(DateTime.Now.TimeOfDay + " | " + str);
LogWriter.Flush();
}
}
//[Conditional("UNITY_EDITOR"), Conditional("DEBUG"), Conditional("Debug")]
public static void SaveFile(string fileName, string str)
{
if (str == null)
str = "[null]";
string path = GetLogPath();
string curDate = System.Environment.NewLine + str;
FileStream fs = File.Open(path + fileName, FileMode.Append, FileAccess.Write);
byte[] bytes = Encoding.UTF8.GetBytes(curDate);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}
}