@@ -31,6 +31,7 @@ namespace TFE_System
31
31
};
32
32
33
33
bool includeTime = true ;
34
+ int maxLogRotations = 3 ;
34
35
35
36
void logTimeToggle ()
36
37
{
@@ -55,7 +56,7 @@ namespace TFE_System
55
56
{
56
57
s_logFile.close ();
57
58
}
58
-
59
+
59
60
void debugWrite (const char * tag, const char * str, ...)
60
61
{
61
62
if (!tag || !str) { return ; }
@@ -69,11 +70,11 @@ namespace TFE_System
69
70
sprintf (s_workStr, " [%s] %s\r\n " , tag, s_msgStr);
70
71
71
72
// Write to the debugger or terminal output.
72
- #ifdef _WIN32
73
- OutputDebugStringA (s_workStr);
74
- #else
75
- fprintf (stderr, " %s" , s_workStr);
76
- #endif
73
+ #ifdef _WIN32
74
+ OutputDebugStringA (s_workStr);
75
+ #else
76
+ fprintf (stderr, " %s" , s_workStr);
77
+ #endif
77
78
}
78
79
79
80
void logWrite (LogWriteType type, const char * tag, const char * str, ...)
@@ -83,12 +84,12 @@ namespace TFE_System
83
84
auto now = std::chrono::system_clock::now ();
84
85
std::time_t now_c = std::chrono::system_clock::to_time_t (now);
85
86
std::tm now_tm;
86
-
87
- #ifdef _WIN32
88
- localtime_s (&now_tm, &now_c); // For thread safety on Windows
89
- #else
90
- localtime_r (&now_c, &now_tm); // For thread safety on Linux
91
- #endif
87
+
88
+ #ifdef _WIN32
89
+ localtime_s (&now_tm, &now_c); // For thread safety on Windows
90
+ #else
91
+ localtime_r (&now_c, &now_tm); // For thread safety on Linux
92
+ #endif
92
93
93
94
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
94
95
now.time_since_epoch ()) % 1000 ;
@@ -130,11 +131,11 @@ namespace TFE_System
130
131
s_logFile.flush ();
131
132
}
132
133
// Write to the debugger or terminal output.
133
- #ifdef _WIN32
134
- OutputDebugStringA (s_workStr);
135
- #else
136
- fprintf (stderr, " %s" , s_workStr);
137
- #endif
134
+ #ifdef _WIN32
135
+ OutputDebugStringA (s_workStr);
136
+ #else
137
+ fprintf (stderr, " %s" , s_workStr);
138
+ #endif
138
139
// Critical log messages also act as asserts in the debugger.
139
140
if (type == LOG_CRITICAL)
140
141
{
@@ -160,4 +161,37 @@ namespace TFE_System
160
161
TFE_FrontEndUI::logToConsole (msgStart);
161
162
}
162
163
}
164
+
165
+ void logRotatingLogFile (const char * fileName, bool append)
166
+ {
167
+ if (!fileName) { return ; }
168
+
169
+ char logPath[TFE_MAX_PATH];
170
+ TFE_Paths::appendPath (PATH_USER_DOCUMENTS, fileName, logPath);
171
+
172
+ // Handle rotations
173
+ for (int i = maxLogRotations; i > 0 ; i--)
174
+ {
175
+ char oldLogPath[TFE_MAX_PATH];
176
+ char newLogPath[TFE_MAX_PATH];
177
+ if (i == 1 )
178
+ {
179
+ sprintf (oldLogPath, " %s" , logPath);
180
+ }
181
+ else
182
+ {
183
+ sprintf (oldLogPath, " %s.%d" , logPath, i - 1 );
184
+ }
185
+
186
+ if (FileUtil::exists (oldLogPath))
187
+ {
188
+ sprintf (newLogPath, " %s.%d" , logPath, i);
189
+ FileUtil::copyFile (oldLogPath, newLogPath);
190
+ }
191
+ }
192
+
193
+ // Open file as normal
194
+ logOpen (fileName, append);
195
+ }
163
196
}
197
+
0 commit comments