forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogWatcher.cs
614 lines (515 loc) · 20.9 KB
/
LogWatcher.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// Copyright(c) 2019 pypy. All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
namespace VRCX
{
public class LogWatcher
{
private class LogContext
{
public long Length;
public long Position;
public string RecentWorldName;
public string LastVideoURL;
public bool ShaderKeywordsLimitReached = false;
}
public static readonly LogWatcher Instance;
private readonly DirectoryInfo m_LogDirectoryInfo;
private readonly Dictionary<string, LogContext> m_LogContextMap; // <FileName, LogContext>
private readonly ReaderWriterLockSlim m_LogListLock;
private readonly List<string[]> m_LogList;
private Thread m_Thread;
private bool m_ResetLog;
// NOTE
// FileSystemWatcher() is unreliable
static LogWatcher()
{
Instance = new LogWatcher();
}
public LogWatcher()
{
var logPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"Low\VRChat\VRChat";
m_LogDirectoryInfo = new DirectoryInfo(logPath);
m_LogContextMap = new Dictionary<string, LogContext>();
m_LogListLock = new ReaderWriterLockSlim();
m_LogList = new List<string[]>();
m_Thread = new Thread(ThreadLoop)
{
IsBackground = true
};
}
internal void Init()
{
m_Thread.Start();
}
internal void Exit()
{
var thread = m_Thread;
m_Thread = null;
thread.Interrupt();
thread.Join();
}
private void ThreadLoop()
{
while (m_Thread != null)
{
Update();
try
{
Thread.Sleep(1000);
}
catch
{
// ThreadInterruptedException
}
}
}
private void Update()
{
if (m_ResetLog == true)
{
m_ResetLog = false;
m_LogContextMap.Clear();
m_LogListLock.EnterWriteLock();
try
{
m_LogList.Clear();
}
finally
{
m_LogListLock.ExitWriteLock();
}
}
var deletedNameSet = new HashSet<string>(m_LogContextMap.Keys);
m_LogDirectoryInfo.Refresh();
if (m_LogDirectoryInfo.Exists == true)
{
var fileInfos = m_LogDirectoryInfo.GetFiles("output_log_*.txt", SearchOption.TopDirectoryOnly);
// sort by creation time
Array.Sort(fileInfos, (a, b) => a.CreationTimeUtc.CompareTo(b.CreationTimeUtc));
var utcNow = DateTime.UtcNow;
var minLimitDateTime = utcNow.AddDays(-7d);
var minRefreshDateTime = utcNow.AddMinutes(-3d);
foreach (var fileInfo in fileInfos)
{
var lastWriteTimeUtc = fileInfo.LastWriteTimeUtc;
if (lastWriteTimeUtc < minLimitDateTime)
{
continue;
}
if (lastWriteTimeUtc >= minRefreshDateTime)
{
fileInfo.Refresh();
if (fileInfo.Exists == false)
{
continue;
}
}
if (m_LogContextMap.TryGetValue(fileInfo.Name, out LogContext logContext) == true)
{
deletedNameSet.Remove(fileInfo.Name);
}
else
{
logContext = new LogContext();
m_LogContextMap.Add(fileInfo.Name, logContext);
}
if (logContext.Length == fileInfo.Length)
{
continue;
}
logContext.Length = fileInfo.Length;
ParseLog(fileInfo, logContext);
}
}
foreach (var name in deletedNameSet)
{
m_LogContextMap.Remove(name);
}
}
private void ParseLog(FileInfo fileInfo, LogContext logContext)
{
try
{
using (var stream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 65536, FileOptions.SequentialScan))
{
stream.Position = logContext.Position;
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
while (true)
{
var line = streamReader.ReadLine();
if (line == null)
{
logContext.Position = stream.Position;
break;
}
// 2020.10.31 23:36:28 Log - [VRCFlowManagerVRC] Destination fetching: wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd
// 2021.02.03 10:18:58 Log - [DŽDŽDžDžDžDŽDŽDžDžDŽDžDžDžDžDŽDŽDŽDžDžDŽDŽDžDžDžDžDŽDžDžDžDžDŽDŽDŽDŽDŽDžDŽDžDŽDŽDŽDžDžDŽDžDžDž] Destination fetching: wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd
if (line.Length <= 36 ||
line[31] != '-')
{
continue;
}
if (line[34] == '[')
{
var offset = line.IndexOf("] ", 35, StringComparison.Ordinal);
if (offset >= 35)
{
offset += 2;
if (ParseLogOnPlayerJoinedOrLeft(fileInfo, logContext, line, offset) == true ||
ParseLogLocation(fileInfo, logContext, line, offset) == true ||
ParseLogPortalSpawn(fileInfo, logContext, line, offset) == true ||
ParseLogJoinBlocked(fileInfo, logContext, line, offset) == true ||
ParseLogAvatarPedestalChange(fileInfo, logContext, line, offset) == true ||
ParseLogVideoPlay(fileInfo, logContext, line, offset) == true ||
ParseLogVideoError(fileInfo, logContext, line, offset) == true)
{
continue;
}
}
continue;
}
if (ParseLogNotification(fileInfo, logContext, line, 34) == true ||
ParseLogShaderKeywordsLimit(fileInfo, logContext, line, 34) == true ||
ParseLogSDK2VideoPlay(fileInfo, logContext, line, 34) == true)
{
continue;
}
}
}
}
}
catch
{
}
}
private void AppendLog(string[] item)
{
m_LogListLock.EnterWriteLock();
try
{
m_LogList.Add(item);
}
finally
{
m_LogListLock.ExitWriteLock();
}
}
private string ConvertLogTimeToISO8601(string line)
{
// 2020.10.31 23:36:22
if (DateTime.TryParseExact(
line.Substring(0, 19),
"yyyy.MM.dd HH:mm:ss",
CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal,
out DateTime dt
) == false)
{
dt = DateTime.UtcNow;
}
return $"{dt:yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'}";
}
private bool ParseLogLocation(FileInfo fileInfo, LogContext logContext, string line, int offset)
{
// 2020.10.31 23:36:28 Log - [VRCFlowManagerVRC] Destination fetching: wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd
// 2020.10.31 23:36:28 Log - [VRCFlowManagerVRC] Destination set: wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd
// 2020.10.31 23:36:31 Log - [RoomManager] Entering Room: VRChat Home
// 2020.10.31 23:36:31 Log - [RoomManager] Joining wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd:67646~private(usr_4f76a584-9d4b-46f6-8209-8305eb683661)~nonce(D9298A536FEEEDDBB61633661A4BDAA09717C5178DEF865C4C09372FE12E09A6)
// 2020.10.31 23:36:31 Log - [RoomManager] Joining or Creating Room: VRChat Home
// 2020.10.31 23:36:31 Log - [RoomManager] Successfully joined room
// 2021.02.03 10:18:58 Log - [DŽDŽDžDžDžDŽDŽDžDžDŽDžDžDžDžDŽDŽDŽDžDžDŽDŽDžDžDžDžDŽDžDžDžDžDŽDŽDŽDŽDŽDžDŽDžDŽDŽDŽDžDžDŽDžDžDž] Destination fetching: wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd
if (string.Compare(line, offset, "Entering Room: ", 0, 15, StringComparison.Ordinal) == 0)
{
var worldName = line.Substring(offset + 15);
logContext.RecentWorldName = worldName;
return true;
}
if (string.Compare(line, offset, "Joining wrld_", 0, 13, StringComparison.Ordinal) == 0)
{
var location = line.Substring(offset + 8);
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"location",
location,
logContext.RecentWorldName
});
return true;
}
return false;
}
private bool ParseLogOnPlayerJoinedOrLeft(FileInfo fileInfo, LogContext logContext, string line, int offset)
{
// 2020.10.31 23:36:58 Log - [NetworkManager] OnPlayerJoined pypy
// 2020.10.31 23:36:58 Log - [Player] Initialized PlayerAPI "pypy" is local
// 2020.10.31 23:36:58 Log - [NetworkManager] OnPlayerJoined Rize♡
// 2020.10.31 23:36:58 Log - [Player] Initialized PlayerAPI "Rize♡" is remote
// 2020.11.01 00:07:01 Log - [NetworkManager] OnPlayerLeft Rize♡
// 2020.11.01 00:07:01 Log - [PlayerManager] Removed player 2 / Rize♡
// 2020.11.01 00:07:02 Log - [Player] Unregistering Rize♡
if (string.Compare(line, offset, "Initialized PlayerAPI \"", 0, 23, StringComparison.Ordinal) == 0)
{
var pos = line.LastIndexOf("\" is ");
if (pos < 0)
{
return false;
}
var userDisplayName = line.Substring(offset + 23, pos - (offset + 23));
var userType = line.Substring(pos + 5);
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"player-joined",
userDisplayName,
userType,
});
return true;
}
// fallback method
/*if (string.Compare(line, offset, "OnPlayerJoined ", 0, 15, StringComparison.Ordinal) == 0)
{
var userDisplayName = line.Substring(offset + 15);
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"player-joined",
userDisplayName
});
return true;
}*/
if (string.Compare(line, offset, "OnPlayerLeft ", 0, 13, StringComparison.Ordinal) == 0)
{
var userDisplayName = line.Substring(offset + 13);
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"player-left",
userDisplayName
});
return true;
}
return false;
}
private bool ParseLogPortalSpawn(FileInfo fileInfo, LogContext logContext, string line, int offset)
{
// 2021.04.06 11:25:45 Log - [Network Processing] RPC invoked ConfigurePortal on (Clone [1600004] Portals/PortalInternalDynamic) for Natsumi-sama
if (string.Compare(line, offset, "RPC invoked ConfigurePortal on (Clone [", 0, 39, StringComparison.Ordinal) != 0)
{
return false;
}
var pos = line.LastIndexOf("] Portals/PortalInternalDynamic) for ");
if (pos < 0)
{
return false;
}
//var portalId = line.Substring(offset + 39, pos - (offset + 39));
var data = line.Substring(pos + 37);
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"portal-spawn",
data
});
return true;
}
private bool ParseLogShaderKeywordsLimit(FileInfo fileInfo, LogContext logContext, string line, int offset)
{
// 2021.04.04 12:21:06 Error - Maximum number (256) of shader keywords exceeded, keyword _TOGGLESIMPLEBLUR_ON will be ignored.
if (logContext.ShaderKeywordsLimitReached == true)
{
return false;
}
if (string.Compare(line, offset, "Maximum number (256) of shader keywords exceeded", 0, 48, StringComparison.Ordinal) != 0)
{
return false;
}
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"event",
"Shader Keyword Limit has been reached"
});
logContext.ShaderKeywordsLimitReached = true;
return true;
}
private bool ParseLogJoinBlocked(FileInfo fileInfo, LogContext logContext, string line, int offset)
{
// 2021.04.07 09:34:37 Error - [Behaviour] Master is not sending any events! Moving to a new instance.
if (string.Compare(line, offset, "Master is not sending any events! Moving to a new instance.", 0, 59, StringComparison.Ordinal) != 0)
{
return false;
}
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"event",
"Joining instance blocked by master"
});
return true;
}
private bool ParseLogAvatarPedestalChange(FileInfo fileInfo, LogContext logContext, string line, int offset)
{
// 2021.05.07 10:48:19 Log - [Network Processing] RPC invoked SwitchAvatar on AvatarPedestal for User
if (string.Compare(line, offset, "RPC invoked SwitchAvatar on AvatarPedestal for ", 0, 47, StringComparison.Ordinal) != 0)
{
return false;
}
var data = line.Substring(offset + 47);
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"event",
$"{data} changed avatar pedestal"
});
return true;
}
private bool ParseLogVideoError(FileInfo fileInfo, LogContext logContext, string line, int offset)
{
// 2021.04.08 06:37:45 Error - [Video Playback] ERROR: Video unavailable
// 2021.04.08 06:40:07 Error - [Video Playback] ERROR: Private video
if (string.Compare(line, offset, "ERROR: ", 0, 7, StringComparison.Ordinal) != 0)
{
return false;
}
var data = line.Substring(offset + 7);
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"event",
"VideoError: " + data
});
return true;
}
private bool ParseLogVideoPlay(FileInfo fileInfo, LogContext logContext, string line, int offset)
{
// 2021.04.20 13:37:69 Log - [Video Playback] Attempting to resolve URL 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
if (string.Compare(line, offset, "Attempting to resolve URL '", 0, 27, StringComparison.Ordinal) != 0)
{
return false;
}
var pos = line.LastIndexOf("'");
if (pos < 0)
{
return false;
}
var data = line.Substring(78);
data = data.Remove(data.Length - 1);
if (logContext.LastVideoURL == data)
{
return false;
}
logContext.LastVideoURL = data;
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"video-play",
data
});
return true;
}
private bool ParseLogSDK2VideoPlay(FileInfo fileInfo, LogContext logContext, string line, int offset)
{
// 2021.04.23 13:12:25 Log - User Natsumi-sama added URL https://www.youtube.com/watch?v=dQw4w9WgXcQ
if (string.Compare(line, offset, "User ", 0, 5, StringComparison.Ordinal) != 0)
{
return false;
}
var pos = line.LastIndexOf(" added URL ");
if (pos < 0)
{
return false;
}
var displayName = line.Substring(offset + 5, pos - (offset + 5));
var data = line.Substring(pos + 11);
if (logContext.LastVideoURL == data)
{
return false;
}
logContext.LastVideoURL = data;
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"video-play",
data,
displayName
});
return true;
}
private bool ParseLogNotification(FileInfo fileInfo, LogContext logContext, string line, int offset)
{
// 2021.01.03 05:48:58 Log - Received Notification: < Notification from username:pypy, sender user id:usr_4f76a584-9d4b-46f6-8209-8305eb683661 to of type: friendRequest, id: not_3a8f66eb-613c-4351-bee3-9980e6b5652c, created at: 01/14/2021 15:38:40 UTC, details: {{}}, type:friendRequest, m seen:False, message: ""> received at 01/02/2021 16:48:58 UTC
if (string.Compare(line, offset, "Received Notification: <", 0, 24, StringComparison.Ordinal) != 0)
{
return false;
}
var pos = line.LastIndexOf("> received at ");
if (pos < 0)
{
return false;
}
var data = line.Substring(offset + 24, pos - (offset + 24));
AppendLog(new[]
{
fileInfo.Name,
ConvertLogTimeToISO8601(line),
"notification",
data
});
return true;
}
public void Reset()
{
m_ResetLog = true;
m_Thread?.Interrupt();
}
public string[][] Get()
{
if (m_ResetLog == false &&
m_LogList.Count > 0)
{
m_LogListLock.EnterWriteLock();
try
{
string[][] items;
if (m_LogList.Count > 1000)
{
items = new string[1000][];
m_LogList.CopyTo(0, items, 0, 1000);
m_LogList.RemoveRange(0, 1000);
}
else
{
items = m_LogList.ToArray();
m_LogList.Clear();
}
return items;
}
finally
{
m_LogListLock.ExitWriteLock();
}
}
return new string[][] { };
}
}
}