2003-07-06 22:11:37 +00:00
|
|
|
/*
|
|
|
|
logger.c -- logging code
|
2006-04-26 13:52:58 +00:00
|
|
|
Copyright (C) 2004-2006 Guus Sliepen <guus@tinc-vpn.org>
|
|
|
|
2004-2005 Ivo Timmermans
|
2003-07-06 22:11:37 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2009-09-24 22:01:00 +00:00
|
|
|
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.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
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"
|
2012-02-26 17:37:36 +00:00
|
|
|
#include "meta.h"
|
2003-07-06 22:11:37 +00:00
|
|
|
#include "logger.h"
|
2012-02-26 17:37:36 +00:00
|
|
|
#include "connection.h"
|
|
|
|
#include "control_common.h"
|
2003-07-06 22:11:37 +00:00
|
|
|
|
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;
|
2012-02-26 17:37:36 +00:00
|
|
|
bool logcontrol = false;
|
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");
|
2011-06-06 14:26:11 +00:00
|
|
|
if(!logfile) {
|
|
|
|
fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
|
2003-07-06 22:11:37 +00:00
|
|
|
logmode = LOGMODE_NULL;
|
2011-06-06 14:26:11 +00:00
|
|
|
}
|
2003-07-06 22:11:37 +00:00
|
|
|
break;
|
|
|
|
case LOGMODE_SYSLOG:
|
2003-08-17 09:04:00 +00:00
|
|
|
#ifdef HAVE_MINGW
|
2003-08-17 12:04:35 +00:00
|
|
|
loghandle = RegisterEventSource(NULL, logident);
|
2011-06-06 14:26:11 +00:00
|
|
|
if(!loghandle) {
|
|
|
|
fprintf(stderr, "Could not open log handle!");
|
2003-08-17 09:04:00 +00:00
|
|
|
logmode = LOGMODE_NULL;
|
2011-06-06 14:26:11 +00:00
|
|
|
}
|
2003-08-17 09:04:00 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-06 14:26:11 +00:00
|
|
|
void reopenlogger() {
|
|
|
|
if(logmode != LOGMODE_FILE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fflush(logfile);
|
|
|
|
FILE *newfile = fopen(logfilename, "a");
|
|
|
|
if(!newfile) {
|
2012-03-26 18:06:39 +00:00
|
|
|
logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
|
2011-06-06 14:26:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fclose(logfile);
|
|
|
|
logfile = newfile;
|
|
|
|
}
|
|
|
|
|
2012-02-26 17:37:36 +00:00
|
|
|
void logger(int level, int priority, const char *format, ...) {
|
2003-07-12 17:41:48 +00:00
|
|
|
va_list ap;
|
2011-06-06 14:26:11 +00:00
|
|
|
char timestr[32] = "";
|
2012-02-26 17:37:36 +00:00
|
|
|
char message[1024] = "";
|
2011-06-06 14:26:11 +00:00
|
|
|
time_t now;
|
2012-02-26 17:37:36 +00:00
|
|
|
static bool suppress = false;
|
|
|
|
|
|
|
|
// Bail out early if there is nothing to do.
|
|
|
|
if(suppress)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL))
|
|
|
|
return;
|
2003-07-12 17:41:48 +00:00
|
|
|
|
|
|
|
va_start(ap, format);
|
2012-02-26 17:37:36 +00:00
|
|
|
vsnprintf(message, sizeof message, format, ap);
|
|
|
|
va_end(ap);
|
2003-07-12 17:41:48 +00:00
|
|
|
|
2012-02-26 17:37:36 +00:00
|
|
|
if(level <= debug_level) {
|
|
|
|
switch(logmode) {
|
|
|
|
case LOGMODE_STDERR:
|
|
|
|
fprintf(stderr, "%s\n", message);
|
|
|
|
fflush(stderr);
|
|
|
|
break;
|
|
|
|
case LOGMODE_FILE:
|
|
|
|
now = time(NULL);
|
|
|
|
strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now));
|
|
|
|
fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
|
|
|
|
fflush(logfile);
|
|
|
|
break;
|
|
|
|
case LOGMODE_SYSLOG:
|
2003-08-17 09:04:00 +00:00
|
|
|
#ifdef HAVE_MINGW
|
2012-02-26 17:37:36 +00:00
|
|
|
{
|
|
|
|
const char *messages[] = {message};
|
|
|
|
ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
|
|
|
|
}
|
2003-08-17 09:04:00 +00:00
|
|
|
#else
|
2003-07-29 10:50:15 +00:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
2003-07-06 22:11:37 +00:00
|
|
|
syslog(priority, "%s", message);
|
2003-08-17 09:04:00 +00:00
|
|
|
#endif
|
2003-07-28 22:06:09 +00:00
|
|
|
#endif
|
2012-02-26 17:37:36 +00:00
|
|
|
break;
|
|
|
|
case LOGMODE_NULL:
|
|
|
|
break;
|
|
|
|
}
|
2003-07-06 22:11:37 +00:00
|
|
|
}
|
2003-07-12 17:41:48 +00:00
|
|
|
|
2012-02-26 17:37:36 +00:00
|
|
|
if(logcontrol) {
|
|
|
|
suppress = true;
|
|
|
|
logcontrol = false;
|
|
|
|
for(splay_node_t *node = connection_tree->head; node; node = node->next) {
|
|
|
|
connection_t *c = node->data;
|
|
|
|
if(!c->status.log)
|
|
|
|
continue;
|
|
|
|
logcontrol = true;
|
|
|
|
if(level > (c->outcompression >= 0 ? c->outcompression : debug_level))
|
|
|
|
continue;
|
|
|
|
int len = strlen(message);
|
|
|
|
if(send_request(c, "%d %d %d", CONTROL, REQ_LOG, len))
|
|
|
|
send_meta(c, message, len);
|
|
|
|
}
|
|
|
|
suppress = false;
|
|
|
|
}
|
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
|
2003-08-17 12:04:35 +00:00
|
|
|
DeregisterEventSource(loghandle);
|
2003-08-17 09:04:00 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|