Skip to content

Commit

Permalink
engine: common: shut up warn-unused-result on log write functions, on…
Browse files Browse the repository at this point in the history
…ly fetch system time for system logs when necessary
  • Loading branch information
a1batross committed Dec 24, 2024
1 parent 4f98187 commit 6469b0a
Showing 1 changed file with 47 additions and 28 deletions.
75 changes: 47 additions & 28 deletions engine/common/sys_con.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ GNU General Public License for more details.
#if XASH_IRIX
#include <sys/time.h>
#endif
#include "xash3d_mathlib.h"

// do not waste precious CPU cycles on mobiles or low memory devices
#if !XASH_WIN32 && !XASH_MOBILE_PLATFORM && !XASH_LOW_MEMORY
#define XASH_COLORIZE_CONSOLE true
#define XASH_COLORIZE_CONSOLE 1
#else
#define XASH_COLORIZE_CONSOLE false
#define XASH_COLORIZE_CONSOLE 0
#endif

typedef struct {
Expand Down Expand Up @@ -148,8 +149,8 @@ void Sys_CloseLog( void )
}
}

#if XASH_COLORIZE_CONSOLE == true
static void Sys_WriteEscapeSequenceForColorcode( int fd, int c )
#if XASH_COLORIZE_CONSOLE
static qboolean Sys_WriteEscapeSequenceForColorcode( int fd, int c )
{
static const char *q3ToAnsi[ 8 ] =
{
Expand All @@ -164,19 +165,26 @@ static void Sys_WriteEscapeSequenceForColorcode( int fd, int c )
};
const char *esc = q3ToAnsi[c];

if( c == 7 )
write( fd, esc, 4 );
else write( fd, esc, 7 );
return write( fd, esc, c == 7 ? 4 : 7 ) < 0 ? false : true;
}
#else
static void Sys_WriteEscapeSequenceForColorcode( int fd, int c ) {}
static qboolean Sys_WriteEscapeSequenceForColorcode( int fd, int c )
{
return true;
}
#endif

static void Sys_PrintLogfile( const int fd, const char *logtime, const char *msg, const qboolean colorize )
static void Sys_PrintLogfile( const int fd, const char *logtime, size_t logtime_len, const char *msg, const int colorize )
{
const char *p = msg;

write( fd, logtime, Q_strlen( logtime ) );
if( logtime_len != 0 )
{
if( write( fd, logtime, logtime_len ) < 0 )
{
// not critical for us
}
}

while( p && *p )
{
Expand Down Expand Up @@ -216,7 +224,7 @@ static void Sys_PrintLogfile( const int fd, const char *logtime, const char *msg
Sys_WriteEscapeSequenceForColorcode( fd, 7 );
}

static void Sys_PrintStdout( const char *logtime, const char *msg )
static void Sys_PrintStdout( const char *logtime, size_t logtime_len, const char *msg )
{
#if XASH_MOBILE_PLATFORM
static char buf[MAX_PRINT_MSG];
Expand Down Expand Up @@ -248,7 +256,7 @@ static void Sys_PrintStdout( const char *logtime, const char *msg )
#endif

#elif !XASH_WIN32 // Wcon does the job
Sys_PrintLogfile( STDOUT_FILENO, logtime, msg, XASH_COLORIZE_CONSOLE );
Sys_PrintLogfile( STDOUT_FILENO, logtime, logtime_len, msg, XASH_COLORIZE_CONSOLE );
Sys_FlushStdout();
#endif
}
Expand All @@ -259,33 +267,44 @@ void Sys_PrintLog( const char *pMsg )
const struct tm *crt_tm;
char logtime[32] = "";
static char lastchar;
size_t len;
qboolean print_time = true;
size_t len, logtime_len = 0;

time( &crt_time );
crt_tm = localtime( &crt_time );
if( !lastchar || lastchar == '\n' )
{
if( time( &crt_time ) >= 0 )
{
crt_tm = localtime( &crt_time );
if( crt_tm == NULL )
print_time = false;
}
}
else print_time = false;

if( !lastchar || lastchar == '\n')
strftime( logtime, sizeof( logtime ), "[%H:%M:%S] ", crt_tm ); //short time
if( print_time )
{
logtime_len = strftime( logtime, sizeof( logtime ), "[%H:%M:%S] ", crt_tm ); // short time
logtime_len = Q_min( logtime_len, sizeof( logtime ) - 1 ); // just in case
}

// spew to stdout
Sys_PrintStdout( logtime, pMsg );
Sys_PrintStdout( logtime, logtime_len, pMsg );

len = Q_strlen( pMsg );

// save last char to detect when line was not ended
lastchar = len > 0 ? pMsg[len - 1] : 0;

if( !s_ld.logfile )
{
// save last char to detect when line was not ended
lastchar = len > 0 ? pMsg[len - 1] : 0;
return;
}

if( !lastchar || lastchar == '\n')
strftime( logtime, sizeof( logtime ), "[%Y:%m:%d|%H:%M:%S] ", crt_tm ); //full time
if( print_time )
{
logtime_len = strftime( logtime, sizeof( logtime ), "[%Y:%m:%d|%H:%M:%S] ", crt_tm ); //full time
logtime_len = Q_min( logtime_len, sizeof( logtime ) - 1 ); // just in case
}

// save last char to detect when line was not ended
lastchar = len > 0 ? pMsg[len - 1] : 0;

Sys_PrintLogfile( s_ld.logfileno, logtime, pMsg, false );
Sys_PrintLogfile( s_ld.logfileno, logtime, logtime_len, pMsg, false );
Sys_FlushLogfile();
}

Expand Down

0 comments on commit 6469b0a

Please sign in to comment.