Skip to content

Commit

Permalink
Add log rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
IonAgorria committed Jun 9, 2024
1 parent a2b5f4b commit c90a9e8
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions Source/XTool/xerrhand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ static const char *defprefix = "XHANDLER INFORM";
static const char *exceptMSG = "EXCEPTION OCCURED";
static const char *rterrorMSG = "RUN-TIME ERROR";

const static int MAX_LOGS = 3;

XErrorHandler ErrH;

void setSignalHandler(sighandler signalHandler) {
Expand Down Expand Up @@ -206,18 +208,46 @@ XErrorHandler::XErrorHandler() {
crash_func = nullptr;
restore_func = nullptr;
state = 0;
std::string pref_path;
log_path.clear();
const char* lop_path_ptr = GET_PREF_PATH();
if (lop_path_ptr) {
log_path = lop_path_ptr;
SDL_free((void*) lop_path_ptr);
const char* pref_path_ptr = GET_PREF_PATH();
if (pref_path_ptr) {
pref_path = pref_path_ptr;
SDL_free((void*) pref_path_ptr);
}
log_path += "logfile.txt";
if (std::filesystem::exists(std::filesystem::u8path(log_path))) {
log_path = pref_path + "logfile.txt";
//Do log rotation from oldest to current
for (int i = MAX_LOGS; 0 <= i; --i) {
std::error_code error;
std::filesystem::remove(std::filesystem::u8path(log_path), error);
std::filesystem::path src_path = std::filesystem::u8path(
i == 0 ? log_path : pref_path + "logfile_" + std::to_string(i) + ".txt"
);
if (!std::filesystem::exists(src_path)) {
continue;
}
if (i == MAX_LOGS) {
//Oldest log, remove it
std::filesystem::remove(src_path, error);
if (error) {
fprintf(stderr, "Error deleting oldest log file: %d %s at %s\n",

Check warning on line 232 in Source/XTool/xerrhand.cpp

View workflow job for this annotation

GitHub Actions / Debug x86 64 bits

'fprintf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'const std::filesystem::path::value_type *'

Check warning on line 232 in Source/XTool/xerrhand.cpp

View workflow job for this annotation

GitHub Actions / Release x86 32 bits

'fprintf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'const std::filesystem::path::value_type *'

Check warning on line 232 in Source/XTool/xerrhand.cpp

View workflow job for this annotation

GitHub Actions / Release x86 64 bits

'fprintf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'const std::filesystem::path::value_type *'
error.value(), error.message().c_str(), src_path.c_str());
}
continue;
}
std::filesystem::path dst_path = std::filesystem::u8path(
pref_path + "logfile_" + std::to_string(i + 1) + ".txt"
);
if (std::filesystem::exists(dst_path)) {
std::filesystem::remove(dst_path, error);
if (error) {
fprintf(stderr, "Error deleting log file: %d %s at %s\n",

Check warning on line 243 in Source/XTool/xerrhand.cpp

View workflow job for this annotation

GitHub Actions / Debug x86 64 bits

'fprintf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'const std::filesystem::path::value_type *'

Check warning on line 243 in Source/XTool/xerrhand.cpp

View workflow job for this annotation

GitHub Actions / Release x86 32 bits

'fprintf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'const std::filesystem::path::value_type *'

Check warning on line 243 in Source/XTool/xerrhand.cpp

View workflow job for this annotation

GitHub Actions / Release x86 64 bits

'fprintf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'const std::filesystem::path::value_type *'
error.value(), error.message().c_str(), dst_path.c_str());
}
}
std::filesystem::rename(src_path, dst_path, error);
if (error) {
fprintf(stderr, "Error deleting log file: %d %s at %s\n", error.value(), error.message().c_str(), log_path.c_str());
fprintf(stderr, "Error renaming log file: %d %s at %s\n",

Check warning on line 249 in Source/XTool/xerrhand.cpp

View workflow job for this annotation

GitHub Actions / Debug x86 64 bits

'fprintf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'const std::filesystem::path::value_type *'

Check warning on line 249 in Source/XTool/xerrhand.cpp

View workflow job for this annotation

GitHub Actions / Release x86 32 bits

'fprintf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'const std::filesystem::path::value_type *'

Check warning on line 249 in Source/XTool/xerrhand.cpp

View workflow job for this annotation

GitHub Actions / Release x86 64 bits

'fprintf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'const std::filesystem::path::value_type *'
error.value(), error.message().c_str(), src_path.c_str());
}
}

Expand Down

0 comments on commit c90a9e8

Please sign in to comment.