1/*** 2 This file is part of libdaemon. 3 4 Copyright 2003-2008 Lennart Poettering 5 6 libdaemon is free software; you can redistribute it and/or modify 7 it under the terms of the GNU Lesser General Public License as 8 published by the Free Software Foundation, either version 2.1 of the 9 License, or (at your option) any later version. 10 11 libdaemon is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with libdaemon. If not, see 18 <http://www.gnu.org/licenses/>. 19***/ 20 21#ifdef HAVE_CONFIG_H 22#include <config.h> 23#endif 24 25#include <stdarg.h> 26#include <stdio.h> 27#include <string.h> 28#include <errno.h> 29 30#include "dlog.h" 31 32enum daemon_log_flags daemon_log_use = DAEMON_LOG_AUTO|DAEMON_LOG_STDERR; 33const char* daemon_log_ident = NULL; 34 35static int daemon_verbosity_level = LOG_INFO; 36 37void daemon_set_verbosity(int verbosity_prio) { 38 39 /* Allow using negative verbosity levels to hide _all_ messages */ 40 if (verbosity_prio > 0 && (verbosity_prio & LOG_PRIMASK) != LOG_PRIMASK) 41 daemon_log(LOG_ERR, "The value %d is not a valid priority value", verbosity_prio); 42 43 daemon_verbosity_level = verbosity_prio & LOG_PRIMASK; 44} 45 46void daemon_logv(int prio, const char* template, va_list arglist) { 47 int saved_errno; 48 49 saved_errno = errno; 50 51 if (daemon_log_use & DAEMON_LOG_SYSLOG) { 52 openlog(daemon_log_ident ? daemon_log_ident : "UNKNOWN", LOG_PID, LOG_DAEMON); 53 vsyslog(prio | LOG_DAEMON, template, arglist); 54 } 55 56 if (prio > daemon_verbosity_level) 57 goto end_daemon_logv; 58 59 if (daemon_log_use & DAEMON_LOG_STDERR) { 60 vfprintf(stderr, template, arglist); 61 fprintf(stderr, "\n"); 62 } 63 64 if (daemon_log_use & DAEMON_LOG_STDOUT) { 65 vfprintf(stdout, template, arglist); 66 fprintf(stdout, "\n"); 67 } 68 69 end_daemon_logv: 70 errno = saved_errno; 71} 72 73void daemon_log(int prio, const char* template, ...) { 74 va_list arglist; 75 76 va_start(arglist, template); 77 daemon_logv(prio, template, arglist); 78 va_end(arglist); 79} 80 81char *daemon_ident_from_argv0(char *argv0) { 82 char *p; 83 84 if ((p = strrchr(argv0, '/'))) 85 return p+1; 86 87 return argv0; 88} 89