2004-03-27 20:01:18 +03:00
/* AMX Mod X
*
* by the AMX Mod X Development Team
*
*
* 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 2 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 .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*
* In addition , as a special exception , the author gives permission to
* link the code of this program with the Half - Life Game Engine ( " HL
* Engine " ) and Modified Game Libraries ( " MODs " ) developed by Valve,
* L . L . C ( " Valve " ) . You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve . If you modify this file , you may extend this exception
* to your version of the file , but you are not obligated to do so . If
* you do not wish to do so , delete this exception statement from your
* version .
*/
2004-07-08 20:02:40 +04:00
// amxx_logging localinfo:
// 0 = no logging
// 1 = one logfile / day
// 2 = one logfile / map
// 3 = HL Logs
2004-03-27 20:01:18 +03:00
2004-07-08 20:02:40 +04:00
# include <time.h>
2004-07-23 20:55:53 +04:00
# ifndef __linux__
2005-09-10 04:59:24 +04:00
# include <io.h>
2004-07-23 20:55:53 +04:00
# endif
2004-03-27 20:01:18 +03:00
# include "amxmodx.h"
2004-09-03 10:53:00 +04:00
# ifndef __linux__
2005-09-10 04:59:24 +04:00
# define vsnprintf _vsnprintf
2004-09-03 10:53:00 +04:00
# endif
2007-03-13 01:27:27 +03:00
# include "svn_version.h"
2004-07-08 20:02:40 +04:00
CLog : : CLog ( )
{
m_LogType = 0 ;
m_LogFile . clear ( ) ;
2006-04-13 20:29:01 +04:00
m_FoundError = false ;
2006-09-10 08:52:43 +04:00
m_LoggedErrMap = false ;
2004-07-08 20:02:40 +04:00
}
2004-03-27 20:01:18 +03:00
2004-07-08 20:02:40 +04:00
CLog : : ~ CLog ( )
2004-03-27 20:01:18 +03:00
{
2004-07-08 20:02:40 +04:00
CloseFile ( ) ;
2004-03-27 20:01:18 +03:00
}
2004-07-08 20:02:40 +04:00
void CLog : : CloseFile ( )
2004-03-27 20:01:18 +03:00
{
2004-07-08 20:02:40 +04:00
// log "log file closed" to old file, if any
if ( ! m_LogFile . empty ( ) )
{
2004-08-13 12:46:04 +04:00
FILE * fp = fopen ( m_LogFile . c_str ( ) , " r " ) ;
2005-09-10 04:59:24 +04:00
2004-07-08 20:02:40 +04:00
if ( fp )
{
fclose ( fp ) ;
2004-10-03 20:49:09 +04:00
fp = fopen ( m_LogFile . c_str ( ) , " a+ " ) ;
2004-07-08 20:02:40 +04:00
// get time
time_t td ;
time ( & td ) ;
tm * curTime = localtime ( & td ) ;
char date [ 32 ] ;
strftime ( date , 31 , " %m/%d/%Y - %H:%M:%S " , curTime ) ;
2004-03-27 20:01:18 +03:00
2004-07-08 20:02:40 +04:00
fprintf ( fp , " L %s: %s \n " , date , " Log file closed. " ) ;
fclose ( fp ) ;
}
2005-09-10 04:59:24 +04:00
2004-07-08 20:02:40 +04:00
m_LogFile . clear ( ) ;
}
}
void CLog : : CreateNewFile ( )
{
CloseFile ( ) ;
2005-09-17 03:48:51 +04:00
2004-03-27 20:01:18 +03:00
// build filename
time_t td ;
time ( & td ) ;
tm * curTime = localtime ( & td ) ;
2006-03-19 23:14:24 +03:00
char file [ 256 ] ;
2004-07-08 20:02:40 +04:00
int i = 0 ;
2005-09-10 04:59:24 +04:00
2004-07-08 20:02:40 +04:00
while ( true )
2004-03-27 20:01:18 +03:00
{
2006-03-19 23:39:25 +03:00
build_pathname_r ( file , sizeof ( file ) - 1 , " %s/L%02d%02d%03d.log " , g_log_dir . c_str ( ) , curTime - > tm_mon + 1 , curTime - > tm_mday , i ) ;
FILE * pTmpFile = fopen ( file , " r " ) ; // open for reading to check whether the file exists
2005-09-10 04:59:24 +04:00
2004-07-08 20:02:40 +04:00
if ( ! pTmpFile )
break ;
2005-09-10 04:59:24 +04:00
2004-07-08 20:02:40 +04:00
fclose ( pTmpFile ) ;
+ + i ;
2004-04-22 12:09:17 +04:00
}
2006-03-19 23:39:25 +03:00
m_LogFile . assign ( file ) ;
2005-09-10 04:59:24 +04:00
2004-07-08 20:02:40 +04:00
// Log logfile start
2004-08-13 12:46:04 +04:00
FILE * fp = fopen ( m_LogFile . c_str ( ) , " w " ) ;
2005-09-10 04:59:24 +04:00
2004-07-08 20:02:40 +04:00
if ( ! fp )
2004-04-22 12:09:17 +04:00
{
2004-07-08 20:02:40 +04:00
ALERT ( at_logged , " [AMXX] Unexpected fatal logging error. AMXX Logging disabled. \n " ) ;
SET_LOCALINFO ( " amxx_logging " , " 0 " ) ;
2005-09-10 04:59:24 +04:00
} else {
2007-03-13 01:27:27 +03:00
fprintf ( fp , " AMX Mod X log file started (file \" %s/L%02d%02d%03d.log \" ) (version \" %s \" ) \n " , g_log_dir . c_str ( ) , curTime - > tm_mon + 1 , curTime - > tm_mday , i , SVN_VERSION_STRING ) ;
2004-10-03 20:49:09 +04:00
fclose ( fp ) ;
}
2004-03-27 20:01:18 +03:00
}
2004-08-15 14:53:48 +04:00
void CLog : : UseFile ( const String & fileName )
2004-03-27 20:01:18 +03:00
{
2005-07-08 09:05:06 +04:00
static char file [ 256 ] ;
m_LogFile . assign ( build_pathname_r ( file , sizeof ( file ) - 1 , " %s/%s " , g_log_dir . c_str ( ) , fileName . c_str ( ) ) ) ;
2004-07-08 20:02:40 +04:00
}
2004-04-22 12:09:17 +04:00
2004-07-08 20:02:40 +04:00
void CLog : : MapChange ( )
{
2004-07-13 20:17:38 +04:00
// create dir if not existing
2005-07-08 09:05:06 +04:00
char file [ 256 ] ;
2004-07-13 20:17:38 +04:00
# ifdef __linux
2005-09-17 03:48:51 +04:00
mkdir ( build_pathname_r ( file , sizeof ( file ) - 1 , " %s " , g_log_dir . c_str ( ) ) , 0700 ) ;
2004-07-13 20:17:38 +04:00
# else
2005-09-17 03:48:51 +04:00
mkdir ( build_pathname_r ( file , sizeof ( file ) - 1 , " %s " , g_log_dir . c_str ( ) ) ) ;
2004-07-13 20:17:38 +04:00
# endif
2004-07-08 20:02:40 +04:00
m_LogType = atoi ( get_localinfo ( " amxx_logging " , " 1 " ) ) ;
2005-09-10 04:59:24 +04:00
2004-07-08 20:02:40 +04:00
if ( m_LogType < 0 | | m_LogType > 3 )
2004-04-22 12:09:17 +04:00
{
2004-07-08 20:02:40 +04:00
SET_LOCALINFO ( " amxx_logging " , " 1 " ) ;
m_LogType = 1 ;
print_srvconsole ( " [AMXX] Invalid amxx_logging value; setting back to 1... " ) ;
2004-04-22 12:09:17 +04:00
}
2006-09-10 08:52:43 +04:00
m_LoggedErrMap = false ;
2004-07-08 20:02:40 +04:00
if ( m_LogType = = 2 )
2004-04-22 12:09:17 +04:00
{
2004-07-08 20:02:40 +04:00
// create new logfile
CreateNewFile ( ) ;
2006-09-10 08:52:43 +04:00
} else if ( m_LogType = = 1 ) {
2005-09-08 17:03:38 +04:00
Log ( " -------- Mapchange to %s -------- " , STRING ( gpGlobals - > mapname ) ) ;
2006-09-10 08:52:43 +04:00
} else {
2004-07-08 20:02:40 +04:00
return ;
2006-09-10 08:52:43 +04:00
}
2004-07-08 20:02:40 +04:00
}
2004-03-27 20:01:18 +03:00
2004-07-08 20:02:40 +04:00
void CLog : : Log ( const char * fmt , . . . )
{
2005-07-08 09:05:06 +04:00
static char file [ 256 ] ;
2005-09-10 04:59:24 +04:00
2004-07-08 20:02:40 +04:00
if ( m_LogType = = 1 | | m_LogType = = 2 )
{
2004-03-27 20:01:18 +03:00
// get time
time_t td ;
time ( & td ) ;
tm * curTime = localtime ( & td ) ;
char date [ 32 ] ;
strftime ( date , 31 , " %m/%d/%Y - %H:%M:%S " , curTime ) ;
2004-07-08 20:02:40 +04:00
// msg
2005-07-08 09:05:06 +04:00
static char msg [ 3072 ] ;
2004-07-08 20:02:40 +04:00
va_list arglst ;
va_start ( arglst , fmt ) ;
2004-09-03 10:53:00 +04:00
vsnprintf ( msg , 3071 , fmt , arglst ) ;
2004-07-08 20:02:40 +04:00
va_end ( arglst ) ;
2005-07-08 09:05:06 +04:00
FILE * pF = NULL ;
2004-07-13 20:17:38 +04:00
if ( m_LogType = = 2 )
{
2004-08-13 12:46:04 +04:00
pF = fopen ( m_LogFile . c_str ( ) , " a+ " ) ;
2004-07-13 20:17:38 +04:00
if ( ! pF )
{
CreateNewFile ( ) ;
2004-08-13 12:46:04 +04:00
pF = fopen ( m_LogFile . c_str ( ) , " a+ " ) ;
2005-09-10 04:59:24 +04:00
2004-07-13 20:17:38 +04:00
if ( ! pF )
{
2004-08-13 12:46:04 +04:00
ALERT ( at_logged , " [AMXX] Unexpected fatal logging error (couldn't open %s for a+). AMXX Logging disabled for this map. \n " , m_LogFile . c_str ( ) ) ;
2004-07-13 20:17:38 +04:00
m_LogType = 0 ;
return ;
}
}
2005-09-10 04:59:24 +04:00
} else {
2005-07-08 09:05:06 +04:00
build_pathname_r ( file , sizeof ( file ) - 1 , " %s/L%02d%02d.log " , g_log_dir . c_str ( ) , curTime - > tm_mon + 1 , curTime - > tm_mday ) ;
pF = fopen ( file , " a+ " ) ;
2004-07-13 20:17:38 +04:00
}
2005-09-17 03:48:51 +04:00
2004-09-01 21:01:52 +04:00
if ( pF )
{
fprintf ( pF , " L %s: %s \n " , date , msg ) ;
fclose ( pF ) ;
2005-09-10 04:59:24 +04:00
} else {
2005-07-08 09:05:06 +04:00
ALERT ( at_logged , " [AMXX] Unexpected fatal logging error (couldn't open %s for a+). AMXX Logging disabled for this map. \n " , file ) ;
2004-09-01 21:01:52 +04:00
m_LogType = 0 ;
return ;
}
2004-07-08 20:02:40 +04:00
// print on server console
2004-03-27 20:01:18 +03:00
print_srvconsole ( " L %s: %s \n " , date , msg ) ;
2006-09-10 08:52:43 +04:00
} else if ( m_LogType = = 3 ) {
2004-03-27 20:01:18 +03:00
// build message
2005-07-08 09:05:06 +04:00
static char msg_ [ 3072 ] ;
2004-03-27 20:01:18 +03:00
va_list arglst ;
va_start ( arglst , fmt ) ;
2005-07-08 09:05:06 +04:00
vsnprintf ( msg_ , 3071 , fmt , arglst ) ;
2004-03-27 20:01:18 +03:00
va_end ( arglst ) ;
2005-07-08 09:05:06 +04:00
ALERT ( at_logged , " %s \n " , msg_ ) ;
2004-03-27 20:01:18 +03:00
}
}
2006-04-13 20:29:01 +04:00
void CLog : : LogError ( const char * fmt , . . . )
{
static char file [ 256 ] ;
if ( m_FoundError )
2006-09-10 08:52:43 +04:00
{
2006-04-13 20:29:01 +04:00
return ;
2006-09-10 08:52:43 +04:00
}
2006-04-13 20:29:01 +04:00
// get time
time_t td ;
time ( & td ) ;
tm * curTime = localtime ( & td ) ;
char date [ 32 ] ;
strftime ( date , 31 , " %m/%d/%Y - %H:%M:%S " , curTime ) ;
// msg
static char msg [ 3072 ] ;
va_list arglst ;
va_start ( arglst , fmt ) ;
2006-09-10 16:07:01 +04:00
vsnprintf ( msg , sizeof ( msg ) - 1 , fmt , arglst ) ;
2006-04-13 20:29:01 +04:00
va_end ( arglst ) ;
FILE * pF = NULL ;
2006-04-21 06:18:34 +04:00
build_pathname_r ( file , sizeof ( file ) - 1 , " %s/error_%02d%02d%02d.log " , g_log_dir . c_str ( ) , curTime - > tm_mon + 1 , curTime - > tm_mday , curTime - > tm_year - 100 ) ;
2006-04-13 20:29:01 +04:00
pF = fopen ( file , " a+ " ) ;
if ( pF )
{
2006-09-10 08:52:43 +04:00
if ( ! m_LoggedErrMap )
{
fprintf ( pF , " L %s: Start of error session. \n " , date ) ;
2006-09-10 16:07:01 +04:00
fprintf ( pF , " L %s: Info (map \" %s \" ) (logfile \" error_%02d%02d%02d.log \" ) \n " , date , STRING ( gpGlobals - > mapname ) , curTime - > tm_mon + 1 , curTime - > tm_mday , curTime - > tm_year - 100 ) ;
2006-09-10 08:52:43 +04:00
m_LoggedErrMap = true ;
}
2006-04-13 20:29:01 +04:00
fprintf ( pF , " L %s: %s \n " , date , msg ) ;
fclose ( pF ) ;
} else {
ALERT ( at_logged , " [AMXX] Unexpected fatal logging error (couldn't open %s for a+). AMXX Error Logging disabled for this map. \n " , file ) ;
m_FoundError = true ;
return ;
}
// print on server console
print_srvconsole ( " L %s: %s \n " , date , msg ) ;
2006-06-06 14:49:33 +04:00
}