mirror of
https://github.com/tildearrow/furnace.git
synced 2024-11-27 06:53:01 +00:00
implement log rotation
This commit is contained in:
parent
6f2c9535bc
commit
f87460cbc9
1 changed files with 21 additions and 1 deletions
22
src/log.cpp
22
src/log.cpp
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
|
||||
#include "ta-log.h"
|
||||
#include "fileutils.h"
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue