/*
logger.c -- logging code
- Copyright (C) 2004-2012 Guus Sliepen <guus@tinc-vpn.org>
- 2004-2005 Ivo Timmermans
+ Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
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
#include "meta.h"
#include "logger.h"
#include "connection.h"
-#include "control_common.h"
#include "sptps.h"
debug_t debug_level = DEBUG_NOTHING;
static logmode_t logmode = LOGMODE_STDERR;
static pid_t logpid;
-extern char *logfilename;
-static FILE *logfile = NULL;
#ifdef HAVE_MINGW
static HANDLE loghandle = NULL;
#endif
static const char *logident = NULL;
-bool logcontrol = false;
-
static void real_logger(int level, int priority, const char *message) {
char timestr[32] = "";
if(suppress)
return;
- if(!logcontrol && (level > debug_level || logmode == LOGMODE_NULL))
+ if(level > debug_level || logmode == LOGMODE_NULL)
return;
if(level <= debug_level) {
fprintf(stderr, "%s\n", message);
fflush(stderr);
break;
- case LOGMODE_FILE:
- if(!now.tv_sec)
- gettimeofday(&now, NULL);
- strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now.tv_sec));
- fprintf(logfile, "%s %s[%ld]: %s\n", timestr, logident, (long)logpid, message);
- fflush(logfile);
- break;
case LOGMODE_SYSLOG:
#ifdef HAVE_MINGW
{
break;
}
}
-
- if(logcontrol) {
- suppress = true;
- logcontrol = false;
- for list_each(connection_t, c, connection_list) {
- 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;
- }
}
void logger(int level, int priority, const char *format, ...) {
case LOGMODE_STDERR:
logpid = getpid();
break;
- case LOGMODE_FILE:
- logpid = getpid();
- logfile = fopen(logfilename, "a");
- if(!logfile) {
- fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
- logmode = LOGMODE_NULL;
- }
- break;
case LOGMODE_SYSLOG:
#ifdef HAVE_MINGW
loghandle = RegisterEventSource(NULL, logident);
sptps_log = sptps_log_quiet;
}
-void reopenlogger() {
- if(logmode != LOGMODE_FILE)
- return;
-
- fflush(logfile);
- FILE *newfile = fopen(logfilename, "a");
- if(!newfile) {
- logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
- return;
- }
- fclose(logfile);
- logfile = newfile;
-}
-
-
void closelogger(void) {
switch(logmode) {
- case LOGMODE_FILE:
- fclose(logfile);
- break;
case LOGMODE_SYSLOG:
#ifdef HAVE_MINGW
DeregisterEventSource(loghandle);