From 0d554418303744f99fbe4a340a7c68f39a8f014b Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 9 Jan 2025 08:04:18 +0300 Subject: [PATCH] engine: properly pass shutdown reason down to close log, helps to finally get rid of global finalmsg --- engine/common/common.h | 1 - engine/common/host.c | 9 ++---- engine/common/sys_con.c | 50 ++++++++++++++++----------------- engine/common/system.c | 8 +++--- engine/common/system.h | 7 ++--- engine/platform/win32/con_win.c | 2 +- 6 files changed, 34 insertions(+), 43 deletions(-) diff --git a/engine/common/common.h b/engine/common/common.h index db6961597f..4a3066b694 100644 --- a/engine/common/common.h +++ b/engine/common/common.h @@ -301,7 +301,6 @@ typedef struct host_parm_s poolhandle_t mempool; // static mempool for misc allocations poolhandle_t imagepool; // imagelib mempool poolhandle_t soundpool; // soundlib mempool - string finalmsg; // server shutdown final message string downloadfile; // filename to be downloading int downloadcount; // how many files remain to downloading char deferred_cmd[128];// deferred commands diff --git a/engine/common/host.c b/engine/common/host.c index 0fd3ccdacd..c4d814cf38 100644 --- a/engine/common/host.c +++ b/engine/common/host.c @@ -362,9 +362,8 @@ static void Host_NewInstance( const char *name, const char *finalmsg ) if( !pChangeGame ) return; host.change_game = true; - Q_strncpy( host.finalmsg, finalmsg, sizeof( host.finalmsg )); - if( !Sys_NewInstance( name )) + if( !Sys_NewInstance( name, finalmsg )) pChangeGame( name ); // call from hl.exe } @@ -838,7 +837,6 @@ void GAME_EXPORT Host_Error( const char *error, ... ) recursive = true; Q_strncpy( hosterror2, hosterror1, sizeof( hosterror2 )); host.errorframe = host.framecount; // to avoid multply calls per frame - Q_snprintf( host.finalmsg, sizeof( host.finalmsg ), "Server crashed: %s", hosterror1 ); // clearing cmd buffer to prevent execute any commands COM_InitHostState(); @@ -1359,9 +1357,6 @@ void Host_ShutdownWithReason( const char *reason ) if( host.status != HOST_ERR_FATAL ) host.status = HOST_SHUTDOWN; // prepare host to normal shutdown - if( !host.change_game ) - Q_strncpy( host.finalmsg, "Server shutdown", sizeof( host.finalmsg )); - #if !XASH_DEDICATED if( host.type == HOST_NORMAL && !error ) Host_WriteConfig(); @@ -1384,5 +1379,5 @@ void Host_ShutdownWithReason( const char *reason ) // restore filter Sys_RestoreCrashHandler(); - Sys_CloseLog(); + Sys_CloseLog( reason ); } diff --git a/engine/common/sys_con.c b/engine/common/sys_con.c index 4483a0452f..316e0f70ee 100644 --- a/engine/common/sys_con.c +++ b/engine/common/sys_con.c @@ -116,37 +116,37 @@ void Sys_InitLog( void ) } } -void Sys_CloseLog( void ) +void Sys_CloseLog( const char *finalmsg ) { - char event_name[64]; + Sys_FlushStdout(); // flush to stdout to ensure all data was written + + if( !s_ld.logfile ) + return; // continue logged - switch( host.status ) + if( !finalmsg ) { - case HOST_CRASHED: - Q_strncpy( event_name, "crashed", sizeof( event_name )); - break; - case HOST_ERR_FATAL: - Q_strncpy( event_name, "stopped with error", sizeof( event_name )); - break; - default: - if( !host.change_game ) Q_strncpy( event_name, "stopped", sizeof( event_name )); - else Q_strncpy( event_name, host.finalmsg, sizeof( event_name )); - break; + switch( host.status ) + { + case HOST_CRASHED: + finalmsg = "crashed"; + break; + case HOST_ERR_FATAL: + finalmsg = "stopped with error"; + break; + default: + finalmsg = "stopped"; + break; + } } - Sys_FlushStdout(); // flush to stdout to ensure all data was written - - if( s_ld.logfile ) - { - fputc( '\n', s_ld.logfile ); - fputs( "================================================================================\n", s_ld.logfile ); - fprintf( s_ld.logfile, "%s (%i, %s, %s, %s-%s)\n", s_ld.title, Q_buildnum(), Q_buildcommit(), Q_buildbranch(), Q_buildos(), Q_buildarch()); - fprintf( s_ld.logfile, "Stopped with reason \"%s\" at %s\n", event_name, Q_timestamp( TIME_FULL )); - fputs( "================================================================================\n", s_ld.logfile ); - fclose( s_ld.logfile ); - s_ld.logfile = NULL; - } + fputc( '\n', s_ld.logfile ); + fputs( "================================================================================\n", s_ld.logfile ); + fprintf( s_ld.logfile, "%s (%i, %s, %s, %s-%s)\n", s_ld.title, Q_buildnum(), Q_buildcommit(), Q_buildbranch(), Q_buildos(), Q_buildarch()); + fprintf( s_ld.logfile, "Stopped with reason \"%s\" at %s\n", finalmsg, Q_timestamp( TIME_FULL )); + fputs( "================================================================================\n", s_ld.logfile ); + fclose( s_ld.logfile ); + s_ld.logfile = NULL; } #if XASH_COLORIZE_CONSOLE diff --git a/engine/common/system.c b/engine/common/system.c index ca781f3572..46cf816cfb 100644 --- a/engine/common/system.c +++ b/engine/common/system.c @@ -529,7 +529,7 @@ but since engine will be unloaded during this call it explicitly doesn't use internal allocation or string copy utils ================== */ -qboolean Sys_NewInstance( const char *gamedir ) +qboolean Sys_NewInstance( const char *gamedir, const char *finalmsg ) { #if XASH_NSWITCH char newargs[4096]; @@ -540,7 +540,7 @@ qboolean Sys_NewInstance( const char *gamedir ) // just restart the entire thing printf( "envSetNextLoad exe: `%s`\n", exe ); printf( "envSetNextLoad argv:\n`%s`\n", newargs ); - Host_ShutdownWithReason( "changing game" ); + Host_ShutdownWithReason( finalmsg ); envSetNextLoad( exe, newargs ); exit( 0 ); #else @@ -578,7 +578,7 @@ qboolean Sys_NewInstance( const char *gamedir ) #if XASH_PSVITA // under normal circumstances it's always going to be the same path exe = strdup( "app0:/eboot.bin" ); - Host_ShutdownWithReason( "changing game" ); + Host_ShutdownWithReason( finalmsg ); sceAppMgrLoadExec( exe, newargs, NULL ); #else exelen = wai_getExecutablePath( NULL, 0, NULL ); @@ -586,7 +586,7 @@ qboolean Sys_NewInstance( const char *gamedir ) wai_getExecutablePath( exe, exelen, NULL ); exe[exelen] = 0; - Host_ShutdownWithReason( "changing game" ); + Host_ShutdownWithReason( finalmsg ); execv( exe, newargs ); #endif diff --git a/engine/common/system.h b/engine/common/system.h index ac18890178..401e3e780c 100644 --- a/engine/common/system.h +++ b/engine/common/system.h @@ -56,11 +56,8 @@ void Sys_DebugBreak( void ); qboolean _Sys_GetParmFromCmdLine( const char *parm, char *out, size_t size ); qboolean Sys_GetIntFromCmdLine( const char *parm, int *out ); void Sys_Print( const char *pMsg ); -void Sys_PrintLog( const char *pMsg ); -void Sys_InitLog( void ); -void Sys_CloseLog( void ); void Sys_Quit( const char *reason ) NORETURN; -qboolean Sys_NewInstance( const char *gamedir ); +qboolean Sys_NewInstance( const char *gamedir, const char *finalmsg ); void *Sys_GetNativeObject( const char *obj ); // @@ -68,7 +65,7 @@ void *Sys_GetNativeObject( const char *obj ); // char *Sys_Input( void ); void Sys_DestroyConsole( void ); -void Sys_CloseLog( void ); +void Sys_CloseLog( const char *finalmsg ); void Sys_InitLog( void ); void Sys_PrintLog( const char *pMsg ); int Sys_LogFileNo( void ); diff --git a/engine/platform/win32/con_win.c b/engine/platform/win32/con_win.c index f7559346df..da38ad8124 100644 --- a/engine/platform/win32/con_win.c +++ b/engine/platform/win32/con_win.c @@ -588,7 +588,7 @@ void Wcon_DestroyConsole( void ) // last text message into console or log Con_Reportf( "%s: Unloading xash.dll\n", __func__ ); - Sys_CloseLog(); + Sys_CloseLog( NULL ); if( !s_wcd.attached ) {