engine: ensure all data was written to stdout when exiting

This commit is contained in:
Alibek Omarov 2022-02-22 06:08:32 +03:00
parent 9917cba8fc
commit 18933e7981

View File

@ -97,6 +97,20 @@ int Sys_LogFileNo( void )
return s_ld.logfileno; return s_ld.logfileno;
} }
static void Sys_FlushStdout( void )
{
// never printing anything to stdout on mobiles
#if !XASH_MOBILE_PLATFORM
fflush( stdout );
#endif
}
static void Sys_FlushLogfile( void )
{
if( s_ld.logfile )
fflush( s_ld.logfile );
}
void Sys_InitLog( void ) void Sys_InitLog( void )
{ {
const char *mode; const char *mode;
@ -147,6 +161,8 @@ void Sys_CloseLog( void )
break; break;
} }
Sys_FlushStdout(); // flush to stdout to ensure all data was written
if( s_ld.logfile ) if( s_ld.logfile )
{ {
fprintf( s_ld.logfile, "\n"); fprintf( s_ld.logfile, "\n");
@ -160,33 +176,11 @@ void Sys_CloseLog( void )
} }
} }
void Sys_PrintLog( const char *pMsg ) static void Sys_PrintColorized( const char *logtime, const char *msg )
{ {
time_t crt_time;
const struct tm *crt_tm;
char logtime[32] = "";
static char lastchar;
time( &crt_time );
crt_tm = localtime( &crt_time );
#if XASH_ANDROID && !XASH_DEDICATED
__android_log_print( ANDROID_LOG_DEBUG, "Xash", "%s", pMsg );
#endif
#if TARGET_OS_IOS
void IOS_Log(const char*);
IOS_Log(pMsg);
#endif
if( !lastchar || lastchar == '\n')
strftime( logtime, sizeof( logtime ), "[%H:%M:%S] ", crt_tm ); //short time
#ifdef XASH_COLORIZE_CONSOLE
{
char colored[4096]; char colored[4096];
const char *msg = pMsg;
int len = 0; int len = 0;
while( *msg && ( len < 4090 ) ) while( *msg && ( len < 4090 ) )
{ {
static char q3ToAnsi[ 8 ] = static char q3ToAnsi[ 8 ] =
@ -222,14 +216,41 @@ void Sys_PrintLog( const char *pMsg )
colored[len++] = *msg++; colored[len++] = *msg++;
} }
colored[len] = 0; colored[len] = 0;
printf( "\033[34m%s\033[0m%s\033[0m", logtime, colored );
} printf( "\033[34m%s\033[0m%s\033[0m", logtime, colored );
#else }
#if !XASH_ANDROID || XASH_DEDICATED
printf( "%s %s", logtime, pMsg ); void Sys_PrintLog( const char *pMsg )
fflush( stdout ); {
time_t crt_time;
const struct tm *crt_tm;
char logtime[32] = "";
static char lastchar;
time( &crt_time );
crt_tm = localtime( &crt_time );
// platform-specific output
#if XASH_ANDROID && !XASH_DEDICATED
__android_log_print( ANDROID_LOG_DEBUG, "Xash", "%s", pMsg );
#endif #endif
#if TARGET_OS_IOS
void IOS_Log(const char*);
IOS_Log(pMsg);
#endif
if( !lastchar || lastchar == '\n')
strftime( logtime, sizeof( logtime ), "[%H:%M:%S] ", crt_tm ); //short time
// spew to stdout, except mobiles
#if !XASH_MOBILE_PLATFORM
#ifdef XASH_COLORIZE_CONSOLE
Sys_PrintColorized( logtime, pMsg );
#else
printf( "%s %s", logtime, pMsg );
#endif
Sys_FlushStdout();
#endif #endif
// save last char to detect when line was not ended // save last char to detect when line was not ended
@ -242,7 +263,7 @@ void Sys_PrintLog( const char *pMsg )
strftime( logtime, sizeof( logtime ), "[%Y:%m:%d|%H:%M:%S]", crt_tm ); //full time strftime( logtime, sizeof( logtime ), "[%Y:%m:%d|%H:%M:%S]", crt_tm ); //full time
fprintf( s_ld.logfile, "%s %s", logtime, pMsg ); fprintf( s_ld.logfile, "%s %s", logtime, pMsg );
fflush( s_ld.logfile ); Sys_FlushLogfile();
} }
/* /*