Created
August 14, 2016 00:17
-
-
Save emilk/d91595ee25b36ce82ce9ef8c5833793d to your computer and use it in GitHub Desktop.
Daily log files with Loguru
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The first 10 bytes of message.preamble is the current date in YYYY-MM-DD. | |
#define DATE_LEN 10 | |
struct DailyLogger | |
{ | |
FILE* file = nullptr; | |
char date[DATE_LEN] = { 0 }; | |
}; | |
void daily_log(void* user_data, const loguru::Message& message) | |
{ | |
DailyLogger* logger = reinterpret_cast<DailyLogger*>(user_data); | |
if (strncmp(logger->date, message.preamble, DATE_LEN) != 0) { | |
// A new day (or first time we ever log) | |
fclose(logger->file); | |
char filename[15]; | |
sprintf(filename, "%.*s.log", DATE_LEN, message.preamble); | |
logger->file = fopen(filename, "w"); | |
memcpy(logger->date, message.preamble, DATE_LEN); | |
} | |
fprintf(logger->file, "%s%s%s%s\n", message.preamble, message.indentation, message.prefix, message.message); | |
fflush(logger->file); | |
} | |
void daily_close(void* user_data) | |
{ | |
DailyLogger* logger = reinterpret_cast<DailyLogger*>(user_data); | |
fclose(logger->file); | |
delete logger; | |
} | |
... | |
loguru::add_callback("daily", daily_log, new DailyLogger, daily_close); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment