From 4411f889046f30da620bd5a03e8f1b8c7e39231d Mon Sep 17 00:00:00 2001 From: blankie Date: Mon, 16 Jan 2023 10:57:07 +0700 Subject: [PATCH] Only replace newline with spaces for logging --- event_loop.cpp | 4 ++++ log.cpp | 26 +++++++++++--------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/event_loop.cpp b/event_loop.cpp index 5b3f155..aa4d968 100644 --- a/event_loop.cpp +++ b/event_loop.cpp @@ -117,6 +117,10 @@ static inline void debug_window() { if (ImGui::Checkbox("Add log entry every second", &log_entry_every_second)) { log_entry_every_second_delta = 0.0f; } + if (ImGui::Button("Add Log Entry with Newlines")) { + log("The following should have five spaces: \"\n\n\n\n\n\""); + } + if (log_entry_every_second) { log_entry_every_second_delta += ImGui::GetIO().DeltaTime; if (log_entry_every_second_delta >= 1.0f) { diff --git a/log.cpp b/log.cpp index 8298356..06a8a45 100644 --- a/log.cpp +++ b/log.cpp @@ -31,25 +31,21 @@ static time_t current_time() { std::string log_entries; void log(std::string entry, time_t time) { - size_t last_newline_pos = 0, newline_pos; - std::string line; + size_t newline_pos; char time_as_str[128] = {0}; strftime(time_as_str, 127 * sizeof(char), "%c", localtime(&time)); - do { - newline_pos = entry.find('\n', last_newline_pos); - line = '['; - line += time_as_str; - line += "] "; - line += entry.substr(last_newline_pos, newline_pos); + while ((newline_pos = entry.find('\n')) != std::string::npos) { + entry.replace(newline_pos, 1, 1, ' '); + } - printf("%s\n", line.c_str()); - if (!log_entries.empty()) { - log_entries += '\n'; - } - log_entries += std::move(line); - last_newline_pos = newline_pos + 1; - } while (last_newline_pos); + if (!log_entries.empty()) { + log_entries += '\n'; + } + log_entries += '['; + log_entries += time_as_str; + log_entries += "] "; + log_entries += std::move(entry); } void log(std::string entry) {