amxmodx/amxmodx/CLogEvent.cpp

319 lines
6.0 KiB
C++
Raw Normal View History

2004-03-05 21:03:14 +00:00
/* AMX Mod X
*
* by the AMX Mod X Development Team
* originally developed by OLO
*
*
* 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-01-31 20:56:22 +00:00
2004-03-24 01:35:44 +00:00
#include "amxmodx.h"
2004-01-31 20:56:22 +00:00
#include "CLogEvent.h"
// *****************************************************
// class LogEventsMngr
// *****************************************************
LogEventsMngr::LogEventsMngr()
{
logCurrent = logCounter = 0;
logcmplist = 0;
arelogevents = false;
memset(logevents, 0, sizeof(logevents));
2004-01-31 20:56:22 +00:00
}
LogEventsMngr::~LogEventsMngr()
{
2005-09-10 20:09:14 +00:00
clearLogEvents();
2004-01-31 20:56:22 +00:00
}
int LogEventsMngr::CLogCmp::compareCondition(const char* string)
{
if (logid == parent->logCounter)
return result;
logid = parent->logCounter;
2005-09-16 23:48:51 +00:00
if (in)
return result = strstr(string, text.c_str()) ? 0 : 1;
return result = strcmp(string,text.c_str());
2004-01-31 20:56:22 +00:00
}
LogEventsMngr::CLogCmp* LogEventsMngr::registerCondition(char* filter)
{
2004-01-31 20:56:22 +00:00
char* temp = filter;
// expand "1=message"
2005-09-16 23:48:51 +00:00
while (isdigit(*filter))
2004-01-31 20:56:22 +00:00
++filter;
2005-09-16 23:48:51 +00:00
bool in = (*filter=='&');
2005-09-16 23:48:51 +00:00
*filter++ = 0;
int pos = atoi(temp);
if (pos < 0 || pos >= MAX_LOGARGS)
pos = 0;
2004-01-31 20:56:22 +00:00
CLogCmp* c = logcmplist;
while (c)
{
if ((c->pos == pos) && (c->in == in) && !strcmp(c->text.c_str(), filter))
2004-01-31 20:56:22 +00:00
return c;
c = c->next;
}
return logcmplist = new CLogCmp(filter, in, pos, logcmplist, this);
2004-01-31 20:56:22 +00:00
}
void LogEventsMngr::CLogEvent::registerFilter(char* filter)
{
CLogCmp *cmp = parent->registerCondition(filter);
if (cmp == 0) return;
for (LogCond* c = filters; c; c = c->next)
{
if (c->argnum == cmp->pos)
{
c->list = new LogCondEle(cmp, c->list);
2004-01-31 20:56:22 +00:00
return;
}
}
LogCondEle* aa = new LogCondEle(cmp, 0);
2005-09-16 23:48:51 +00:00
if (aa == 0)
return;
filters = new LogCond(cmp->pos, aa, filters);
2004-01-31 20:56:22 +00:00
}
void LogEventsMngr::setLogString(const char* frmt, va_list& vaptr)
{
2004-01-31 20:56:22 +00:00
++logCounter;
int len = vsnprintf(logString, 255, frmt, vaptr);
if (len == - 1)
{
2004-01-31 20:56:22 +00:00
len = 255;
logString[len] = 0;
}
2005-09-16 23:48:51 +00:00
if (len)
logString[--len] = 0;
2004-01-31 20:56:22 +00:00
logArgc = 0;
}
void LogEventsMngr::setLogString(const char* frmt, ...)
{
2004-01-31 20:56:22 +00:00
++logCounter;
va_list logArgPtr;
va_start(logArgPtr, frmt);
int len = vsnprintf(logString, 255, frmt, logArgPtr);
if (len == - 1)
{
2004-01-31 20:56:22 +00:00
len = 255;
logString[len] = 0;
}
va_end(logArgPtr);
2005-09-16 23:48:51 +00:00
if (len)
logString[--len] = 0;
2004-01-31 20:56:22 +00:00
logArgc = 0;
}
void LogEventsMngr::parseLogString()
{
register const char* b = logString;
register int a;
while (*b && logArgc < MAX_LOGARGS)
{
a = 0;
if (*b == '"')
{
++b;
while (*b && *b != '"' && a < 127)
logArgs[logArgc][a++] = *b++;
logArgs[logArgc++][a] = 0;
if (*b) b+=2; // thanks to double terminator
}
else if (*b == '(')
{
++b;
while (*b && *b != ')' && a < 127)
logArgs[logArgc][a++] = *b++;
logArgs[logArgc++][a] = 0;
if (*b) b+=2;
} else {
while (*b && *b != '(' && *b != '"' && a < 127)
logArgs[logArgc][a++] = *b++;
if (*b) --a;
logArgs[logArgc++][a] = 0;
}
}
2004-01-31 20:56:22 +00:00
}
LogEventsMngr::CLogEvent* LogEventsMngr::registerLogEvent(CPluginMngr::CPlugin* plugin, int func, int pos)
2004-01-31 20:56:22 +00:00
{
if (pos < 1 || pos > MAX_LOGARGS)
return 0;
arelogevents = true;
CLogEvent** d = &logevents[pos];
2005-09-16 23:48:51 +00:00
while (*d)
d = &(*d)->next;
return *d = new CLogEvent(plugin, func, this);
2004-01-31 20:56:22 +00:00
}
void LogEventsMngr::executeLogEvents()
{
2005-09-16 23:48:51 +00:00
bool valid;
for (CLogEvent* a = logevents[logArgc]; a; a = a->next)
2004-10-03 07:04:08 +00:00
{
2004-01-31 20:56:22 +00:00
valid = true;
for (CLogEvent::LogCond* b = a->filters; b; b = b->next)
2004-10-03 07:04:08 +00:00
{
2004-01-31 20:56:22 +00:00
valid = false;
for (CLogEvent::LogCondEle* c = b->list; c; c = c->next)
{
if (c->cmp->compareCondition(logArgs[b->argnum]) == 0)
{
2004-01-31 20:56:22 +00:00
valid = true;
break;
}
}
2004-10-03 07:04:08 +00:00
if (!valid)
break;
2004-01-31 20:56:22 +00:00
}
2004-10-03 07:04:08 +00:00
if (valid)
2004-01-31 20:56:22 +00:00
{
executeForwards(a->func);
2004-01-31 20:56:22 +00:00
}
2005-09-16 23:48:51 +00:00
}
2004-01-31 20:56:22 +00:00
}
void LogEventsMngr::clearLogEvents()
{
logCurrent = logCounter = 0;
arelogevents = false;
for (int i = 0; i < MAX_LOGARGS + 1; ++i)
{
CLogEvent **a = &logevents[i];
while (*a)
{
CLogEvent* bb = (*a)->next;
delete *a;
*a = bb;
}
}
clearConditions();
2004-01-31 20:56:22 +00:00
}
void LogEventsMngr::clearConditions()
{
while (logcmplist)
{
CLogCmp* a = logcmplist->next;
delete logcmplist;
logcmplist = a;
}
2004-01-31 20:56:22 +00:00
}
LogEventsMngr::CLogEvent::LogCond::~LogCond()
2005-09-16 23:48:51 +00:00
{
while (list)
{
2004-01-31 20:56:22 +00:00
LogCondEle* cc = list->next;
delete list;
list = cc;
}
}
LogEventsMngr::CLogEvent::~CLogEvent()
{
while (filters)
{
2004-01-31 20:56:22 +00:00
LogCond* cc = filters->next;
delete filters;
filters = cc;
}
}
LogEventsMngr::CLogEvent *LogEventsMngr::getValidLogEvent(CLogEvent * a)
2004-01-31 20:56:22 +00:00
{
bool valid;
while (a)
{
2004-01-31 20:56:22 +00:00
valid = true;
for (CLogEvent::LogCond* b = a->filters; b; b = b->next)
{
2004-01-31 20:56:22 +00:00
valid = false;
for (CLogEvent::LogCondEle* c = b->list; c; c = c->next)
{
if (c->cmp->compareCondition(logArgs[b->argnum]) == 0)
{
2004-01-31 20:56:22 +00:00
valid = true;
break;
}
}
2004-01-31 20:56:22 +00:00
if (!valid) break;
}
if (!valid)
{
2004-01-31 20:56:22 +00:00
a = a->next;
continue;
}
2004-01-31 20:56:22 +00:00
return a;
}
2004-01-31 20:56:22 +00:00
return 0;
2004-09-15 21:27:35 +00:00
}