Skip to content

Commit 5f0db72

Browse files
committed
Add log rotations.
1 parent f7a999c commit 5f0db72

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

TheForceEngine/TFE_System/log.cpp

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace TFE_System
3131
};
3232

3333
bool includeTime = true;
34+
int maxLogRotations = 3;
3435

3536
void logTimeToggle()
3637
{
@@ -55,7 +56,7 @@ namespace TFE_System
5556
{
5657
s_logFile.close();
5758
}
58-
59+
5960
void debugWrite(const char* tag, const char* str, ...)
6061
{
6162
if (!tag || !str) { return; }
@@ -69,11 +70,11 @@ namespace TFE_System
6970
sprintf(s_workStr, "[%s] %s\r\n", tag, s_msgStr);
7071

7172
//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
7778
}
7879

7980
void logWrite(LogWriteType type, const char* tag, const char* str, ...)
@@ -83,12 +84,12 @@ namespace TFE_System
8384
auto now = std::chrono::system_clock::now();
8485
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
8586
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
9293

9394
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
9495
now.time_since_epoch()) % 1000;
@@ -130,11 +131,11 @@ namespace TFE_System
130131
s_logFile.flush();
131132
}
132133
//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
138139
//Critical log messages also act as asserts in the debugger.
139140
if (type == LOG_CRITICAL)
140141
{
@@ -160,4 +161,37 @@ namespace TFE_System
160161
TFE_FrontEndUI::logToConsole(msgStart);
161162
}
162163
}
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+
}
163196
}
197+

TheForceEngine/TFE_System/system.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace TFE_System
5959
bool logOpen(const char* filename, bool append=false);
6060
void logClose();
6161
void logWrite(LogWriteType type, const char* tag, const char* str, ...);
62+
void logRotatingLogFile(const char* fileName, bool append=false);
6263

6364
// Lighter weight debug output (only useful when running in a terminal or debugger).
6465
void debugWrite(const char* tag, const char* str, ...);

TheForceEngine/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ int main(int argc, char* argv[])
509509
pathsSet &= TFE_Paths::setProgramPath();
510510
pathsSet &= TFE_Paths::setProgramDataPath("TheForceEngine");
511511
pathsSet &= TFE_Paths::setUserDocumentsPath("TheForceEngine");
512-
TFE_System::logOpen("the_force_engine_log.txt");
512+
TFE_System::logRotatingLogFile("the_force_engine_log.txt");
513513
TFE_System::logWrite(LOG_MSG, "Main", "The Force Engine %s", c_gitVersion);
514514
if (!pathsSet)
515515
{

0 commit comments

Comments
 (0)