2004-03-27 17:01:18 +00: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 16:02:40 +00:00
// amxx_logging localinfo:
// 0 = no logging
// 1 = one logfile / day
// 2 = one logfile / map
// 3 = HL Logs
2004-03-27 17:01:18 +00:00
2004-07-08 16:02:40 +00:00
# include <time.h>
2004-07-23 16:55:53 +00:00
# ifndef __linux__
2004-07-08 16:02:40 +00:00
# include <io.h>
2004-07-23 16:55:53 +00:00
# endif
2004-03-27 17:01:18 +00:00
# include "amxmodx.h"
2004-07-08 16:02:40 +00:00
CLog : : CLog ( )
{
m_LogType = 0 ;
m_LogFile . clear ( ) ;
}
2004-03-27 17:01:18 +00:00
2004-07-08 16:02:40 +00:00
CLog : : ~ CLog ( )
2004-03-27 17:01:18 +00:00
{
2004-07-08 16:02:40 +00:00
CloseFile ( ) ;
2004-03-27 17:01:18 +00:00
}
2004-07-08 16:02:40 +00:00
void CLog : : CloseFile ( )
2004-03-27 17:01:18 +00:00
{
2004-07-08 16:02:40 +00:00
// log "log file closed" to old file, if any
if ( ! m_LogFile . empty ( ) )
{
FILE * fp = fopen ( m_LogFile . str ( ) , " r " ) ;
if ( fp )
{
fclose ( fp ) ;
fopen ( m_LogFile . str ( ) , " a+ " ) ;
// 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 17:01:18 +00:00
2004-07-08 16:02:40 +00:00
fprintf ( fp , " L %s: %s \n " , date , " Log file closed. " ) ;
fclose ( fp ) ;
}
m_LogFile . clear ( ) ;
}
}
void CLog : : CreateNewFile ( )
{
CloseFile ( ) ;
2004-03-27 17:01:18 +00:00
// build filename
time_t td ;
time ( & td ) ;
tm * curTime = localtime ( & td ) ;
2004-07-08 16:02:40 +00:00
int i = 0 ;
while ( true )
2004-03-27 17:01:18 +00:00
{
2004-07-08 16:02:40 +00:00
m_LogFile . set ( build_pathname ( " %s/L%02d%02d%03d.log " , g_log_dir . str ( ) , curTime - > tm_mon + 1 , curTime - > tm_mday , i ) ) ;
FILE * pTmpFile = fopen ( m_LogFile . str ( ) , " r " ) ; // open for reading to check whether the file exists
if ( ! pTmpFile )
break ;
fclose ( pTmpFile ) ;
+ + i ;
2004-04-22 08:09:17 +00:00
}
2004-07-08 16:02:40 +00:00
// Log logfile start
FILE * fp = fopen ( m_LogFile . str ( ) , " w " ) ;
if ( ! fp )
2004-04-22 08:09:17 +00:00
{
2004-07-08 16:02:40 +00:00
ALERT ( at_logged , " [AMXX] Unexpected fatal logging error. AMXX Logging disabled. \n " ) ;
SET_LOCALINFO ( " amxx_logging " , " 0 " ) ;
}
fprintf ( fp , " AMX Mod X log file started (file \" %s/L%02d%02d%03d.log \" ) (version \" %s \" ) \n " , g_log_dir . str ( ) , curTime - > tm_mon + 1 , curTime - > tm_mday , i , AMX_VERSION ) ;
fclose ( fp ) ;
2004-03-27 17:01:18 +00:00
}
2004-07-08 16:02:40 +00:00
void CLog : : UseFile ( const String & fileName )
2004-03-27 17:01:18 +00:00
{
2004-07-08 16:02:40 +00:00
m_LogFile . set ( build_pathname ( " %s/%s " , g_log_dir . str ( ) , fileName . str ( ) ) ) ;
}
2004-04-22 08:09:17 +00:00
2004-07-08 16:02:40 +00:00
void CLog : : MapChange ( )
{
2004-07-13 16:17:38 +00:00
// create dir if not existing
# ifdef __linux
mkdir ( build_pathname ( " %s " , g_log_dir . str ( ) ) , 0700 ) ;
# else
mkdir ( build_pathname ( " %s " , g_log_dir . str ( ) ) ) ;
# endif
2004-07-08 16:02:40 +00:00
m_LogType = atoi ( get_localinfo ( " amxx_logging " , " 1 " ) ) ;
if ( m_LogType < 0 | | m_LogType > 3 )
2004-04-22 08:09:17 +00:00
{
2004-07-08 16:02:40 +00:00
SET_LOCALINFO ( " amxx_logging " , " 1 " ) ;
m_LogType = 1 ;
print_srvconsole ( " [AMXX] Invalid amxx_logging value; setting back to 1... " ) ;
2004-04-22 08:09:17 +00:00
}
2004-07-08 16:02:40 +00:00
if ( m_LogType = = 2 )
2004-04-22 08:09:17 +00:00
{
2004-07-08 16:02:40 +00:00
// create new logfile
CreateNewFile ( ) ;
2004-04-22 08:09:17 +00:00
}
2004-07-08 16:02:40 +00:00
else if ( m_LogType = = 1 )
2004-03-27 17:01:18 +00:00
{
2004-07-08 16:02:40 +00:00
Log ( " -------- Mapchange -------- " ) ;
}
else
return ;
}
2004-03-27 17:01:18 +00:00
2004-07-08 16:02:40 +00:00
void CLog : : Log ( const char * fmt , . . . )
{
if ( m_LogType = = 1 | | m_LogType = = 2 )
{
2004-03-27 17:01:18 +00: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 16:02:40 +00:00
// msg
char msg [ 3072 ] ;
va_list arglst ;
va_start ( arglst , fmt ) ;
vsprintf ( msg , fmt , arglst ) ;
va_end ( arglst ) ;
2004-07-13 16:17:38 +00:00
FILE * pF ;
if ( m_LogType = = 2 )
{
pF = fopen ( m_LogFile . str ( ) , " a+ " ) ;
if ( ! pF )
{
CreateNewFile ( ) ;
pF = fopen ( m_LogFile . str ( ) , " a+ " ) ;
if ( ! pF )
{
ALERT ( at_logged , " [AMXX] Unexpected fatal logging error (couldn't open %s for a+). AMXX Logging disabled for this map. \n " , m_LogFile . str ( ) ) ;
m_LogType = 0 ;
return ;
}
}
}
else
{
pF = fopen ( build_pathname ( " %s/L%02d%02d.log " , g_log_dir . str ( ) , curTime - > tm_mon + 1 , curTime - > tm_mday ) , " a+ " ) ;
}
2004-03-27 17:01:18 +00:00
fprintf ( pF , " L %s: %s \n " , date , msg ) ;
2004-07-08 16:02:40 +00:00
2004-03-27 17:01:18 +00:00
fclose ( pF ) ;
2004-07-08 16:02:40 +00:00
// print on server console
2004-03-27 17:01:18 +00:00
print_srvconsole ( " L %s: %s \n " , date , msg ) ;
}
2004-07-08 16:02:40 +00:00
else if ( m_LogType = = 3 )
2004-03-27 17:01:18 +00:00
{
// build message
// :TODO: Overflow possible here
char msg [ 3072 ] ;
va_list arglst ;
va_start ( arglst , fmt ) ;
vsprintf ( msg , fmt , arglst ) ;
va_end ( arglst ) ;
2004-03-27 17:40:06 +00:00
ALERT ( at_logged , " %s \n " , msg ) ;
2004-03-27 17:01:18 +00:00
}
}