Browse Source

engine: ensure all data was written to stdout when exiting

pull/2/head
Alibek Omarov 3 years ago
parent
commit
18933e7981
  1. 113
      engine/common/sys_con.c

113
engine/common/sys_con.c

@ -97,6 +97,20 @@ int Sys_LogFileNo( void ) @@ -97,6 +97,20 @@ int Sys_LogFileNo( void )
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 )
{
const char *mode;
@ -147,6 +161,8 @@ void Sys_CloseLog( void ) @@ -147,6 +161,8 @@ void Sys_CloseLog( void )
break;
}
Sys_FlushStdout(); // flush to stdout to ensure all data was written
if( s_ld.logfile )
{
fprintf( s_ld.logfile, "\n");
@ -160,6 +176,50 @@ void Sys_CloseLog( void ) @@ -160,6 +176,50 @@ void Sys_CloseLog( void )
}
}
static void Sys_PrintColorized( const char *logtime, const char *msg )
{
char colored[4096];
int len = 0;
while( *msg && ( len < 4090 ) )
{
static char q3ToAnsi[ 8 ] =
{
'0', // COLOR_BLACK
'1', // COLOR_RED
'2', // COLOR_GREEN
'3', // COLOR_YELLOW
'4', // COLOR_BLUE
'6', // COLOR_CYAN
'5', // COLOR_MAGENTA
0 // COLOR_WHITE
};
if( IsColorString( msg ) )
{
int color;
msg++;
color = q3ToAnsi[ *msg++ % 8 ];
colored[len++] = '\033';
colored[len++] = '[';
if( color )
{
colored[len++] = '3';
colored[len++] = color;
}
else
colored[len++] = '0';
colored[len++] = 'm';
}
else
colored[len++] = *msg++;
}
colored[len] = 0;
printf( "\033[34m%s\033[0m%s\033[0m", logtime, colored );
}
void Sys_PrintLog( const char *pMsg )
{
time_t crt_time;
@ -169,6 +229,8 @@ void Sys_PrintLog( const char *pMsg ) @@ -169,6 +229,8 @@ void Sys_PrintLog( const char *pMsg )
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
@ -178,58 +240,17 @@ void Sys_PrintLog( const char *pMsg ) @@ -178,58 +240,17 @@ void Sys_PrintLog( const char *pMsg )
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
{
char colored[4096];
const char *msg = pMsg;
int len = 0;
while( *msg && ( len < 4090 ) )
{
static char q3ToAnsi[ 8 ] =
{
'0', // COLOR_BLACK
'1', // COLOR_RED
'2', // COLOR_GREEN
'3', // COLOR_YELLOW
'4', // COLOR_BLUE
'6', // COLOR_CYAN
'5', // COLOR_MAGENTA
0 // COLOR_WHITE
};
if( IsColorString( msg ) )
{
int color;
msg++;
color = q3ToAnsi[ *msg++ % 8 ];
colored[len++] = '\033';
colored[len++] = '[';
if( color )
{
colored[len++] = '3';
colored[len++] = color;
}
else
colored[len++] = '0';
colored[len++] = 'm';
}
else
colored[len++] = *msg++;
}
colored[len] = 0;
printf( "\033[34m%s\033[0m%s\033[0m", logtime, colored );
}
Sys_PrintColorized( logtime, pMsg );
#else
#if !XASH_ANDROID || XASH_DEDICATED
printf( "%s %s", logtime, pMsg );
fflush( stdout );
#endif
Sys_FlushStdout();
#endif
// save last char to detect when line was not ended
@ -242,7 +263,7 @@ void Sys_PrintLog( const char *pMsg ) @@ -242,7 +263,7 @@ void Sys_PrintLog( const char *pMsg )
strftime( logtime, sizeof( logtime ), "[%Y:%m:%d|%H:%M:%S]", crt_tm ); //full time
fprintf( s_ld.logfile, "%s %s", logtime, pMsg );
fflush( s_ld.logfile );
Sys_FlushLogfile();
}
/*

Loading…
Cancel
Save