From 402b38951df1c55e4c04c3103088390021fef189 Mon Sep 17 00:00:00 2001 From: Velaron Date: Mon, 13 Jun 2022 22:36:43 +0300 Subject: [PATCH] engine: better color code stripping --- engine/common/sys_con.c | 80 +++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/engine/common/sys_con.c b/engine/common/sys_con.c index 8dfb9938..ffa0db2b 100644 --- a/engine/common/sys_con.c +++ b/engine/common/sys_con.c @@ -14,7 +14,10 @@ GNU General Public License for more details. */ #include "common.h" -#if XASH_ANDROID +#if XASH_WIN32 +#define STDOUT_FILENO 1 +#include +#elif XASH_ANDROID #include #endif #include @@ -129,16 +132,18 @@ void Sys_InitLog( void ) if( s_ld.log_active ) { s_ld.logfile = fopen( s_ld.log_path, mode ); - if( !s_ld.logfile ) + + if ( !s_ld.logfile ) { Con_Reportf( S_ERROR "Sys_InitLog: can't create log file %s: %s\n", s_ld.log_path, strerror( errno ) ); + return; } - else - { - fprintf( s_ld.logfile, "=================================================================================\n" ); - fprintf( s_ld.logfile, "\t%s (build %i) started at %s\n", s_ld.title, Q_buildnum(), Q_timestamp( TIME_FULL )); - fprintf( s_ld.logfile, "=================================================================================\n" ); - } + + s_ld.logfileno = fileno( s_ld.logfile ); + + fprintf( s_ld.logfile, "=================================================================================\n" ); + fprintf( s_ld.logfile, "\t%s (build %i) started at %s\n", s_ld.title, Q_buildnum(), Q_timestamp( TIME_FULL ) ); + fprintf( s_ld.logfile, "=================================================================================\n" ); } } @@ -220,41 +225,64 @@ static void Sys_PrintColorized( const char *logtime, const char *msg ) printf( "\033[34m%s\033[0m%s\033[0m", logtime, colored ); } -void Sys_PrintLog( const char *pMsg ) +static void Sys_PrintFile( int fd, const char *logtime, const char *msg ) { - time_t crt_time; - const struct tm *crt_tm; - char logtime[32] = ""; - static char lastchar; + write( fd, logtime, Q_strlen( logtime ) ); - time( &crt_time ); - crt_tm = localtime( &crt_time ); + while ( *msg ) + { + const char *p = strchr( msg, '^' ); + + if ( p && IsColorString( p ) ) + { + msg += write( fd, msg, p - msg ); + msg += 2; + } else msg += write( fd, msg, Q_strlen( msg ) ); + } +} + +static void Sys_PrintStdout( const char *logtime, const char *msg ) +{ +#if XASH_MOBILE_PLATFORM + static char buf[MAX_PRINT_MSG]; // strip color codes - COM_StripColors( pMsg, (char *)pMsg ); + COM_StripColors( msg, buf ); // platform-specific output #if XASH_ANDROID && !XASH_DEDICATED - __android_log_print( ANDROID_LOG_DEBUG, "Xash", "%s", pMsg ); -#endif + __android_log_print( ANDROID_LOG_DEBUG, "Xash", "%s", buf ); +#endif // XASH_ANDROID && !XASH_DEDICATED #if TARGET_OS_IOS - void IOS_Log(const char*); - IOS_Log(pMsg); + void IOS_Log( const char * ); + IOS_Log( buf ); +#endif // TARGET_OS_IOS +#else // XASH_MOBILE_PLATFORM + Sys_PrintFile( STDOUT_FILENO, logtime, msg ); #endif +} + +void Sys_PrintLog( const char *pMsg ) +{ + time_t crt_time; + const struct tm *crt_tm; + char logtime[32] = ""; + static char lastchar; + + time( &crt_time ); + crt_tm = localtime( &crt_time ); if( !lastchar || lastchar == '\n') strftime( logtime, sizeof( logtime ), "[%H:%M:%S] ", crt_tm ); //short time - // spew to stdout, except mobiles -#if !XASH_MOBILE_PLATFORM + // spew to stdout #ifdef XASH_COLORIZE_CONSOLE Sys_PrintColorized( logtime, pMsg ); #else - printf( "%s%s", logtime, pMsg ); + Sys_PrintStdout( logtime, pMsg ); #endif Sys_FlushStdout(); -#endif if( !s_ld.logfile ) return; @@ -263,9 +291,9 @@ void Sys_PrintLog( const char *pMsg ) strftime( logtime, sizeof( logtime ), "[%Y:%m:%d|%H:%M:%S] ", crt_tm ); //full time // save last char to detect when line was not ended - lastchar = pMsg[strlen(pMsg)-1]; + lastchar = pMsg[Q_strlen( pMsg ) - 1]; - fprintf( s_ld.logfile, "%s%s", logtime, pMsg ); + Sys_PrintFile( s_ld.logfileno, logtime, pMsg ); Sys_FlushLogfile(); }