@ -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 ) , " %s dbg: [%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 , " %s dbg: [%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 ;
}