From f527c2c3880de6883b5b86ec8fedfb02a98651db Mon Sep 17 00:00:00 2001 From: cathery Date: Sat, 18 Apr 2020 15:19:02 +0300 Subject: [PATCH] Discard large log files on boot --- source/Sysmodule/source/config_handler.cpp | 1 + source/Sysmodule/source/log.cpp | 26 +++++++++++++++++++++- source/Sysmodule/source/log.h | 3 +++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/source/Sysmodule/source/config_handler.cpp b/source/Sysmodule/source/config_handler.cpp index fe281d9..4eb4ea1 100644 --- a/source/Sysmodule/source/config_handler.cpp +++ b/source/Sysmodule/source/config_handler.cpp @@ -306,6 +306,7 @@ namespace syscon::config Result Initialize() { + DiscardOldLogs(); config::LoadAllConfigs(); config::CheckForFileChanges(); utimerCreate(&filecheckTimer, 1e+9L, TimerType_Repeating); diff --git a/source/Sysmodule/source/log.cpp b/source/Sysmodule/source/log.cpp index c14e94c..7634ffd 100644 --- a/source/Sysmodule/source/log.cpp +++ b/source/Sysmodule/source/log.cpp @@ -5,6 +5,30 @@ static ams::os::Mutex printMutex; +void DiscardOldLogs() +{ + std::scoped_lock printLock(printMutex); + + FsFileSystem *fs = fsdevGetDeviceFileSystem("sdmc"); + FsFile file; + s64 fileSize; + + Result rc = fsFsOpenFile(fs, LOG_PATH, FsOpenMode_Read, &file); + if (R_FAILED(rc)) + return; + + rc = fsFileGetSize(&file, &fileSize); + fsFileClose(&file); + if (R_FAILED(rc)) + return; + + if (fileSize >= 0x20'000) + { + fsFsDeleteFile(fs, LOG_PATH); + WriteToLog("Deleted previous log file"); + } +} + void WriteToLog(const char *fmt, ...) { std::scoped_lock printLock(printMutex); @@ -14,7 +38,7 @@ void WriteToLog(const char *fmt, ...) timeGetCurrentTime(TimeType_LocalSystemClock, &ts); timeToCalendarTimeWithMyRule(ts, &caltime, nullptr); - FILE *fp = fopen(CONFIG_PATH "log.txt", "a"); + FILE *fp = fopen(LOG_PATH, "a"); //Print time fprintf(fp, "%04i-%02i-%02i %02i:%02i:%02i: ", caltime.year, caltime.month, caltime.day, caltime.hour, caltime.minute, caltime.second); diff --git a/source/Sysmodule/source/log.h b/source/Sysmodule/source/log.h index 647c0df..df09311 100644 --- a/source/Sysmodule/source/log.h +++ b/source/Sysmodule/source/log.h @@ -1,9 +1,12 @@ #pragma once +#define LOG_PATH CONFIG_PATH "log.txt" + #ifdef __cplusplus extern "C" { #endif + void DiscardOldLogs(); void WriteToLog(const char *fmt, ...) __attribute__((format(printf, 1, 2)));