X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Flogger.c;h=ea29e3f69c3414e9d8b6278573bd2bc724cf614d;hb=c3e43398c1d6a31697ea70be30b40e936d4e4e9b;hp=8029565ff43da5260a51eb219692148ac51fecb8;hpb=1401faf608e1c8af0d0754e545b0ec79d2bd5d93;p=meshlink diff --git a/src/logger.c b/src/logger.c index 8029565f..ea29e3f6 100644 --- a/src/logger.c +++ b/src/logger.c @@ -1,7 +1,6 @@ /* logger.c -- logging code - Copyright (C) 2003 Guus Sliepen - 2003 Ivo Timmermans + Copyright (C) 2014 Guus Sliepen 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 @@ -13,82 +12,31 @@ 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. - - $Id: logger.c,v 1.1.2.2 2003/07/06 23:16:28 guus Exp $ + 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. */ -#include -#include -#include -#include +#include "system.h" -#include "conf.h" #include "logger.h" +#include "meshlink_internal.h" +#include "sptps.h" -#include "system.h" +// TODO: refactor logging code to use a meshlink_handle_t *. +void logger(int level, int priority, const char *format, ...) { + //if(level > mesh->debug_level) + // return; -volatile int debug_level = DEBUG_NOTHING; -static int logmode = LOGMODE_STDERR; -static pid_t logpid; -extern char *logfilename; -static FILE *logfile = NULL; -static const char *logident = NULL; + va_list ap; + char message[1024] = ""; -void openlogger(const char *ident, int mode) { - 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: - openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON); - break; - } -} + va_start(ap, format); + int len = vsnprintf(message, sizeof message, format, ap); + va_end(ap); -void vlogger(int priority, const char *format, va_list ap) { - switch(logmode) { - case LOGMODE_STDERR: - vfprintf(stderr, format, ap); - fprintf(stderr, "\n"); - break; - case LOGMODE_FILE: - fprintf(logfile, "%ld %s[%d]: ", time(NULL), logident, logpid); - vfprintf(logfile, format, ap); - fprintf(logfile, "\n"); - break; - case LOGMODE_SYSLOG: -#ifdef HAVE_VSYSLOG - vsyslog(priority, format, ap); -#else - { - char message[4096]; - vsnprintf(message, sizeof(message), format, ap); - syslog(priority, "%s", message); - } -#endif - break; - } -} + if(len > 0 && len < sizeof message && message[len - 1] == '\n') + message[len - 1] = 0; -void closelogger(void) { - switch(logmode) { - case LOGMODE_FILE: - fclose(logfile); - break; - case LOGMODE_SYSLOG: - closelog(); - break; - } + fprintf(stderr, "%s\n", message); }