From 585c79c7646e5a8fe6317ce471e6c9a8e13386f1 Mon Sep 17 00:00:00 2001 From: blankie Date: Mon, 16 Jan 2023 15:56:07 +0700 Subject: [PATCH] Refactor logging --- log.cpp | 22 ++++++++++++++++------ log.h | 3 +++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/log.cpp b/log.cpp index f6421d4..9e7cc42 100644 --- a/log.cpp +++ b/log.cpp @@ -7,7 +7,7 @@ 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; char time_as_str[128] = {0}; @@ -16,15 +16,25 @@ void log(std::string entry, time_t time) { 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()) { log_entries += '\n'; } - log_entries += '['; - log_entries += time_as_str; - log_entries += "] "; - log_entries += std::move(entry); + log_entries += std::move(line); +} + +void log(std::string entry, time_t time) { + log_raw(format_log(std::move(entry), time)); } void log(std::string entry) { - log(std::move(entry), time(nullptr)); + log_raw(format_log(std::move(entry))); } diff --git a/log.h b/log.h index 00c5d2a..02369f6 100644 --- a/log.h +++ b/log.h @@ -6,5 +6,8 @@ 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);