Skip to content

Commit 208ac0d

Browse files
committed
Logger: (Optimization) Keep log the file open for the duration of the application.
1 parent 18fa726 commit 208ac0d

File tree

3 files changed

+108
-9
lines changed

3 files changed

+108
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.3.2
4+
5+
### Chnages
6+
- Logger: (Optimization) Keep log the file open for the duration of the application.
7+
38
## 1.3.1
49

510
### Fixed

Runtime/Scripts/Logger.cs

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
using UnityEngine;
22
using System;
33
using System.IO;
4+
using System.Collections.Generic;
45

56
namespace BetaHub
67
{
7-
public class Logger
8+
public class Logger : IDisposable
89
{
910
private string logFileName;
1011
private string _logPath;
12+
private FileStream fileStream;
13+
private StreamWriter writer;
14+
private List<string> logBuffer;
15+
private readonly object lockObject = new object();
16+
private float lastFlushTime;
17+
private bool disposed = false;
18+
19+
private const int BUFFER_SIZE = 50;
20+
private const float FLUSH_INTERVAL = 2f;
1121

1222
public string LogPath => _logPath;
1323

@@ -21,34 +31,118 @@ public Logger()
2131

2232
Application.logMessageReceivedThreaded += UnityLogHandler;
2333
_logPath = Path.Combine(Application.persistentDataPath, logFileName);
24-
if (File.Exists(_logPath))
34+
35+
logBuffer = new List<string>(BUFFER_SIZE);
36+
lastFlushTime = Time.realtimeSinceStartup;
37+
38+
InitializeFileStream();
39+
}
40+
41+
private void InitializeFileStream()
42+
{
43+
try
2544
{
26-
File.Delete(_logPath);
45+
if (File.Exists(_logPath))
46+
{
47+
File.Delete(_logPath);
48+
}
49+
50+
fileStream = new FileStream(_logPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
51+
writer = new StreamWriter(fileStream) { AutoFlush = false };
52+
}
53+
catch (Exception e)
54+
{
55+
Debug.LogError("Error initializing log file stream: " + e.Message);
2756
}
2857
}
2958

3059
private void UnityLogHandler(string condition, string stackTrace, LogType type)
3160
{
61+
if (disposed) return;
62+
3263
string log = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " [" + type + "] " + condition + "\n" + stackTrace;
3364
WriteToLog(log);
3465
}
3566

3667
private void WriteToLog(string log)
3768
{
38-
try
69+
if (disposed || writer == null) return;
70+
71+
lock (lockObject)
3972
{
40-
using (var fileStream = new FileStream(_logPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
73+
try
4174
{
42-
using (var writer = new StreamWriter(fileStream))
75+
logBuffer.Add(log);
76+
77+
bool shouldFlush = logBuffer.Count >= BUFFER_SIZE ||
78+
(Time.realtimeSinceStartup - lastFlushTime) >= FLUSH_INTERVAL;
79+
80+
if (shouldFlush)
4381
{
44-
writer.WriteLine(log);
82+
FlushBuffer();
4583
}
4684
}
85+
catch (Exception e)
86+
{
87+
Debug.LogError("Error buffering log: " + e.Message);
88+
}
89+
}
90+
}
91+
92+
private void FlushBuffer()
93+
{
94+
if (logBuffer.Count == 0 || writer == null) return;
95+
96+
try
97+
{
98+
foreach (string log in logBuffer)
99+
{
100+
writer.WriteLine(log);
101+
}
102+
103+
writer.Flush();
104+
fileStream.Flush();
105+
logBuffer.Clear();
106+
lastFlushTime = Time.realtimeSinceStartup;
47107
}
48108
catch (Exception e)
49109
{
50-
Debug.LogError("Error writing to log file: " + e.Message);
110+
Debug.LogError("Error flushing log buffer: " + e.Message);
111+
}
112+
}
113+
114+
public void ForceFlush()
115+
{
116+
lock (lockObject)
117+
{
118+
FlushBuffer();
51119
}
52120
}
121+
122+
public void Dispose()
123+
{
124+
if (disposed) return;
125+
126+
Application.logMessageReceivedThreaded -= UnityLogHandler;
127+
128+
lock (lockObject)
129+
{
130+
FlushBuffer();
131+
132+
writer?.Dispose();
133+
fileStream?.Dispose();
134+
135+
writer = null;
136+
fileStream = null;
137+
logBuffer?.Clear();
138+
139+
disposed = true;
140+
}
141+
}
142+
143+
~Logger()
144+
{
145+
Dispose();
146+
}
53147
}
54148
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.betahub.bugreporter",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"displayName": "BetaHub Bug Reporter",
55
"description": "A tool for recording gameplay videos and submitting bug reports directly to BetaHub.",
66
"unity": "2022.3",

0 commit comments

Comments
 (0)