2018-04-14 03:08:28 +02:00
/*
sys_con . c - stdout and log
Copyright ( C ) 2007 Uncle Mike
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
*/
# include "common.h"
2022-06-13 21:36:43 +02:00
# if XASH_WIN32
# define STDOUT_FILENO 1
# include <io.h>
# elif XASH_ANDROID
2018-04-14 03:08:28 +02:00
# include <android/log.h>
# endif
2022-01-29 01:04:54 +01:00
# include <string.h>
# include <errno.h>
2023-01-14 07:35:30 +01:00
# if XASH_IRIX
# include <sys/time.h>
# endif
2018-04-14 03:08:28 +02:00
2022-06-14 03:23:09 +02:00
// 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
2018-04-22 12:07:40 +02:00
// use with caution, running engine in Qt Creator may cause a freeze in read() call
2023-06-30 00:44:03 +02:00
// I have never encountered this bug anywhere else, so still enable by default
# define XASH_USE_SELECT 1
2022-06-14 03:23:09 +02:00
# else
# define XASH_COLORIZE_CONSOLE false
2018-04-14 03:08:28 +02:00
# endif
2021-03-27 23:57:40 +01:00
# if XASH_USE_SELECT
2018-04-14 03:08:28 +02:00
// non-blocking console input
# include <sys/select.h>
# endif
typedef struct {
char title [ 64 ] ;
qboolean log_active ;
char log_path [ MAX_SYSPATH ] ;
FILE * logfile ;
int logfileno ;
} LogData ;
static LogData s_ld ;
char * Sys_Input ( void )
{
2021-03-27 23:57:40 +01:00
# if XASH_USE_SELECT
2023-06-30 00:44:03 +02:00
if ( Host_IsDedicated ( ) )
2018-04-14 03:08:28 +02:00
{
fd_set rfds ;
static char line [ 1024 ] ;
static int len ;
struct timeval tv ;
tv . tv_sec = 0 ;
tv . tv_usec = 0 ;
2018-04-22 12:07:40 +02:00
FD_ZERO ( & rfds ) ;
FD_SET ( 0 , & rfds ) ; // stdin
while ( select ( 1 , & rfds , NULL , NULL , & tv ) > 0 )
2018-04-14 03:08:28 +02:00
{
if ( read ( 0 , & line [ len ] , 1 ) ! = 1 )
break ;
if ( line [ len ] = = ' \n ' | | len > 1022 )
{
line [ + + len ] = 0 ;
len = 0 ;
return line ;
}
len + + ;
tv . tv_sec = 0 ;
tv . tv_usec = 0 ;
}
}
# endif
2019-11-24 01:52:08 +01:00
# if XASH_WIN32
2018-04-14 03:08:28 +02:00
return Wcon_Input ( ) ;
# endif
return NULL ;
}
void Sys_DestroyConsole ( void )
{
// last text message into console or log
2018-10-27 23:39:29 +02:00
Con_Reportf ( " Sys_DestroyConsole: Exiting! \n " ) ;
2019-11-24 01:52:08 +01:00
# if XASH_WIN32
2018-04-14 03:08:28 +02:00
Wcon_DestroyConsole ( ) ;
# endif
}
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
SYSTEM LOG
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
2018-05-14 09:14:41 +02:00
int Sys_LogFileNo ( void )
{
return s_ld . logfileno ;
}
2022-02-22 04:08:32 +01:00
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 ) ;
}
2018-04-14 03:08:28 +02:00
void Sys_InitLog ( void )
{
const char * mode ;
2018-05-03 18:51:23 +02:00
if ( Sys_CheckParm ( " -log " ) & & host . allow_console ! = 0 )
{
s_ld . log_active = true ;
Q_strncpy ( s_ld . log_path , " engine.log " , sizeof ( s_ld . log_path ) ) ;
}
2018-04-14 03:08:28 +02:00
if ( host . change_game & & host . type ! = HOST_DEDICATED )
mode = " a " ;
else mode = " w " ;
// create log if needed
if ( s_ld . log_active )
{
s_ld . logfile = fopen ( s_ld . log_path , mode ) ;
2022-06-13 21:36:43 +02:00
if ( ! s_ld . logfile )
2022-01-29 01:04:00 +01:00
{
2022-01-29 01:04:54 +01:00
Con_Reportf ( S_ERROR " Sys_InitLog: can't create log file %s: %s \n " , s_ld . log_path , strerror ( errno ) ) ;
2022-06-13 21:36:43 +02:00
return ;
2022-01-29 01:04:00 +01:00
}
2022-06-13 21:36:43 +02:00
s_ld . logfileno = fileno ( s_ld . logfile ) ;
fprintf ( s_ld . logfile , " ================================================================================= \n " ) ;
2023-10-22 15:23:15 +02:00
fprintf ( s_ld . logfile , " \t %s (build %i commit %s (%s-%s)) started at %s \n " , s_ld . title , Q_buildnum ( ) , Q_buildcommit ( ) , Q_buildos ( ) , Q_buildarch ( ) , Q_timestamp ( TIME_FULL ) ) ;
2022-06-13 21:36:43 +02:00
fprintf ( s_ld . logfile , " ================================================================================= \n " ) ;
2018-04-14 03:08:28 +02:00
}
}
void Sys_CloseLog ( void )
{
char event_name [ 64 ] ;
// continue logged
switch ( host . status )
{
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 ;
}
2022-02-22 04:08:32 +01:00
Sys_FlushStdout ( ) ; // flush to stdout to ensure all data was written
2018-04-14 03:08:28 +02:00
if ( s_ld . logfile )
{
fprintf ( s_ld . logfile , " \n " ) ;
fprintf ( s_ld . logfile , " ================================================================================= " ) ;
if ( host . change_game ) fprintf ( s_ld . logfile , " \n \t %s (build %i) %s \n " , s_ld . title , Q_buildnum ( ) , event_name ) ;
else fprintf ( s_ld . logfile , " \n \t %s (build %i) % s at % s \ n " , s_ld.title, Q_buildnum(), event_name, Q_timestamp( TIME_FULL )) ;
2018-05-03 18:51:23 +02:00
fprintf ( s_ld . logfile , " ================================================================================= \n " ) ;
2018-04-14 03:08:28 +02:00
fclose ( s_ld . logfile ) ;
s_ld . logfile = NULL ;
}
}
2022-06-14 03:23:09 +02:00
# if XASH_COLORIZE_CONSOLE == true
static void Sys_WriteEscapeSequenceForColorcode ( int fd , int c )
2022-02-22 04:08:32 +01:00
{
2022-06-14 03:23:09 +02:00
static const char * q3ToAnsi [ 8 ] =
2022-02-22 04:08:32 +01:00
{
2022-06-14 03:23:09 +02:00
" \033 [30m " , // COLOR_BLACK
" \033 [31m " , // COLOR_RED
" \033 [32m " , // COLOR_GREEN
" \033 [33m " , // COLOR_YELLOW
" \033 [34m " , // COLOR_BLUE
" \033 [36m " , // COLOR_CYAN
" \033 [35m " , // COLOR_MAGENTA
" \033 [0m " , // COLOR_WHITE
} ;
const char * esc = q3ToAnsi [ c ] ;
if ( c = = 7 )
write ( fd , esc , 4 ) ;
else write ( fd , esc , 5 ) ;
2022-02-22 04:08:32 +01:00
}
2022-06-14 03:23:09 +02:00
# else
static void Sys_WriteEscapeSequenceForColorcode ( int fd , int c ) { }
# endif
2022-02-22 04:08:32 +01:00
2022-06-14 03:23:09 +02:00
static void Sys_PrintLogfile ( const int fd , const char * logtime , const char * msg , const qboolean colorize )
2018-04-14 03:08:28 +02:00
{
2022-06-14 02:27:08 +02:00
const char * p = msg ;
2022-06-13 21:36:43 +02:00
write ( fd , logtime , Q_strlen ( logtime ) ) ;
2018-04-14 03:08:28 +02:00
2022-06-14 02:27:08 +02:00
while ( p & & * p )
2022-06-13 21:36:43 +02:00
{
2022-06-14 02:27:08 +02:00
p = Q_strchr ( msg , ' ^ ' ) ;
2022-06-13 21:36:43 +02:00
2022-06-14 02:27:08 +02:00
if ( p = = NULL )
{
write ( fd , msg , Q_strlen ( msg ) ) ;
break ;
}
else if ( IsColorString ( p ) )
2022-06-13 21:36:43 +02:00
{
2022-06-14 02:27:08 +02:00
if ( p ! = msg )
write ( fd , msg , p - msg ) ;
msg = p + 2 ;
2022-06-14 03:23:09 +02:00
if ( colorize )
Sys_WriteEscapeSequenceForColorcode ( fd , ColorIndex ( p [ 1 ] ) ) ;
2022-06-14 02:27:08 +02:00
}
else
{
write ( fd , msg , p - msg + 1 ) ;
msg = p + 1 ;
}
2022-06-13 21:36:43 +02:00
}
2022-06-14 03:23:09 +02:00
// flush the color
if ( colorize )
Sys_WriteEscapeSequenceForColorcode ( fd , 7 ) ;
2022-06-13 21:36:43 +02:00
}
static void Sys_PrintStdout ( const char * logtime , const char * msg )
{
# if XASH_MOBILE_PLATFORM
static char buf [ MAX_PRINT_MSG ] ;
2022-02-22 04:08:32 +01:00
2022-06-10 10:20:58 +02:00
// strip color codes
2022-06-13 21:36:43 +02:00
COM_StripColors ( msg , buf ) ;
2022-06-10 10:20:58 +02:00
2022-02-22 04:08:32 +01:00
// platform-specific output
2019-09-19 16:08:16 +02:00
# if XASH_ANDROID && !XASH_DEDICATED
2022-06-14 03:23:09 +02:00
__android_log_write ( ANDROID_LOG_DEBUG , " Xash " , buf ) ;
2022-06-13 21:36:43 +02:00
# endif // XASH_ANDROID && !XASH_DEDICATED
2018-04-14 03:08:28 +02:00
# if TARGET_OS_IOS
2022-06-13 21:36:43 +02:00
void IOS_Log ( const char * ) ;
IOS_Log ( buf ) ;
# endif // TARGET_OS_IOS
2023-02-05 02:09:32 +01:00
2023-03-24 00:25:11 +01:00
# if XASH_NSWITCH && NSWITCH_DEBUG
2023-02-05 02:09:32 +01:00
// just spew it to stderr normally in debug mode
fprintf ( stderr , " %s %s " , logtime , buf ) ;
# endif // XASH_NSWITCH && NSWITCH_DEBUG
2023-02-09 03:59:30 +01:00
2023-03-24 00:25:11 +01:00
# if XASH_PSVITA
// spew to stderr only in developer mode
if ( host_developer . value )
{
fprintf ( stderr , " %s %s " , logtime , buf ) ;
}
# endif
2022-06-14 03:25:40 +02:00
# elif !XASH_WIN32 // Wcon does the job
2022-06-14 03:23:09 +02:00
Sys_PrintLogfile ( STDOUT_FILENO , logtime , msg , XASH_COLORIZE_CONSOLE ) ;
2022-06-14 03:25:40 +02:00
Sys_FlushStdout ( ) ;
2018-04-14 03:08:28 +02:00
# endif
2022-06-13 21:36:43 +02:00
}
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 ) ;
2018-04-14 03:08:28 +02:00
if ( ! lastchar | | lastchar = = ' \n ' )
strftime ( logtime , sizeof ( logtime ) , " [%H:%M:%S] " , crt_tm ) ; //short time
2022-06-13 21:36:43 +02:00
// spew to stdout
Sys_PrintStdout ( logtime , pMsg ) ;
2018-05-03 18:51:23 +02:00
2018-04-14 03:08:28 +02:00
if ( ! s_ld . logfile )
2022-06-14 03:23:09 +02:00
{
// save last char to detect when line was not ended
lastchar = pMsg [ Q_strlen ( pMsg ) - 1 ] ;
2018-04-14 03:08:28 +02:00
return ;
2022-06-14 03:23:09 +02:00
}
2018-04-14 03:08:28 +02:00
if ( ! lastchar | | lastchar = = ' \n ' )
2022-06-10 10:20:58 +02:00
strftime ( logtime , sizeof ( logtime ) , " [%Y:%m:%d|%H:%M:%S] " , crt_tm ) ; //full time
// save last char to detect when line was not ended
2022-06-13 21:36:43 +02:00
lastchar = pMsg [ Q_strlen ( pMsg ) - 1 ] ;
2018-04-14 03:08:28 +02:00
2022-06-14 03:23:09 +02:00
Sys_PrintLogfile ( s_ld . logfileno , logtime , pMsg , false ) ;
2022-02-22 04:08:32 +01:00
Sys_FlushLogfile ( ) ;
2018-04-14 03:08:28 +02:00
}
2018-04-18 17:32:30 +02:00
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
CONSOLE PRINT
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
/*
= = = = = = = = = = = = =
Con_Printf
= = = = = = = = = = = = =
*/
2020-01-19 02:15:54 +01:00
void GAME_EXPORT Con_Printf ( const char * szFmt , . . . )
2018-04-18 17:32:30 +02:00
{
static char buffer [ MAX_PRINT_MSG ] ;
va_list args ;
if ( ! host . allow_console )
return ;
va_start ( args , szFmt ) ;
Q_vsnprintf ( buffer , sizeof ( buffer ) , szFmt , args ) ;
va_end ( args ) ;
Sys_Print ( buffer ) ;
}
/*
= = = = = = = = = = = = =
Con_DPrintf
= = = = = = = = = = = = =
*/
2020-01-19 02:15:54 +01:00
void GAME_EXPORT Con_DPrintf ( const char * szFmt , . . . )
2018-04-18 17:32:30 +02:00
{
static char buffer [ MAX_PRINT_MSG ] ;
va_list args ;
if ( host_developer . value < DEV_NORMAL )
return ;
va_start ( args , szFmt ) ;
Q_vsnprintf ( buffer , sizeof ( buffer ) , szFmt , args ) ;
va_end ( args ) ;
if ( buffer [ 0 ] = = ' 0 ' & & buffer [ 1 ] = = ' \n ' & & buffer [ 2 ] = = ' \0 ' )
return ; // hlrally spam
Sys_Print ( buffer ) ;
}
/*
= = = = = = = = = = = = =
Con_Reportf
= = = = = = = = = = = = =
*/
2018-04-23 22:07:54 +02:00
void Con_Reportf ( const char * szFmt , . . . )
2018-04-18 17:32:30 +02:00
{
static char buffer [ MAX_PRINT_MSG ] ;
va_list args ;
if ( host_developer . value < DEV_EXTENDED )
return ;
va_start ( args , szFmt ) ;
Q_vsnprintf ( buffer , sizeof ( buffer ) , szFmt , args ) ;
va_end ( args ) ;
Sys_Print ( buffer ) ;
2018-04-22 12:07:40 +02:00
}
2020-02-08 17:00:39 +01:00
# if XASH_MESSAGEBOX == MSGBOX_STDERR
void Platform_MessageBox ( const char * title , const char * message , qboolean parentMainWindow )
{
fprintf ( stderr ,
" ====================================== \n "
" %s: %s \n "
" ====================================== \n " , title , message ) ;
}
# endif