(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'.

pull/155/head
rubidium 15 years ago
parent 69a13877c3
commit aeb59f8e19

@ -16,6 +16,7 @@
#include "console_internal.h" #include "console_internal.h"
#include "network/network.h" #include "network/network.h"
#include "network/network_func.h" #include "network/network_func.h"
#include "debug.h"
#include <stdarg.h> #include <stdarg.h>
@ -53,7 +54,9 @@ static void IConsoleWriteToLogFile(const char *string)
{ {
if (_iconsole_output_file != NULL) { if (_iconsole_output_file != NULL) {
/* if there is an console output file ... also print it there */ /* 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) { fwrite("\n", 1, 1, _iconsole_output_file) != 1) {
fclose(_iconsole_output_file); fclose(_iconsole_output_file);
_iconsole_output_file = NULL; _iconsole_output_file = NULL;
@ -107,7 +110,7 @@ void IConsolePrint(ConsoleColour colour_code, const char *string)
str_validate(str, str + strlen(str)); str_validate(str, str + strlen(str));
if (_network_dedicated) { if (_network_dedicated) {
fprintf(stdout, "%s\n", str); fprintf(stdout, "%s%s\n", GetLogPrefix(), str);
fflush(stdout); fflush(stdout);
IConsoleWriteToLogFile(str); IConsoleWriteToLogFile(str);
free(str); // free duplicated string since it's not used anymore free(str); // free duplicated string since it's not used anymore

@ -10,13 +10,15 @@
/** @file debug.cpp Handling of printing debug messages. */ /** @file debug.cpp Handling of printing debug messages. */
#include "stdafx.h" #include "stdafx.h"
#include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "console_func.h" #include "console_func.h"
#include "debug.h" #include "debug.h"
#include "string_func.h" #include "string_func.h"
#include "network/core/core.h" #include "network/core/core.h"
#include "fileio_func.h" #include "fileio_func.h"
#include "settings_type.h"
#include <time.h>
#if defined(ENABLE_NETWORK) #if defined(ENABLE_NETWORK)
SOCKET _debug_socket = INVALID_SOCKET; SOCKET _debug_socket = INVALID_SOCKET;
@ -74,7 +76,7 @@ static void debug_print(const char *dbg, const char *buf)
if (_debug_socket != INVALID_SOCKET) { if (_debug_socket != INVALID_SOCKET) {
char buf2[1024 + 32]; 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); send(_debug_socket, buf2, (int)strlen(buf2), 0);
return; return;
} }
@ -86,14 +88,14 @@ static void debug_print(const char *dbg, const char *buf)
_sntprintf(tbuf, sizeof(tbuf), _T("%s"), OTTD2FS(dbg)); _sntprintf(tbuf, sizeof(tbuf), _T("%s"), OTTD2FS(dbg));
NKDbgPrintfW(_T("dbg: [%s] %s\n"), tbuf, OTTD2FS(buf)); NKDbgPrintfW(_T("dbg: [%s] %s\n"), tbuf, OTTD2FS(buf));
#else #else
fprintf(stderr, "dbg: [%s] %s\n", dbg, buf); fprintf(stderr, "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
#endif #endif
IConsoleDebug(dbg, buf); IConsoleDebug(dbg, buf);
} else { } else {
static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR); static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
if (f == NULL) return; if (f == NULL) return;
fprintf(f, "%s", buf); fprintf(f, "%s%s", GetLogPrefix(), buf);
fflush(f); fflush(f);
} }
} }
@ -180,3 +182,21 @@ const char *GetDebugString()
return dbgstr; 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;
}

@ -93,4 +93,6 @@ const char *GetDebugString();
void ShowInfo(const char *str); void ShowInfo(const char *str);
void CDECL ShowInfoF(const char *str, ...) WARN_FORMAT(1, 2); void CDECL ShowInfoF(const char *str, ...) WARN_FORMAT(1, 2);
const char *GetLogPrefix();
#endif /* DEBUG_H */ #endif /* DEBUG_H */

@ -102,6 +102,8 @@ struct GUISettings {
uint16 network_chat_box_width; ///< width of the chat box in pixels uint16 network_chat_box_width; ///< width of the chat box in pixels
uint8 network_chat_box_height; ///< height of the chat box in lines uint8 network_chat_box_height; ///< height of the chat box in lines
#endif #endif
bool show_date_in_logs; ///< whether to show dates in console logs
}; };
/** Settings related to currency/unit systems. */ /** Settings related to currency/unit systems. */

@ -574,6 +574,13 @@ const SettingDesc _settings[] = {
SDTC_BOOL(gui.persistent_buildingtools, S, 0, false, STR_CONFIG_SETTING_PERSISTENT_BUILDINGTOOLS, NULL), 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), 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_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), SDTC_VAR(gui.console_backlog_length, SLE_UINT16, S, 0, 100, 10, 65500, 0, STR_NULL, NULL),
#ifdef ENABLE_NETWORK #ifdef ENABLE_NETWORK

Loading…
Cancel
Save