Files
assaultcube-/source/src/log.cpp
T
Lee Salzman a0f6c067fe warning fixes
git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@3497 e016b1c5-ed68-44aa-a2f7-31cac88cffde
2008-11-15 00:14:25 +00:00

121 lines
2.1 KiB
C++

// server-side logging of events
#include "pch.h"
#include "cube.h"
struct filelog : log
{
FILE *file;
string filepath;
filelog(const char *filepath)
{
s_strcpy(this->filepath, filepath);
file = NULL;
}
~filelog()
{
close();
}
virtual void writeline(int level, const char *msg, ...)
{
if(!enabled) return;
s_sprintfdv(sf, msg);
filtertext(sf, sf, 2);
if(console)
{
puts(sf);
fflush(stdout);
}
if(file)
{
fprintf(file, "%s\n", sf);
fflush(file);
}
}
virtual void open()
{
if(file) return;
file = fopen(filepath, "w");
if(file) enabled = true;
}
virtual void close()
{
if(!file) return;
fclose(file);
enabled = false;
}
};
#if !defined(WIN32) && !defined(__APPLE__)
#include <syslog.h>
#include <signal.h>
struct posixsyslog : log
{
int facility;
string ident;
posixsyslog(int facility, const char *ident)
{
this->facility = facility;
s_strcpy(this->ident, ident);
}
~posixsyslog()
{
close();
}
virtual void writeline(int level, const char *msg, ...)
{
if(!enabled) return;
s_sprintfdv(sf, msg);
filtertext(sf, sf, 2);
int l = (level==log::info ? LOG_INFO : ( level==log::warning ? LOG_WARNING : LOG_ERR));
syslog(l, "%s", sf);
if(console)
{
puts(sf);
fflush(stdout);
}
}
virtual void open()
{
if(enabled) return;
openlog(ident, LOG_NDELAY, facility);
enabled = true;
}
virtual void close()
{
if(!enabled) return;
closelog();
enabled = false;
}
};
struct log *newlogger(const char *identity)
{
s_sprintfd(id)("AssaultCube %s", identity);
return new posixsyslog(LOG_LOCAL6, id);
}
#else
struct log *newlogger(const char *identity)
{
s_sprintfd(file)("serverlog_%s.txt", identity);
return new filelog(file);
}
#endif