-
-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathFileLogStorage.cs
More file actions
458 lines (398 loc) · 15.9 KB
/
Copy pathFileLogStorage.cs
File metadata and controls
458 lines (398 loc) · 15.9 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
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
/*
┌──────────────────────────────────────────────────────────────────┐
│ Author: Ivan Murzak (https://github.com/IvanMurzak) │
│ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │
│ Copyright (c) 2025 Ivan Murzak │
│ Licensed under the Apache License, Version 2.0. │
│ See the LICENSE file in the project root for more information. │
└──────────────────────────────────────────────────────────────────┘
*/
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using com.IvanMurzak.McpPlugin.Common;
using com.IvanMurzak.ReflectorNet;
using com.IvanMurzak.ReflectorNet.Utils;
using com.IvanMurzak.Unity.MCP.Utils;
using Microsoft.Extensions.Logging;
using UnityEngine;
namespace com.IvanMurzak.Unity.MCP
{
using ILogger = Microsoft.Extensions.Logging.ILogger;
public class FileLogStorage : ILogStorage, IDisposable
{
protected const int DefaultMaxFileSizeMB = 512;
protected readonly ILogger _logger;
protected readonly string _directoryPath;
protected readonly string _requestedFileName;
protected readonly JsonSerializerOptions _jsonOptions;
protected readonly object _fileMutex = new();
protected readonly int _fileBufferSize;
protected readonly long _maxFileSizeBytes;
protected readonly ThreadSafeBool _isDisposed = new(false);
protected string fileName;
protected string filePath;
protected FileStream? fileWriteStream;
public FileLogStorage(
ILogger? logger = null,
string? directoryPath = null,
string? requestedFileName = null,
int fileBufferSize = 4096,
int maxFileSizeMB = DefaultMaxFileSizeMB,
JsonSerializerOptions? jsonOptions = null)
{
if (!MainThread.Instance.IsMainThread)
throw new Exception($"{nameof(FileLogStorage)} must be initialized on the main thread.");
if (fileBufferSize <= 0)
throw new ArgumentOutOfRangeException(nameof(fileBufferSize), "File buffer size must be greater than zero.");
if (maxFileSizeMB <= 0)
throw new ArgumentOutOfRangeException(nameof(maxFileSizeMB), "Max file size must be greater than zero.");
_logger = logger ?? UnityLoggerFactory.LoggerFactory.CreateLogger(GetType().GetTypeShortName());
_directoryPath = Path.GetFullPath(directoryPath ?? (Application.isEditor
? $"{Path.GetDirectoryName(Application.dataPath)}/Temp/mcp-server"
: $"{Application.persistentDataPath}/Temp/mcp-server"));
_requestedFileName = requestedFileName ?? (Application.isEditor
? "ai-editor-logs.txt"
: "ai-player-logs.txt");
if (!Directory.Exists(_directoryPath))
Directory.CreateDirectory(_directoryPath);
_fileBufferSize = fileBufferSize;
_maxFileSizeBytes = maxFileSizeMB * 1024L * 1024L;
_jsonOptions = jsonOptions ?? new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
fileWriteStream = CreateWriteStream(_requestedFileName, out fileName, out filePath);
}
protected virtual FileStream CreateWriteStream(string fileName, out string resultFileName, out string resultFilePath)
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(CreateWriteStream));
throw new ObjectDisposedException(GetType().GetTypeShortName());
}
var baseName = Path.GetFileNameWithoutExtension(fileName);
var extension = Path.GetExtension(fileName);
var currentFileName = fileName;
int incrementIndex = 1;
while (true)
{
if (incrementIndex > 1000)
throw new Exception("Failed to create unique log file name after 1000 attempts.");
try
{
var filePath = Path.GetFullPath(Path.Combine(_directoryPath, currentFileName));
_logger.LogDebug("Creating log file stream: {file}", filePath);
var stream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, bufferSize: _fileBufferSize, useAsync: false)
?? throw new Exception("Failed to create file stream for log storage.");
resultFileName = currentFileName;
resultFilePath = filePath;
return stream;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to create log file stream for {file}. Retrying with a different file name.",
currentFileName);
incrementIndex++;
currentFileName = $"{baseName}-{incrementIndex}{extension}";
}
}
}
public virtual void Flush()
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(Flush));
return;
}
lock (_fileMutex)
{
fileWriteStream?.Flush();
}
}
public virtual Task FlushAsync()
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(FlushAsync));
return Task.CompletedTask;
}
return Task.Run(() =>
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(FlushAsync));
return;
}
lock (_fileMutex)
{
fileWriteStream?.Flush();
}
});
}
public virtual Task AppendAsync(params LogEntry[] entries)
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(AppendAsync));
return Task.CompletedTask;
}
return Task.Run(() =>
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(AppendAsync));
return;
}
lock (_fileMutex)
{
AppendInternal(entries);
}
});
}
public virtual void Append(params LogEntry[] entries)
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(Append));
return;
}
lock (_fileMutex)
{
AppendInternal(entries);
}
}
protected virtual void AppendInternal(params LogEntry[] entries)
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(AppendInternal));
return;
}
fileWriteStream ??= CreateWriteStream(_requestedFileName, out fileName, out filePath);
// Check if file size limit reached and reset if needed
if (fileWriteStream.Length >= _maxFileSizeBytes)
{
ResetLogFile();
}
foreach (var entry in entries)
{
System.Text.Json.JsonSerializer.Serialize(fileWriteStream, entry, _jsonOptions);
fileWriteStream.WriteByte((byte)'\n');
}
fileWriteStream.Flush();
}
/// <summary>
/// Resets the log file by deleting it and creating a new one.
/// Called when file size limit is reached.
/// </summary>
protected virtual void ResetLogFile()
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(ResetLogFile));
return;
}
_logger.LogInformation("Log file size limit reached ({maxSizeMB}MB). Resetting log file.",
_maxFileSizeBytes / (1024 * 1024));
fileWriteStream?.Flush();
fileWriteStream?.Dispose();
fileWriteStream = null;
if (File.Exists(filePath))
File.Delete(filePath);
fileWriteStream = CreateWriteStream(_requestedFileName, out this.fileName, out filePath);
}
/// <summary>
/// Closes and disposes the current file stream if open. Clears the log cache file.
/// </summary>
public virtual void Clear()
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(Clear));
return;
}
lock (_fileMutex)
{
fileWriteStream?.Dispose();
fileWriteStream = null;
if (File.Exists(filePath))
File.Delete(filePath);
if (File.Exists(filePath))
_logger.LogError("Failed to delete cache file: {file}", filePath);
}
}
public virtual Task<LogEntry[]> QueryAsync(
int maxEntries = 100,
LogType? logTypeFilter = null,
bool includeStackTrace = false,
int lastMinutes = 0)
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(QueryAsync));
return Task.FromResult(Array.Empty<LogEntry>());
}
return Task.Run(() => Query(maxEntries, logTypeFilter, includeStackTrace, lastMinutes));
}
public virtual LogEntry[] Query(
int maxEntries = 100,
LogType? logTypeFilter = null,
bool includeStackTrace = false,
int lastMinutes = 0)
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(Query));
return Array.Empty<LogEntry>();
}
lock (_fileMutex)
{
return QueryInternal(maxEntries, logTypeFilter, includeStackTrace, lastMinutes);
}
}
protected virtual LogEntry[] QueryInternal(
int maxEntries = 100,
LogType? logTypeFilter = null,
bool includeStackTrace = false,
int lastMinutes = 0)
{
if (!File.Exists(filePath))
return Array.Empty<LogEntry>();
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var cutoffTime = lastMinutes > 0
? DateTime.Now.AddMinutes(-lastMinutes)
: (DateTime?)null;
var allLogs = ReadLogEntriesFromLinesInReverse(fileStream, cutoffTime);
// Apply log type filter
if (logTypeFilter.HasValue)
{
allLogs = allLogs
.Where(log => log.LogType == logTypeFilter.Value);
}
// Take the most recent entries (up to maxEntries)
var filteredLogs = allLogs
.Take(maxEntries)
.Reverse()
.ToArray();
// Clear stack traces if not requested
if (!includeStackTrace)
{
foreach (var log in filteredLogs)
log.StackTrace = null;
}
return filteredLogs;
}
}
protected virtual IEnumerable<LogEntry> ReadLogEntriesFromLinesInReverse(FileStream fileStream, DateTime? cutoffTime = null)
{
if (_isDisposed.Value)
{
_logger.LogWarning("{method} called but already disposed, ignored.",
nameof(ReadLogEntriesFromLinesInReverse));
yield break;
}
var position = fileStream.Length;
if (position == 0) yield break;
var buffer = new byte[_fileBufferSize];
var lineBuffer = new List<byte>();
while (position > 0)
{
var bytesToRead = (int)Math.Min(position, _fileBufferSize);
position -= bytesToRead;
fileStream.Seek(position, SeekOrigin.Begin);
fileStream.Read(buffer, 0, bytesToRead);
for (int i = bytesToRead - 1; i >= 0; i--)
{
var b = buffer[i];
if (b == '\n')
{
if (lineBuffer.Count > 0)
{
lineBuffer.Reverse();
var logEntry = DeserializeLogEntry(lineBuffer);
if (logEntry != null)
{
if (cutoffTime.HasValue && logEntry.Timestamp < cutoffTime.Value)
yield break;
yield return logEntry;
}
lineBuffer.Clear();
}
}
else if (b == '\r')
{
// Ignore \r
}
else
{
lineBuffer.Add(b);
}
}
}
if (lineBuffer.Count > 0)
{
lineBuffer.Reverse();
var logEntry = DeserializeLogEntry(lineBuffer);
if (logEntry != null)
{
if (cutoffTime.HasValue && logEntry.Timestamp < cutoffTime.Value)
yield break;
yield return logEntry;
}
}
}
protected virtual LogEntry? DeserializeLogEntry(List<byte> jsonBytes)
{
var json = System.Text.Encoding.UTF8.GetString(jsonBytes.ToArray());
return DeserializeLogEntry(json);
}
protected virtual LogEntry? DeserializeLogEntry(string json)
{
try
{
return System.Text.Json.JsonSerializer.Deserialize<LogEntry>(json, _jsonOptions);
}
catch
{
return null;
}
}
public virtual void Dispose()
{
if (!_isDisposed.TrySetTrue())
return; // already disposed
try
{
Flush();
}
finally
{
fileWriteStream?.Dispose();
fileWriteStream = null;
}
GC.SuppressFinalize(this);
}
~FileLogStorage() => Dispose();
}
}