[host] windows: rotate the host log out keeping at most 3 prior versions

This commit is contained in:
Geoffrey McRae 2024-03-06 12:59:15 +11:00
parent d81395b672
commit 9123984ecc

View File

@ -33,6 +33,7 @@
#include <winternl.h> #include <winternl.h>
#include <dwmapi.h> #include <dwmapi.h>
#include <avrt.h> #include <avrt.h>
#include <sys/stat.h>
#include "interface/platform.h" #include "interface/platform.h"
#include "common/debug.h" #include "common/debug.h"
@ -45,7 +46,7 @@
#define ID_MENU_SHOW_LOG 3000 #define ID_MENU_SHOW_LOG 3000
#define ID_MENU_EXIT 3001 #define ID_MENU_EXIT 3001
#define LOG_NAME "looking-glass-host.txt" #define LOG_NAME "looking-glass-host"
struct AppState struct AppState
{ {
@ -542,10 +543,35 @@ bool app_init(void)
// redirect stderr to a file // redirect stderr to a file
if (logFile && strcmp(logFile, "stderr") != 0) if (logFile && strcmp(logFile, "stderr") != 0)
{ {
DEBUG_INFO("Logs will be written to: %s", logFile); // if the log file already exists, rotate it out
char finalName[strlen(logFile)+5];
char name1[strlen(logFile)+7];
char name2[strlen(logFile)+7];
sprintf(finalName, "%s.txt", logFile);
struct stat st;
if (stat(finalName, &st) == 0)
{
sprintf(name2, "%s.%d.txt", logFile, 3);
if (stat(name2, &st) == 0)
unlink(name2);
for(int i = 2; i > 0; --i)
{
sprintf(name1, "%s.%d.txt", logFile, i);
if (stat(name1, &st) == 0)
rename(name1, name2);
strcpy(name2, name1);
}
rename(finalName, name2);
}
DEBUG_INFO("Logs will be written to: %s", finalName);
DEBUG_INFO("Please see there for any further information"); DEBUG_INFO("Please see there for any further information");
if (!freopen(logFile, "a", stderr)) if (!freopen(finalName, "a", stderr))
DEBUG_WARN("Failed to open log file, will log to stderr"); DEBUG_WARN("Failed to open log file, will log to stderr");
} }