1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-12-27 06:15:19 +00:00

Discard large log files on boot

This commit is contained in:
cathery 2020-04-18 15:19:02 +03:00
parent 2aac5d747e
commit f527c2c388
3 changed files with 29 additions and 1 deletions

View File

@ -306,6 +306,7 @@ namespace syscon::config
Result Initialize()
{
DiscardOldLogs();
config::LoadAllConfigs();
config::CheckForFileChanges();
utimerCreate(&filecheckTimer, 1e+9L, TimerType_Repeating);

View File

@ -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);

View File

@ -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)));