This repository was archived by the owner on Oct 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptLogger.cs
More file actions
176 lines (137 loc) · 3.93 KB
/
Copy pathScriptLogger.cs
File metadata and controls
176 lines (137 loc) · 3.93 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
using System;
using System.Collections.Generic;
using System.IO;
using Cysharp.Threading.Tasks;
using EasyButtons;
using TMPro;
using UnityEngine;
namespace Argyle.UnclesToolkit
{
public class ScriptLogger : ArgyleComponent
{
public TextMeshProUGUI _textUI;
public bool _displayStackTrace;
public bool _displayLogs;
public bool _displayWarnings;
public bool _displayErrors;
public bool _clearOnEnable;
public bool _isPaused;
public bool _isSaved;
public bool _isDisplayed;
public bool _logAll = false;
public List<string> Tags = new List<string>();
private string _fullString;
private string _newContent = string.Empty;
private string _fileName = Path.Combine("Logs",$"ScriptLog-{DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day} " +
$"{DateTime.Now.Hour}-{DateTime.Now.Minute}-{DateTime.Now.Second}.txt");
private SecureStore _store;
#region ==== Monobehavior ====------------------
// Start is called before the first frame update
void Awake()
{
_textUI.text = "";
_store = new SecureStore();
}
private void OnEnable()
{
if(_clearOnEnable)
Clear();
Application.logMessageReceived += LogHandler;
}
private void OnDisable()
{
Application.logMessageReceived -= LogHandler;
}
private void Update()
{
if (_newContent.Length > 0)
{
_fullString += _newContent;
if(_isDisplayed)
Display();
if (_isSaved)
SaveToFile(_newContent);
_newContent = String.Empty;
}
}
#endregion -----------------/Monobehavior ====
public void LogHandler(string message, string stackTrace, LogType logtype)
{
if(_isPaused)
return;
bool isLogged = false;
foreach (var tag in Tags)
if (message.Contains(tag))
isLogged = true;
if(!_logAll && !isLogged)
return;
string displayString = "";
string stackTraceString = "...stack trace: " + stackTrace;
//Add a prefix to differentiate log types.
switch (logtype)
{
case LogType.Error:
if (!_displayErrors) return;
displayString = "! ERROR: ";
break;
case LogType.Assert:
if (!_displayLogs) return;
displayString = "Assert: ";
break;
case LogType.Warning:
if (!_displayWarnings) return;
displayString = "WARNING: ";
break;
case LogType.Log:
if(!_displayLogs) return;
displayString = "Log: ";
break;
case LogType.Exception:
if (!_displayErrors) return;
displayString = "!!! EXCEPTION: ";
break;
default:
break;
}
displayString += message + Environment.NewLine;
if (_displayStackTrace)
{
displayString += stackTraceString;
}
UpdateLog(displayString);
}
public void UpdateLog(string logString)
{
_newContent += logString + Environment.NewLine;
}
public void Display()
{
if(_textUI == null)
return;
_textUI.text = _fullString.Length > 10000 ? _fullString.Substring(0, 10000) : _fullString;
}
public async void SaveToFile(string newStuff)
{
await Timing.WaitFor(() => { return _store != null; });
_store.StoreAppendAsync(newStuff, _fileName);
}
public void Clear()
{
_fullString = "";
}
#region ==== Controls ====------------------
public void ToggleLog() => ToggleLog(!_displayLogs);
public void ToggleLog(bool setTo) => _displayLogs = setTo;
public void ToggleWarning() => ToggleWarning(!_displayWarnings);
public void ToggleWarning(bool setTo) => _displayWarnings = setTo;
public void ToggleError() => ToggleError(!_displayErrors);
public void ToggleError(bool setTo) => _displayErrors = setTo;
public void TogglePause() => TogglePause(!_isPaused);
public void TogglePause(bool setTo) => _isPaused = setTo;
public void ToggleLogAll() => ToggleLogAll(!_logAll);
public void ToggleLogAll(bool setTo) => _logAll = setTo;
#endregion -----------------/Controls ====
[Button]
public void Ping() => Debug.Log("Ping");
}
}