diff options
-rw-r--r-- | src/console.cpp | 7 | ||||
-rw-r--r-- | src/debug.cpp | 28 | ||||
-rw-r--r-- | src/debug.h | 2 | ||||
-rw-r--r-- | src/settings_type.h | 2 | ||||
-rw-r--r-- | src/table/settings.h | 7 |
5 files changed, 40 insertions, 6 deletions
diff --git a/src/console.cpp b/src/console.cpp index 821d5bf62..e1913071f 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -16,6 +16,7 @@ #include "console_internal.h" #include "network/network.h" #include "network/network_func.h" +#include "debug.h" #include <stdarg.h> @@ -53,7 +54,9 @@ static void IConsoleWriteToLogFile(const char *string) { if (_iconsole_output_file != NULL) { /* if there is an console output file ... also print it there */ - if (fwrite(string, strlen(string), 1, _iconsole_output_file) != 1 || + const char *header = GetLogPrefix(); + if (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) { fclose(_iconsole_output_file); _iconsole_output_file = NULL; @@ -107,7 +110,7 @@ void IConsolePrint(ConsoleColour colour_code, const char *string) str_validate(str, str + strlen(str)); if (_network_dedicated) { - fprintf(stdout, "%s\n", str); + fprintf(stdout, "%s%s\n", 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 f024aede8..fcf716255 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -10,13 +10,15 @@ /** @file debug.cpp Handling of printing debug messages. */ #include "stdafx.h" -#include <stdio.h> #include <stdarg.h> #include "console_func.h" #include "debug.h" #include "string_func.h" #include "network/core/core.h" #include "fileio_func.h" +#include "settings_type.h" + +#include <time.h> #if defined(ENABLE_NETWORK) SOCKET _debug_socket = INVALID_SOCKET; @@ -74,7 +76,7 @@ static void debug_print(const char *dbg, const char *buf) if (_debug_socket != INVALID_SOCKET) { char buf2[1024 + 32]; - snprintf(buf2, lengthof(buf2), "dbg: [%s] %s\n", dbg, buf); + snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf); send(_debug_socket, buf2, (int)strlen(buf2), 0); return; } @@ -86,14 +88,14 @@ static void debug_print(const char *dbg, const char *buf) _sntprintf(tbuf, sizeof(tbuf), _T("%s"), OTTD2FS(dbg)); NKDbgPrintfW(_T("dbg: [%s] %s\n"), tbuf, OTTD2FS(buf)); #else - fprintf(stderr, "dbg: [%s] %s\n", dbg, buf); + fprintf(stderr, "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf); #endif IConsoleDebug(dbg, buf); } else { static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR); if (f == NULL) return; - fprintf(f, "%s", buf); + fprintf(f, "%s%s", GetLogPrefix(), buf); fflush(f); } } @@ -180,3 +182,21 @@ const char *GetDebugString() return dbgstr; } + +/** + * Get the prefix for logs; if show_date_in_logs is enabled it returns + * the date, otherwise it returns nothing. + * @return the prefix for logs (do not free), never NULL + */ +const char *GetLogPrefix() +{ + static char _log_prefix[24]; + if (_settings_client.gui.show_date_in_logs) { + time_t cur_time = time(NULL); + strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time)); + } else { + *_log_prefix = '\0'; + } + return _log_prefix; +} + diff --git a/src/debug.h b/src/debug.h index f3c9b4199..8bea36330 100644 --- a/src/debug.h +++ b/src/debug.h @@ -93,4 +93,6 @@ const char *GetDebugString(); void ShowInfo(const char *str); void CDECL ShowInfoF(const char *str, ...) WARN_FORMAT(1, 2); +const char *GetLogPrefix(); + #endif /* DEBUG_H */ diff --git a/src/settings_type.h b/src/settings_type.h index 3b74c5d1b..fb986adf9 100644 --- a/src/settings_type.h +++ b/src/settings_type.h @@ -102,6 +102,8 @@ struct GUISettings { uint16 network_chat_box_width; ///< width of the chat box in pixels uint8 network_chat_box_height; ///< height of the chat box in lines #endif + + bool show_date_in_logs; ///< whether to show dates in console logs }; /** Settings related to currency/unit systems. */ diff --git a/src/table/settings.h b/src/table/settings.h index 1f832dbc6..ceebac0ea 100644 --- a/src/table/settings.h +++ b/src/table/settings.h @@ -574,6 +574,13 @@ const SettingDesc _settings[] = { SDTC_BOOL(gui.persistent_buildingtools, S, 0, false, STR_CONFIG_SETTING_PERSISTENT_BUILDINGTOOLS, NULL), SDTC_BOOL(gui.expenses_layout, S, 0, false, STR_CONFIG_SETTING_EXPENSES_LAYOUT, RedrawScreen), +/* For the dedicated build we'll enable dates in logs by default. */ +#ifdef DEDICATED + SDTC_BOOL(gui.show_date_in_logs, S, 0, true, STR_NULL, NULL), +#else + SDTC_BOOL(gui.show_date_in_logs, S, 0, false, STR_NULL, NULL), +#endif + SDTC_VAR(gui.console_backlog_timeout, SLE_UINT16, S, 0, 100, 10, 65500, 0, STR_NULL, NULL), SDTC_VAR(gui.console_backlog_length, SLE_UINT16, S, 0, 100, 10, 65500, 0, STR_NULL, NULL), #ifdef ENABLE_NETWORK |