-
Notifications
You must be signed in to change notification settings - Fork 1
/
BetterLogging.cs
195 lines (173 loc) · 6.94 KB
/
BetterLogging.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
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
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace FortySevenE
{
public class UnityLogMessage
{
public string condition;
public string stackTrace;
public LogType type;
public UnityLogMessage(string condition, string stackTrace, LogType type)
{
this.condition = condition;
this.stackTrace = stackTrace;
this.type = type;
}
}
public enum LogLevel
{
Verbose,
Info,
Warning,
Error
}
public class BetterLogging : MonoBehaviour
{
[SerializeField] private bool _keepLogFiles = true;
[SerializeField] private LogLevel _minEditorLogLevel = LogLevel.Verbose;
[SerializeField] private LogLevel _minBuildLogLevel = LogLevel.Info;
public static readonly string DateTimeParseFormat = "yyyy_MM_dd_T_HH_mm_ss_fff";
private static BetterLogging _instance;
private static object _lock = new object();
public string CurrentLogFileName { get; private set; }
public string CurrentLogPath { get; private set; }
public StreamWriter LoggingFileStream { get; private set; }
private ConcurrentQueue<UnityLogMessage> _logConcurrentQueue;
static public void Log(string message, LogLevel logLevel = LogLevel.Info,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
var minLogLevel = LogLevel.Info;
if (_instance != null)
{
minLogLevel = Application.isEditor? _instance._minEditorLogLevel: _instance._minBuildLogLevel;
}
if (!Application.isPlaying || logLevel >= minLogLevel)
{
string prefix = $"[{Path.GetFileNameWithoutExtension(sourceFilePath)}->{memberName}:{sourceLineNumber})]";
switch(logLevel)
{
case LogLevel.Verbose:
goto case LogLevel.Info;
case LogLevel.Info:
Debug.Log($"{prefix} {message}");
break;
case LogLevel.Warning:
Debug.LogWarning($"{prefix} {message}");
break;
case LogLevel.Error:
Debug.LogError($"{prefix} {message}");
break;
}
}
}
static private BetterLogging GetInstance()
{
lock (_lock)
{
if (_instance == null)
{
// Search for existing instance.
_instance = FindObjectOfType<BetterLogging>();
// Create new instance if one doesn't already exist.
if (_instance == null)
{
// Need to create a new GameObject to attach the singleton to.
var singletonObject = new GameObject();
_instance = singletonObject.AddComponent<BetterLogging>();
singletonObject.name = "BetterLogging";
Debug.Log("<b>A BetterLogging instance was added to the scene.</b>");
}
if (_instance != null)
{
if (Application.isPlaying)
{
// Make instance persistent.
DontDestroyOnLoad(_instance);
}
}
}
return _instance;
}
}
private void Awake()
{
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(_instance);
}
else
{
Debug.LogWarning($"Another instance of BetterLogging detected on GO {name}. Destroyed", gameObject);
Destroy(this);
}
if (_keepLogFiles)
{
_logConcurrentQueue = new ConcurrentQueue<UnityLogMessage>();
CurrentLogFileName = Application.productName + "_" + DateTime.Now.ToString(DateTimeParseFormat) + ".txt";
string logDirectory = Path.Combine(Application.persistentDataPath, "Logs");
if (!Directory.Exists(logDirectory))
{
Directory.CreateDirectory(logDirectory);
}
CurrentLogPath = Path.Combine(logDirectory, CurrentLogFileName);
if (!File.Exists(CurrentLogPath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(CurrentLogPath))
{
sw.WriteLine("App Name: " + Application.productName);
sw.WriteLine("App Version: " + Application.version);
sw.WriteLine("Unity Version: " + Application.unityVersion);
sw.WriteLine("CPU: " + SystemInfo.processorType);
sw.WriteLine("GPU: " + SystemInfo.graphicsDeviceName);
sw.WriteLine("Memory: " + SystemInfo.systemMemorySize);
sw.WriteLine(Environment.NewLine);
}
}
}
}
private void OnEnable()
{
if (_keepLogFiles)
{
Application.logMessageReceivedThreaded += Application_logMessageReceivedThreaded;
}
}
private void OnDisable()
{
if (_keepLogFiles)
{
Application.logMessageReceivedThreaded -= Application_logMessageReceivedThreaded;
}
}
private void LateUpdate()
{
if (!_keepLogFiles)
{
return;
}
if (_logConcurrentQueue.Count > 0)
{
using (LoggingFileStream = File.AppendText(CurrentLogPath))
{
UnityLogMessage logMessage = default;
while (_logConcurrentQueue.TryDequeue(out logMessage))
{
LoggingFileStream.Write($"[{DateTime.Now.ToString(DateTimeParseFormat)}] " + logMessage.type.ToString() + Environment.NewLine + logMessage.condition + Environment.NewLine + logMessage.stackTrace + Environment.NewLine);
}
}
}
}
private void Application_logMessageReceivedThreaded(string condition, string stackTrace, LogType type)
{
//Add the log from different threads to the main thread, so it can be write to the files without the locking issue
_logConcurrentQueue.Enqueue(new UnityLogMessage(condition, stackTrace, type));
}
}
}