From f87460cbc977b2c6cfce207008b01c91ac3c69f2 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Wed, 5 Apr 2023 19:16:00 -0500 Subject: [PATCH] implement log rotation --- src/log.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/log.cpp b/src/log.cpp index 536583c44..074e79c40 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -18,6 +18,7 @@ */ #include "ta-log.h" +#include "fileutils.h" #include #include @@ -169,9 +170,28 @@ bool startLogFile(const char* path) { if (logFileAvail) return true; // rotate log file if possible + char oldPath[4096]; + char newPath[4096]; + + if (fileExists(path)==1) { + for (int i=4; i>=0; i--) { + if (i>0) { + snprintf(oldPath,4095,"%s.%d",path,i); + } else { + strncpy(oldPath,path,4095); + } + snprintf(newPath,4095,"%s.%d",path,i+1); + + if (i>=4) { + deleteFile(oldPath); + } else { + moveFiles(oldPath,newPath); + } + } + } // open log file - if ((logFile=fopen(path,"w+"))==NULL) { + if ((logFile=ps_fopen(path,"w+"))==NULL) { logFileAvail=false; logW("could not open log file! (%s)",strerror(errno)); return false;