diff --git a/src/console.cpp b/src/console.cpp index fb6c939b62..12f465793b 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -53,7 +53,8 @@ static void IConsoleWriteToLogFile(const char *string) { if (_iconsole_output_file != nullptr) { /* if there is an console output file ... also print it there */ - const char *header = GetLogPrefix(); + log_prefix prefix_writer; + const char *header = prefix_writer.GetLogPrefix(); if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) || fwrite(string, strlen(string), 1, _iconsole_output_file) != 1 || fwrite("\n", 1, 1, _iconsole_output_file) != 1) { @@ -115,7 +116,7 @@ void IConsolePrint(TextColour colour_code, const char *string) if (_network_dedicated) { NetworkAdminConsole("console", str); - fprintf(stdout, "%s%s\n", GetLogPrefix(), str); + fprintf(stdout, "%s%s\n", log_prefix().GetLogPrefix(), str); fflush(stdout); IConsoleWriteToLogFile(str); free(str); // free duplicated string since it's not used anymore diff --git a/src/debug.cpp b/src/debug.cpp index 07d47313af..b95cf1cdc0 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -143,7 +143,7 @@ void debug_print(const char *dbg, const char *buf) if (_debug_socket != INVALID_SOCKET) { char buf2[1024 + 32]; - seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf); + seprintf(buf2, lastof(buf2), "%sdbg: [%s] %s\n", log_prefix().GetLogPrefix(), dbg, buf); /* Prevent sending a message concurrently, as that might cause interleaved messages. */ static std::mutex _debug_socket_mutex; @@ -157,7 +157,7 @@ void debug_print(const char *dbg, const char *buf) if (strcmp(dbg, "desync") == 0) { static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR); if (f != nullptr) { - fprintf(f, "%s%s\n", GetLogPrefix(), buf); + fprintf(f, "%s%s\n", log_prefix().GetLogPrefix(), buf); fflush(f); } #ifdef RANDOM_DEBUG @@ -191,7 +191,7 @@ void debug_print(const char *dbg, const char *buf) } char buffer[512]; - seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf); + seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", log_prefix().GetLogPrefix(), dbg, buf); str_strip_colours(buffer); @@ -323,15 +323,14 @@ std::string GetDebugString() * the date, otherwise it returns nothing. * @return the prefix for logs (do not free), never nullptr */ -const char *GetLogPrefix() +const char *log_prefix::GetLogPrefix() { - static char _log_prefix[24]; if (_settings_client.gui.show_date_in_logs) { - LocalTime::Format(_log_prefix, lastof(_log_prefix), "[%Y-%m-%d %H:%M:%S] "); + LocalTime::Format(this->buffer, lastof(this->buffer), "[%Y-%m-%d %H:%M:%S] "); } else { - *_log_prefix = '\0'; + this->buffer[0] = '\0'; } - return _log_prefix; + return this->buffer; } struct DesyncMsgLogEntry { diff --git a/src/debug.h b/src/debug.h index 4ea8a08187..75b5aa613c 100644 --- a/src/debug.h +++ b/src/debug.h @@ -131,6 +131,13 @@ std::string GetDebugString(); void ShowInfo(const char *str); void CDECL ShowInfoF(const char *str, ...) WARN_FORMAT(1, 2); +struct log_prefix { + const char *GetLogPrefix(); + +private: + char buffer[24]; +}; + const char *GetLogPrefix(); void ClearDesyncMsgLog();