Only replace newline with spaces for logging

This commit is contained in:
blankie 2023-01-16 10:57:07 +07:00
parent 5d4b91963c
commit 4411f88904
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
2 changed files with 15 additions and 15 deletions

View File

@ -117,6 +117,10 @@ static inline void debug_window() {
if (ImGui::Checkbox("Add log entry every second", &log_entry_every_second)) { if (ImGui::Checkbox("Add log entry every second", &log_entry_every_second)) {
log_entry_every_second_delta = 0.0f; 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) { if (log_entry_every_second) {
log_entry_every_second_delta += ImGui::GetIO().DeltaTime; log_entry_every_second_delta += ImGui::GetIO().DeltaTime;
if (log_entry_every_second_delta >= 1.0f) { if (log_entry_every_second_delta >= 1.0f) {

26
log.cpp
View File

@ -31,25 +31,21 @@ static time_t current_time() {
std::string log_entries; std::string log_entries;
void log(std::string entry, time_t time) { void log(std::string entry, time_t time) {
size_t last_newline_pos = 0, newline_pos; size_t newline_pos;
std::string line;
char time_as_str[128] = {0}; char time_as_str[128] = {0};
strftime(time_as_str, 127 * sizeof(char), "%c", localtime(&time)); strftime(time_as_str, 127 * sizeof(char), "%c", localtime(&time));
do { while ((newline_pos = entry.find('\n')) != std::string::npos) {
newline_pos = entry.find('\n', last_newline_pos); entry.replace(newline_pos, 1, 1, ' ');
line = '['; }
line += time_as_str;
line += "] ";
line += entry.substr(last_newline_pos, newline_pos);
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;
last_newline_pos = newline_pos + 1; log_entries += "] ";
} while (last_newline_pos); log_entries += std::move(entry);
} }
void log(std::string entry) { void log(std::string entry) {