X-Git-Url: http://git.meshlink.io/?p=meshlink;a=blobdiff_plain;f=src%2Flogger.c;h=78d629ee2e5305a57e812522e7bb79c04f5ed1d2;hp=8029565ff43da5260a51eb219692148ac51fecb8;hb=963c5055505f2fc117cd5efa06eaa02c9b2bf85d;hpb=1401faf608e1c8af0d0754e545b0ec79d2bd5d93 diff --git a/src/logger.c b/src/logger.c index 8029565f..78d629ee 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-2017 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,45 @@ 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" + +// TODO: refactor logging code to use a meshlink_handle_t *. +void logger(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *format, ...) { + assert(format); + + if(mesh) { + if(level < mesh->log_level || !mesh->log_cb) { + return; + } + } else { + if(level < global_log_level || !global_log_cb) { + return; + } + } -#include "system.h" - -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 && (size_t)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; + if(mesh) { + mesh->log_cb(mesh, level, message); + } else { + global_log_cb(NULL, level, message); } }