Refactor logging

This commit is contained in:
blankie 2023-01-16 15:56:07 +07:00
parent 7c5e9e5db5
commit 585c79c764
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
2 changed files with 19 additions and 6 deletions

22
log.cpp
View File

@ -7,7 +7,7 @@
std::string log_entries; std::string log_entries;
void log(std::string entry, time_t time) { std::string format_log(std::string entry, time_t time) {
size_t newline_pos; size_t newline_pos;
char time_as_str[128] = {0}; char time_as_str[128] = {0};
@ -16,15 +16,25 @@ void log(std::string entry, time_t time) {
entry.replace(newline_pos, 1, 1, ' '); entry.replace(newline_pos, 1, 1, ' ');
} }
return std::string(1, '[') + time_as_str + "] " + std::move(entry);
}
std::string format_log(std::string entry) {
return format_log(std::move(entry), time(nullptr));
}
void log_raw(std::string line) {
printf("%s\n", line.c_str());
if (!log_entries.empty()) { if (!log_entries.empty()) {
log_entries += '\n'; log_entries += '\n';
} }
log_entries += '['; log_entries += std::move(line);
log_entries += time_as_str; }
log_entries += "] ";
log_entries += std::move(entry); void log(std::string entry, time_t time) {
log_raw(format_log(std::move(entry), time));
} }
void log(std::string entry) { void log(std::string entry) {
log(std::move(entry), time(nullptr)); log_raw(format_log(std::move(entry)));
} }

3
log.h
View File

@ -6,5 +6,8 @@
extern std::string log_entries; extern std::string log_entries;
std::string format_log(std::string entry, time_t time);
std::string format_log(std::string entry);
void log_raw(std::string line);
void log(std::string entry, time_t time); void log(std::string entry, time_t time);
void log(std::string entry); void log(std::string entry);