forked from NtsFranz/Spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cs
363 lines (310 loc) · 8.09 KB
/
Logger.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Spark;
/// <summary>
/// ✍ Logs any data to a file or remotely
/// </summary>
public class Logger
{
public static bool ENABLE_LOGGER = true;
public static string logFolder = "Log";
private const string fileExtension = ".tsv";
private const string delimiter = "\t";
private const string dateFormat = "yyyy/MM/dd HH:mm:ss.fff";
private const string newLineChar = "\n";
private const string passwordField = "password";
public static string folder = "logs";
public static List<string> subFolders = new List<string>();
/// <summary>
/// How many lines to wait before actually logging
/// </summary>
public static int lineLogInterval = 0;
private static int numLinesLogged;
public static bool usePerDeviceFolder = true;
public static bool usePerLaunchFolder = true;
public static bool enableLoggingLocal = true;
public static bool useFullFileCache = true;
public static List<string> fullFileCache = new List<string>();
public static StringBuilder unusedFileCache = new StringBuilder();
private static readonly HttpClient client = new HttpClient();
public enum LogType
{
/// <summary>
/// Just print to terminal and nothing else.
/// </summary>
Info,
/// <summary>
/// Print to custom file and terminal.
/// </summary>
File,
/// <summary>
/// Print to terminal and error file.
/// </summary>
Error
}
/// <summary>
/// Dictionary of filename and list of lines that haven't been logged yet
/// </summary>
private static Dictionary<string, List<string>> dataToLog = new Dictionary<string, List<string>>();
public static readonly object dataToLogLock = new object();
public static bool initialized;
private static string macAddrVal;
public static string DeviceId =>
macAddrVal ??= (
from nic in NetworkInterface.GetAllNetworkInterfaces()
where nic.OperationalStatus == OperationalStatus.Up
select nic.GetPhysicalAddress().ToString()
).FirstOrDefault();
/// <summary>
/// Starts the logging thread.
/// </summary>
public static void Init()
{
if (!initialized)
{
initialized = true;
Thread loggerThread = new Thread(LoggerThread);
loggerThread.Start();
}
}
/// <summary>
/// Periodically writes the data we've collected so far
/// </summary>
public static void LoggerThread()
{
do
{
ActuallyLog();
Thread.Sleep(1000);
} while (Program.running);
}
/// <summary>
/// 🧵 Writes a message to the console, file, or upload
/// </summary>
/// <param name="logType"></param>
/// <param name="elements">The columns in the row</param>
public static void LogRow(LogType logType, params string[] elements)
{
if (!ENABLE_LOGGER) return;
switch (logType)
{
case LogType.Info:
if (elements.Length == 1)
{
if (SparkSettings.instance.showDatabaseLog || !elements[0].Contains("[DB]"))
{
Console.WriteLine(elements[0]);
}
}
else
Console.WriteLine("LogType info with more or less than 1 argument.");
break;
case LogType.File:
if (elements.Length >= 2)
{
List<string> list = new List<string>(elements);
list.RemoveAt(0);
LogRow(elements[0], list);
Console.WriteLine(list[0]);
if (useFullFileCache)
{
lock (Program.logOutputWriteLock)
{
fullFileCache.Add(list[0]);
unusedFileCache.AppendLine(list[0]);
// get rid of really old stuff
if (fullFileCache.Count > 5000)
{
fullFileCache.RemoveRange(0, 1000);
}
}
}
try
{
Program.EventLog?.Invoke(list[0]);
}
catch (Exception e)
{
LogRow(LogType.Error, "Error processing EventLog event\n" + e);
}
}
else
{
Console.WriteLine("LogType File with fewer than 2 parameters. Must specify a file name and data.");
}
break;
case LogType.Error:
if (elements.Length > 0)
{
Console.WriteLine(elements[0]);
LogRow("error", elements);
}
break;
}
}
/// <summary>
/// Logs data to a file
/// </summary>
/// <param name="fileName">e.g. "movement"</param>
/// <param name="data">List of columns to log</param>
private static void LogRow(string fileName, IEnumerable<string> data)
{
if (!ENABLE_LOGGER) return;
// add the data to the dictionary
try
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append(DateTime.Now.ToString(dateFormat));
strBuilder.Append(delimiter);
strBuilder.Append(DeviceId);
strBuilder.Append(delimiter);
strBuilder.Append(Program.AppVersionString());
strBuilder.Append(delimiter);
foreach (string elem in data)
{
string newElem = elem;
if (elem.Contains(delimiter))
{
newElem = elem.Replace(delimiter, "@");
LogRow(LogType.Error, "Data contains delimiter: " + newElem);
}
strBuilder.Append(newElem);
strBuilder.Append(delimiter);
}
lock (dataToLogLock)
{
if (!dataToLog.ContainsKey(fileName))
{
dataToLog.Add(fileName, new List<string>());
}
dataToLog[fileName].Add(strBuilder.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
numLinesLogged++;
}
/// <summary>
/// Just references the LogRow overload above
/// </summary>
/// <param name="fileName"></param>
/// <param name="elements"></param>
private static void LogRow(string fileName, params string[] elements)
{
if (!ENABLE_LOGGER) return;
if (elements is null)
{
return;
}
LogRow(fileName, new List<string>(elements));
}
public static void Error(string error)
{
LogRow(LogType.Error, error);
}
private static void ActuallyLog()
{
if (!ENABLE_LOGGER) return;
StringBuilder allOutputData = new StringBuilder();
try
{
lock (dataToLogLock)
{
foreach (var fileName in dataToLog.Keys)
{
StreamWriter fileWriter = null;
if (enableLoggingLocal)
{
string filePath, directoryPath;
// combine with some other data path, such as AppData
directoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Spark", logFolder);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
filePath = Path.Combine(directoryPath, fileName + fileExtension);
// create writer
try
{
fileWriter = new StreamWriter(filePath, true);
}
catch (IOException e)
{
LogRow(LogType.Error, $"Can't open log file for writing\n{e}");
continue;
}
}
allOutputData.Clear();
// actually log data
try
{
foreach (var row in dataToLog[fileName])
{
if (enableLoggingLocal)
{
fileWriter.WriteLine(row);
}
if (SparkSettings.instance.logToServer)
{
allOutputData.Append(row);
allOutputData.Append(newLineChar);
}
}
dataToLog[fileName].Clear();
if (SparkSettings.instance.logToServer)
{
Upload(fileName + fileExtension, allOutputData.ToString(), folder);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
if (enableLoggingLocal)
{
fileWriter.Close();
}
}
numLinesLogged = 0;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static async void Upload(string name, string data, string appName)
{
if (!ENABLE_LOGGER) return;
return; // TODO This was uploading *super* often for some reason
var values = new Dictionary<string, string>
{
{ passwordField, SecretKeys.webLogPassword },
{ "file", name },
{ "data", data },
{ "folder", appName }
};
try
{
var content = new FormUrlEncodedContent(values);
// var response = await client.PostAsync(SecretKeys.webLogURL, content);
// if (!response.IsSuccessStatusCode)
// {
// Console.WriteLine(response.Content.ToString());
// }
}
catch (Exception e)
{
Console.WriteLine("Cound not upload log to the server: " + data.Substring(0, data.Length > 1000 ? 1000 : data.Length) + "\n Error: " + e);
}
}
}