logging functions moved from util.cpp to amxxlog.cpp; renamed to AMXXLOG_*; AMXXLOG_Init added (called from Meta_Attach)
UTIL_GetModulesNum moved to modules.cpp and renamed to countModules
This commit is contained in:
parent
5c1287bbfb
commit
cdbfcdc4c4
142
amxmodx/amxxlog.cpp
Executable file
142
amxmodx/amxxlog.cpp
Executable file
|
@ -0,0 +1,142 @@
|
||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <extdll.h>
|
||||||
|
#include <meta_api.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "amxmodx.h"
|
||||||
|
|
||||||
|
String g_AMXXLOG_LogFile;
|
||||||
|
cvar_t init_amx_logging = {"amx_logging", "", FCVAR_SPONLY};
|
||||||
|
cvar_t *amx_logging = NULL;
|
||||||
|
|
||||||
|
// Initialize cvar; Called from Meta_Attach
|
||||||
|
void AMXXLOG_Init()
|
||||||
|
{
|
||||||
|
CVAR_REGISTER(&init_amx_logging);
|
||||||
|
amx_logging = CVAR_GET_POINTER(init_amx_logging.name);
|
||||||
|
CVAR_SET_STRING(init_amx_logging.name, "1");
|
||||||
|
}
|
||||||
|
|
||||||
|
void AMXXLOG_MakeNewLogFile()
|
||||||
|
{
|
||||||
|
if (amx_logging && amx_logging->value == 0.0f)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// build filename
|
||||||
|
time_t td;
|
||||||
|
time(&td);
|
||||||
|
tm *curTime = localtime(&td);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
g_AMXXLOG_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(g_AMXXLOG_LogFile.str(), "r"); // open for reading to check whether the file exists
|
||||||
|
if (!pTmpFile)
|
||||||
|
break;
|
||||||
|
fclose(pTmpFile);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
// Log logfile start
|
||||||
|
AMXXLOG_Log("AMX Mod X log file started (file \"%s/L%02d%02d%03d.log\") (version \"%s\")", g_log_dir.str(), curTime->tm_mon + 1, curTime->tm_mday, i, AMX_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AMXXLOG_Log(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
if (!amx_logging)
|
||||||
|
return;
|
||||||
|
int logType = (int)amx_logging->value;
|
||||||
|
if (logType == 0)
|
||||||
|
return;
|
||||||
|
else if (logType == 1)
|
||||||
|
{
|
||||||
|
// build message
|
||||||
|
// :TODO: Overflow possible here
|
||||||
|
char msg[3072];
|
||||||
|
va_list arglst;
|
||||||
|
va_start(arglst, fmt);
|
||||||
|
vsprintf(msg, fmt, arglst);
|
||||||
|
va_end(arglst);
|
||||||
|
|
||||||
|
// get time
|
||||||
|
time_t td;
|
||||||
|
time(&td);
|
||||||
|
tm *curTime = localtime(&td);
|
||||||
|
|
||||||
|
char date[32];
|
||||||
|
strftime(date, 31, "%m/%d/%Y - %H:%M:%S", curTime);
|
||||||
|
|
||||||
|
static bool s_inCreatingLogFile = false;
|
||||||
|
FILE *pF = fopen(g_AMXXLOG_LogFile.str(), "a+");
|
||||||
|
if (!pF)
|
||||||
|
{
|
||||||
|
if (s_inCreatingLogFile)
|
||||||
|
{
|
||||||
|
ALERT(at_logged, "[AMXX] Unexpected fatal logging error. AMXX Logging disabled.");
|
||||||
|
CVAR_SET_FLOAT(init_amx_logging.name, 0.0f);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Create new logfile
|
||||||
|
s_inCreatingLogFile = true;
|
||||||
|
AMXXLOG_MakeNewLogFile();
|
||||||
|
s_inCreatingLogFile = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// log msg now
|
||||||
|
fprintf(pF, "L %s: %s\n", date, msg);
|
||||||
|
fclose(pF);
|
||||||
|
print_srvconsole("L %s: %s\n", date, msg);
|
||||||
|
}
|
||||||
|
else if (logType == 2)
|
||||||
|
{
|
||||||
|
// build message
|
||||||
|
// :TODO: Overflow possible here
|
||||||
|
char msg[3072];
|
||||||
|
va_list arglst;
|
||||||
|
va_start(arglst, fmt);
|
||||||
|
vsprintf(msg, fmt, arglst);
|
||||||
|
va_end(arglst);
|
||||||
|
ALERT(at_logged, msg);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ALERT(at_logged, "[AMXX] Invalid %s value. Setting to 0", init_amx_logging.name);
|
||||||
|
CVAR_SET_FLOAT(init_amx_logging.name, 0.0f);
|
||||||
|
}
|
||||||
|
}
|
|
@ -59,8 +59,8 @@ void report_error( int code, char* fmt, ... )
|
||||||
//File fp( "error_amx.log","a" );
|
//File fp( "error_amx.log","a" );
|
||||||
//fp << string;
|
//fp << string;
|
||||||
print_srvconsole( string );
|
print_srvconsole( string );
|
||||||
UTIL_Log("[AMXX] Make sure that modules are compatible with AMX Mod X %s" , AMX_VERSION );
|
AMXXLOG_Log("[AMXX] Make sure that modules are compatible with AMX Mod X %s" , AMX_VERSION );
|
||||||
UTIL_Log("[AMXX] Please fix the problem then start the server again" );
|
AMXXLOG_Log("[AMXX] Please fix the problem then start the server again" );
|
||||||
}
|
}
|
||||||
sleep( 5 );
|
sleep( 5 );
|
||||||
exit( code );
|
exit( code );
|
||||||
|
@ -348,7 +348,7 @@ int loadModules(const char* filename)
|
||||||
|
|
||||||
if ( !fp )
|
if ( !fp )
|
||||||
{
|
{
|
||||||
UTIL_Log( "[AMXX] Modules list not found (file \"%s\")",filename);
|
AMXXLOG_Log( "[AMXX] Modules list not found (file \"%s\")",filename);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -461,7 +461,7 @@ void dettachMetaModModules( const char* filename )
|
||||||
|
|
||||||
if ( !fp )
|
if ( !fp )
|
||||||
{
|
{
|
||||||
UTIL_Log( "[AMXX] Modules list not found (file \"%s\")",filename);
|
AMXXLOG_Log( "[AMXX] Modules list not found (file \"%s\")",filename);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -503,7 +503,7 @@ void attachMetaModModules( const char* filename )
|
||||||
|
|
||||||
if ( !fp )
|
if ( !fp )
|
||||||
{
|
{
|
||||||
UTIL_Log( "[AMXX] Modules list not found (file \"%s\")",filename);
|
AMXXLOG_Log( "[AMXX] Modules list not found (file \"%s\")",filename);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -550,3 +550,37 @@ void attachMetaModModules( const char* filename )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Get the number of running modules
|
||||||
|
int countModules(CountModulesMode mode)
|
||||||
|
{
|
||||||
|
CList<CModule>::iterator iter;
|
||||||
|
int num;
|
||||||
|
switch (mode)
|
||||||
|
{
|
||||||
|
case CountModules_All:
|
||||||
|
return g_modules.size();
|
||||||
|
case CountModules_Running:
|
||||||
|
iter = g_modules.begin();
|
||||||
|
num = 0;
|
||||||
|
while (iter)
|
||||||
|
{
|
||||||
|
if ((*iter).getStatusValue() == MODULE_LOADED)
|
||||||
|
++num;
|
||||||
|
++iter;
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
case CountModules_Stopped:
|
||||||
|
iter = g_modules.begin();
|
||||||
|
num = 0;
|
||||||
|
while (iter)
|
||||||
|
{
|
||||||
|
if ((*iter).getStatusValue() != MODULE_LOADED)
|
||||||
|
++num;
|
||||||
|
++iter;
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
|
@ -271,95 +271,3 @@ void UTIL_FakeClientCommand(edict_t *pEdict, const char *cmd, const char *arg1,
|
||||||
MDLL_ClientCommand(pEdict);
|
MDLL_ClientCommand(pEdict);
|
||||||
g_fakecmd.fake = false;
|
g_fakecmd.fake = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String g_UTIL_LogFile;
|
|
||||||
|
|
||||||
void UTIL_MakeNewLogFile()
|
|
||||||
{
|
|
||||||
// build filename
|
|
||||||
|
|
||||||
time_t td;
|
|
||||||
time(&td);
|
|
||||||
tm *curTime = localtime(&td);
|
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
g_UTIL_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(g_UTIL_LogFile.str(), "r"); // open for reading to check whether the file exists
|
|
||||||
if (!pTmpFile)
|
|
||||||
break;
|
|
||||||
fclose(pTmpFile);
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
// Log logfile start
|
|
||||||
UTIL_Log("AMX Mod X log file started (file \"%s/L%02d%02d%03d.log\") (version \"%s\")", g_log_dir.str(), curTime->tm_mon + 1, curTime->tm_mday, i, AMX_VERSION);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UTIL_Log(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
// build message
|
|
||||||
// :TODO: Overflow possible here
|
|
||||||
char msg[3072];
|
|
||||||
va_list arglst;
|
|
||||||
va_start(arglst, fmt);
|
|
||||||
vsprintf(msg, fmt, arglst);
|
|
||||||
va_end(arglst);
|
|
||||||
|
|
||||||
// get time
|
|
||||||
time_t td;
|
|
||||||
time(&td);
|
|
||||||
tm *curTime = localtime(&td);
|
|
||||||
|
|
||||||
char date[32];
|
|
||||||
strftime(date, 31, "%m/%d/%Y - %H:%M:%S", curTime);
|
|
||||||
|
|
||||||
// log msg now
|
|
||||||
FILE *pF = fopen(g_UTIL_LogFile.str(), "a+");
|
|
||||||
if (!pF)
|
|
||||||
return; // don't try to create a new logfile to prevent recursion crashes if there is an unforseen error
|
|
||||||
|
|
||||||
fprintf(pF, "L %s: %s\n", date, msg);
|
|
||||||
fclose(pF);
|
|
||||||
print_srvconsole("L %s: %s\n", date, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the number of running modules
|
|
||||||
int UTIL_GetModulesNum(int mode)
|
|
||||||
{
|
|
||||||
CList<CModule>::iterator iter;
|
|
||||||
int num;
|
|
||||||
switch (mode)
|
|
||||||
{
|
|
||||||
case UTIL_MODULES_ALL:
|
|
||||||
return g_modules.size();
|
|
||||||
case UTIL_MODULES_RUNNING:
|
|
||||||
iter = g_modules.begin();
|
|
||||||
num = 0;
|
|
||||||
while (iter)
|
|
||||||
{
|
|
||||||
if ((*iter).getStatusValue() == MODULE_LOADED)
|
|
||||||
++num;
|
|
||||||
++iter;
|
|
||||||
}
|
|
||||||
return num;
|
|
||||||
case UTIL_MODULES_STOPPED:
|
|
||||||
iter = g_modules.begin();
|
|
||||||
num = 0;
|
|
||||||
while (iter)
|
|
||||||
{
|
|
||||||
if ((*iter).getStatusValue() != MODULE_LOADED)
|
|
||||||
++num;
|
|
||||||
++iter;
|
|
||||||
}
|
|
||||||
return num;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user