Skip to content

Commit

Permalink
engine: properly pass shutdown reason down to close log, helps to fin…
Browse files Browse the repository at this point in the history
…ally get rid of global finalmsg
  • Loading branch information
a1batross committed Jan 9, 2025
1 parent 98ff33d commit 0d55441
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 43 deletions.
1 change: 0 additions & 1 deletion engine/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions engine/common/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -1384,5 +1379,5 @@ void Host_ShutdownWithReason( const char *reason )

// restore filter
Sys_RestoreCrashHandler();
Sys_CloseLog();
Sys_CloseLog( reason );
}
50 changes: 25 additions & 25 deletions engine/common/sys_con.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions engine/common/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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
Expand Down Expand Up @@ -578,15 +578,15 @@ 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 );
exe = malloc( exelen + 1 );
wai_getExecutablePath( exe, exelen, NULL );
exe[exelen] = 0;

Host_ShutdownWithReason( "changing game" );
Host_ShutdownWithReason( finalmsg );

execv( exe, newargs );
#endif
Expand Down
7 changes: 2 additions & 5 deletions engine/common/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,16 @@ 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 );

//
// sys_con.c
//
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 );
Expand Down
2 changes: 1 addition & 1 deletion engine/platform/win32/con_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down

0 comments on commit 0d55441

Please sign in to comment.