changeset 12994:dbd0b4627e86 draft

(svn r17488) -Feature [FS#2339]: add the date to all logging in the (real, not in-game) console if show_date_in_console is set. For dedicated server binaries the default is 'on', for the rest it is 'off'.
author rubidium <rubidium@openttd.org>
date Wed, 09 Sep 2009 15:11:46 +0000
parents 90133be1c216
children 47083e652be6
files src/console.cpp src/debug.cpp src/debug.h src/settings_type.h src/table/settings.h
diffstat 5 files changed, 40 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
 {
 	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 @@
 	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
--- 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 @@
 	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 @@
 		_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 @@
 
 	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;
+}
+
--- a/src/debug.h
+++ b/src/debug.h
@@ -93,4 +93,6 @@
 void ShowInfo(const char *str);
 void CDECL ShowInfoF(const char *str, ...) WARN_FORMAT(1, 2);
 
+const char *GetLogPrefix();
+
 #endif /* DEBUG_H */
--- a/src/settings_type.h
+++ b/src/settings_type.h
@@ -102,6 +102,8 @@
 	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. */
--- a/src/table/settings.h
+++ b/src/table/settings.h
@@ -574,6 +574,13 @@
 	 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