2003-07-06 22:11:37 +00:00
|
|
|
/*
|
|
|
|
logger.c -- logging code
|
|
|
|
Copyright (C) 2003 Guus Sliepen <guus@sliepen.eu.org>
|
|
|
|
2003 Ivo Timmermans <ivo@o2w.nl>
|
|
|
|
|
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
2003-08-17 09:04:00 +00:00
|
|
|
$Id: logger.c,v 1.1.2.10 2003/08/17 09:04:00 guus Exp $
|
2003-07-06 22:11:37 +00:00
|
|
|
*/
|
|
|
|
|
2003-07-17 15:06:27 +00:00
|
|
|
#include "system.h"
|
2003-07-06 22:11:37 +00:00
|
|
|
|
|
|
|
#include "conf.h"
|
|
|
|
#include "logger.h"
|
|
|
|
|
2003-07-22 20:55:21 +00:00
|
|
|
debug_t debug_level = DEBUG_NOTHING;
|
|
|
|
static logmode_t logmode = LOGMODE_STDERR;
|
2003-07-06 23:16:29 +00:00
|
|
|
static pid_t logpid;
|
2003-07-06 22:11:37 +00:00
|
|
|
extern char *logfilename;
|
2003-07-06 23:16:29 +00:00
|
|
|
static FILE *logfile = NULL;
|
2003-08-17 09:04:00 +00:00
|
|
|
#ifdef HAVE_MINGW
|
|
|
|
static HANDLE loghandle = NULL;
|
|
|
|
#endif
|
2003-07-06 23:16:29 +00:00
|
|
|
static const char *logident = NULL;
|
2003-07-06 22:11:37 +00:00
|
|
|
|
2003-07-22 20:55:21 +00:00
|
|
|
void openlogger(const char *ident, logmode_t mode) {
|
2003-07-06 22:11:37 +00:00
|
|
|
logident = ident;
|
|
|
|
logmode = mode;
|
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
case LOGMODE_STDERR:
|
|
|
|
logpid = getpid();
|
|
|
|
break;
|
|
|
|
case LOGMODE_FILE:
|
|
|
|
logpid = getpid();
|
|
|
|
logfile = fopen(logfilename, "a");
|
|
|
|
if(!logfile)
|
|
|
|
logmode = LOGMODE_NULL;
|
|
|
|
break;
|
|
|
|
case LOGMODE_SYSLOG:
|
2003-08-17 09:04:00 +00:00
|
|
|
#ifdef HAVE_MINGW
|
|
|
|
loghandle = OpenEventLog(NULL, identname);
|
|
|
|
if(!loghandle)
|
|
|
|
logmode = LOGMODE_NULL;
|
|
|
|
break;
|
|
|
|
#else
|
2003-07-29 10:50:15 +00:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
2003-07-06 22:11:37 +00:00
|
|
|
openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
|
|
|
|
break;
|
2003-08-17 09:04:00 +00:00
|
|
|
#endif
|
2003-07-28 22:06:09 +00:00
|
|
|
#endif
|
|
|
|
case LOGMODE_NULL:
|
|
|
|
break;
|
2003-07-06 22:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-12 17:41:48 +00:00
|
|
|
void logger(int priority, const char *format, ...) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
2003-07-06 22:11:37 +00:00
|
|
|
switch(logmode) {
|
|
|
|
case LOGMODE_STDERR:
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
fprintf(stderr, "\n");
|
2003-07-29 22:59:01 +00:00
|
|
|
fflush(stderr);
|
2003-07-06 22:11:37 +00:00
|
|
|
break;
|
|
|
|
case LOGMODE_FILE:
|
2003-07-06 23:16:29 +00:00
|
|
|
fprintf(logfile, "%ld %s[%d]: ", time(NULL), logident, logpid);
|
2003-07-06 22:11:37 +00:00
|
|
|
vfprintf(logfile, format, ap);
|
|
|
|
fprintf(logfile, "\n");
|
2003-08-02 20:50:38 +00:00
|
|
|
fflush(logfile);
|
2003-07-06 22:11:37 +00:00
|
|
|
break;
|
|
|
|
case LOGMODE_SYSLOG:
|
2003-08-17 09:04:00 +00:00
|
|
|
#ifdef HAVE_MINGW
|
|
|
|
{
|
|
|
|
char message[4096];
|
|
|
|
vsnprintf(message, sizeof(message), format, ap);
|
|
|
|
ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, &message, NULL);
|
|
|
|
}
|
|
|
|
#else
|
2003-07-29 10:50:15 +00:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
2003-07-06 22:11:37 +00:00
|
|
|
#ifdef HAVE_VSYSLOG
|
|
|
|
vsyslog(priority, format, ap);
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
char message[4096];
|
|
|
|
vsnprintf(message, sizeof(message), format, ap);
|
|
|
|
syslog(priority, "%s", message);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
2003-08-17 09:04:00 +00:00
|
|
|
#endif
|
2003-07-28 22:06:09 +00:00
|
|
|
#endif
|
|
|
|
case LOGMODE_NULL:
|
|
|
|
break;
|
2003-07-06 22:11:37 +00:00
|
|
|
}
|
2003-07-12 17:41:48 +00:00
|
|
|
|
|
|
|
va_end(ap);
|
2003-07-06 22:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void closelogger(void) {
|
|
|
|
switch(logmode) {
|
|
|
|
case LOGMODE_FILE:
|
|
|
|
fclose(logfile);
|
|
|
|
break;
|
|
|
|
case LOGMODE_SYSLOG:
|
2003-08-17 09:04:00 +00:00
|
|
|
#ifdef HAVE_MINGW
|
|
|
|
CloseEventLog(loghandle);
|
|
|
|
break;
|
|
|
|
#else
|
2003-07-29 10:50:15 +00:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
2003-07-06 22:11:37 +00:00
|
|
|
closelog();
|
|
|
|
break;
|
2003-08-17 09:04:00 +00:00
|
|
|
#endif
|
2003-07-28 22:06:09 +00:00
|
|
|
#endif
|
|
|
|
case LOGMODE_NULL:
|
|
|
|
case LOGMODE_STDERR:
|
|
|
|
break;
|
|
|
|
break;
|
2003-07-06 22:11:37 +00:00
|
|
|
}
|
|
|
|
}
|