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)) {
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) {

26
log.cpp
View File

@ -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) {