2 This file is part of libdaemon.
4 Copyright 2003-2008 Lennart Poettering
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.
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.
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/>.
32 enum daemon_log_flags daemon_log_use = DAEMON_LOG_AUTO|DAEMON_LOG_STDERR;
33 const char* daemon_log_ident = NULL;
35 static int daemon_verbosity_level = LOG_INFO;
37 void daemon_set_verbosity(int verbosity_prio) {
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);
43 daemon_verbosity_level = verbosity_prio & LOG_PRIMASK;
46 void daemon_logv(int prio, const char* template, va_list arglist) {
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);
56 if (prio > daemon_verbosity_level)
59 if (daemon_log_use & DAEMON_LOG_STDERR) {
60 vfprintf(stderr, template, arglist);
61 fprintf(stderr, "\n");
64 if (daemon_log_use & DAEMON_LOG_STDOUT) {
65 vfprintf(stdout, template, arglist);
66 fprintf(stdout, "\n");
73 void daemon_log(int prio, const char* template, ...) {
76 va_start(arglist, template);
77 daemon_logv(prio, template, arglist);
81 char *daemon_ident_from_argv0(char *argv0) {
84 if ((p = strrchr(argv0, '/')))