Skip to content

Commit fd34c3e

Browse files
committed
Logger: Rolling log few fixes/improvements [Related #642]
- Use different FileRollPtr instead - Ensure flushing has been completed before rolling - Don't enable it by default (LogMaxNumberOfBackupFiles = 0) - Increase LogRollMaxFileSize to 10MB (especially with trace size will grow too fast) - Rename Config fields so it will be clear that they are both for rolling - Ensures both greater than 0 before enabling them
1 parent 6483c8e commit fd34c3e

2 files changed

Lines changed: 44 additions & 62 deletions

File tree

FlyleafLib/Engine/Config.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,14 +1021,14 @@ public Flyleaf.FFmpeg.LogLevel
10211021
public int LogCachedLines { get; set; } = 20;
10221022

10231023
/// <summary>
1024-
/// Max filesize of log before starting a new log file when <see cref="LogMaxNumberOfBackupFiles"/> is greater than 0
1024+
/// Max filesize (in bytes) of log before starting a new log file <see cref="LogRollMaxFiles"/>
10251025
/// </summary>
1026-
public long LogFileSizeMax { get; set; } = 1024 * 1024;
1026+
public long LogRollMaxFileSize { get; set; } = 10 * 1024 * 1024;
10271027

10281028
/// <summary>
1029-
/// Max number of backup logfiles to roll over before deleting the oldest, setting this to 0 disables rollover
1029+
/// Max number of log files to roll over before deleting the oldest
10301030
/// </summary>
1031-
public long LogMaxNumberOfBackupFiles { get; set; } = 5;
1031+
public long LogRollMaxFiles { get; set; } = 0;
10321032

10331033
/// <summary>
10341034
/// Sets the logger's datetime string format

FlyleafLib/Utils/Logger.cs

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ public static class Logger
99
public static bool CanTrace => Engine.Config.LogLevel >= LogLevel.Trace;
1010

1111

12-
public static Action<string>
13-
CustomOutput = DevNullPtr;
14-
internal static Action<string>
15-
Output = DevNullPtr;
16-
static string lastOutput = "";
12+
public static Action<string> CustomOutput = DevNullPtr;
13+
internal static Action<string> Output = DevNullPtr;
1714

1815
static ConcurrentQueue<byte[]>
1916
fileData = [];
@@ -48,49 +45,24 @@ internal static void SetOutput()
4845
string output = Engine.Config.LogOutput;
4946

5047
if (string.IsNullOrEmpty(output))
51-
{
52-
if (lastOutput != "")
53-
{
54-
Output = DevNullPtr;
55-
lastOutput = "";
56-
}
57-
}
58-
else if (output.StartsWith(":"))
48+
Output = DevNullPtr;
49+
else if (output.StartsWith(':'))
5950
{
6051
if (output == ":console")
61-
{
62-
if (lastOutput != ":console")
63-
{
64-
Output = Console.WriteLine;
65-
lastOutput = ":console";
66-
}
67-
}
52+
Output = Console.WriteLine;
6853
else if (output == ":debug")
69-
{
70-
if (lastOutput != ":debug")
71-
{
72-
Output = DebugPtr;
73-
lastOutput = ":debug";
74-
}
75-
}
54+
Output = DebugPtr;
7655
else if (output == ":custom")
77-
{
78-
if (lastOutput != ":custom")
79-
{
80-
Output = CustomOutput;
81-
lastOutput = ":custom";
82-
}
83-
}
56+
Output = CustomOutput;
8457
else
8558
throw new Exception("Invalid log output");
8659
}
8760
else
8861
{
8962
lock (lockFileStream)
9063
{
91-
// Flush File Data on Previously Opened File Stream
9264
if (fileStream != null)
93-
{
65+
{ // Flush File Data on Previously Opened File Stream
9466
while (fileData.TryDequeue(out byte[] data))
9567
fileStream.Write(data, 0, data.Length);
9668
fileStream.Dispose();
@@ -103,21 +75,19 @@ internal static void SetOutput()
10375
if (Engine.Config.LogAppend)
10476
{
10577
fileStream = new FileStream(output, FileMode.Append, FileAccess.Write);
78+
Output = FilePtr;
10679
}
107-
else if (Engine.Config.LogFileSizeMax > 0)
80+
81+
else if (Engine.Config.LogRollMaxFiles > 0 && Engine.Config.LogRollMaxFileSize > 0)
10882
{
109-
// If we have rolling log enables and do not append, then we need to roll the log files first
110-
RollLogFiles();
83+
RollLogFiles(); // If we have rolling log enables and do not append, then we need to roll the log files first
11184
fileStream = new FileStream(Engine.Config.LogOutput, FileMode.Create, FileAccess.Write);
85+
Output = FileRollPtr;
11286
}
11387
else
11488
{
11589
fileStream = new FileStream(output, FileMode.Create, FileAccess.Write);
116-
}
117-
if (lastOutput != ":file")
118-
{
11990
Output = FilePtr;
120-
lastOutput = ":file";
12191
}
12292
}
12393
}
@@ -128,38 +98,52 @@ static void FilePtr(string msg)
12898
{
12999
fileData.Enqueue(Encoding.UTF8.GetBytes($"{msg}\r\n"));
130100

131-
if (!fileTaskRunning && Engine.Config.LogFileSizeMax > 0 && fileStream.Length >= Engine.Config.LogFileSizeMax)
132-
{
133-
HandleLogFileRolling();
134-
return;
135-
}
101+
if (!fileTaskRunning && fileData.Count > Engine.Config.LogCachedLines)
102+
FlushFileData();
103+
}
104+
105+
static void FileRollPtr(string msg)
106+
{
107+
fileData.Enqueue(Encoding.UTF8.GetBytes($"{msg}\r\n"));
136108

137109
if (!fileTaskRunning && fileData.Count > Engine.Config.LogCachedLines)
138110
{
139-
FlushFileData();
140-
return;
111+
if (fileStream.Length >= Engine.Config.LogRollMaxFileSize)
112+
{
113+
while (fileTaskRunning) Thread.Sleep(10);
114+
115+
lock (lockFileStream)
116+
{
117+
while (fileData.TryDequeue(out byte[] data))
118+
fileStream.Write(data, 0, data.Length);
119+
120+
fileStream.Flush();
121+
}
122+
123+
HandleLogFileRolling();
124+
}
125+
else
126+
FlushFileData();
141127
}
142128
}
143129

144130
static void RollLogFiles()
145131
{
146132
string name = Engine.Config.LogOutput;
147-
for (long i = Engine.Config.LogMaxNumberOfBackupFiles; i > 0; i--)
133+
134+
for (long i = Engine.Config.LogRollMaxFiles; i > 0; i--)
148135
{
149136
string logFile = $"{name}.{i}";
150137
string nextLogFile = $"{name}.{i + 1}";
151138
if (File.Exists(logFile))
152139
{
153-
if (i == Engine.Config.LogMaxNumberOfBackupFiles)
154-
{
140+
if (i == Engine.Config.LogRollMaxFiles)
155141
File.Delete(logFile);
156-
}
157142
else
158-
{
159143
File.Move(logFile, nextLogFile);
160-
}
161144
}
162145
}
146+
163147
if (File.Exists(name))
164148
File.Move(name, $"{name}.{1}");
165149
}
@@ -173,9 +157,7 @@ static void HandleLogFileRolling()
173157
lock (lockFileStream)
174158
{
175159
fileStream.Dispose();
176-
177160
RollLogFiles();
178-
179161
fileStream = new FileStream(Engine.Config.LogOutput, FileMode.Create, FileAccess.Write);
180162
}
181163

0 commit comments

Comments
 (0)