From eeb3cd677dbd847c953bd21471e76e54d9eb8e44 Mon Sep 17 00:00:00 2001 From: Pawel Jakub Dawidek Date: Thu, 27 Jan 2011 19:24:07 +0000 Subject: [PATCH] Add functions to initialize/finalize pjdlog. This allows to open/close log file at will. MFC after: 1 week --- sbin/hastctl/hastctl.c | 1 + sbin/hastd/hastd.c | 1 + sbin/hastd/pjdlog.c | 67 +++++++++++++++++++++++++++++++++++++++++- sbin/hastd/pjdlog.h | 4 +++ 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/sbin/hastctl/hastctl.c b/sbin/hastctl/hastctl.c index d19b541e00b3..80b4e49628b0 100644 --- a/sbin/hastctl/hastctl.c +++ b/sbin/hastctl/hastctl.c @@ -430,6 +430,7 @@ main(int argc, char *argv[]) break; } + pjdlog_init(PJDLOG_MODE_STD); pjdlog_debug_set(debug); cfg = yy_config_parse(cfgpath, true); diff --git a/sbin/hastd/hastd.c b/sbin/hastd/hastd.c index 30478cad60ab..37c2bf5cc931 100644 --- a/sbin/hastd/hastd.c +++ b/sbin/hastd/hastd.c @@ -772,6 +772,7 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; + pjdlog_init(PJDLOG_MODE_STD); pjdlog_debug_set(debuglevel); g_gate_load(); diff --git a/sbin/hastd/pjdlog.c b/sbin/hastd/pjdlog.c index 2d62412ee7c1..61530f3dd812 100644 --- a/sbin/hastd/pjdlog.c +++ b/sbin/hastd/pjdlog.c @@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -41,10 +42,37 @@ __FBSDID("$FreeBSD$"); #include "pjdlog.h" +static bool pjdlog_initialized = false; static int pjdlog_mode = PJDLOG_MODE_STD; static int pjdlog_debug_level = 0; static char pjdlog_prefix[128]; +void +pjdlog_init(int mode) +{ + + assert(!pjdlog_initialized); + assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG); + + if (mode == PJDLOG_MODE_SYSLOG) + openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON); + pjdlog_mode = mode; + + pjdlog_initialized = true; +} + +void +pjdlog_fini(void) +{ + + assert(pjdlog_initialized); + + if (pjdlog_mode == PJDLOG_MODE_SYSLOG) + closelog(); + + pjdlog_initialized = false; +} + /* * Configure where the logs should go. * By default they are send to stdout/stderr, but after going into background @@ -55,12 +83,18 @@ void pjdlog_mode_set(int mode) { + assert(pjdlog_initialized); assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG); - pjdlog_mode = mode; + if (pjdlog_mode == mode) + return; if (mode == PJDLOG_MODE_SYSLOG) openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON); + else /* if (mode == PJDLOG_MODE_STD) */ + closelog(); + + pjdlog_mode = mode; } /* @@ -70,6 +104,8 @@ int pjdlog_mode_get(void) { + assert(pjdlog_initialized); + return (pjdlog_mode); } @@ -81,6 +117,7 @@ void pjdlog_debug_set(int level) { + assert(pjdlog_initialized); assert(level >= 0); pjdlog_debug_level = level; @@ -93,6 +130,8 @@ int pjdlog_debug_get(void) { + assert(pjdlog_initialized); + return (pjdlog_debug_level); } @@ -105,6 +144,8 @@ pjdlog_prefix_set(const char *fmt, ...) { va_list ap; + assert(pjdlog_initialized); + va_start(ap, fmt); pjdlogv_prefix_set(fmt, ap); va_end(ap); @@ -118,6 +159,7 @@ void pjdlogv_prefix_set(const char *fmt, va_list ap) { + assert(pjdlog_initialized); assert(fmt != NULL); vsnprintf(pjdlog_prefix, sizeof(pjdlog_prefix), fmt, ap); @@ -160,6 +202,8 @@ pjdlog_common(int loglevel, int debuglevel, int error, const char *fmt, ...) { va_list ap; + assert(pjdlog_initialized); + va_start(ap, fmt); pjdlogv_common(loglevel, debuglevel, error, fmt, ap); va_end(ap); @@ -174,6 +218,7 @@ pjdlogv_common(int loglevel, int debuglevel, int error, const char *fmt, va_list ap) { + assert(pjdlog_initialized); assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT || loglevel == LOG_CRIT || loglevel == LOG_ERR || loglevel == LOG_WARNING || loglevel == LOG_NOTICE || @@ -250,6 +295,8 @@ void pjdlogv(int loglevel, const char *fmt, va_list ap) { + assert(pjdlog_initialized); + /* LOG_DEBUG is invalid here, pjdlogv?_debug() should be used. */ assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT || loglevel == LOG_CRIT || loglevel == LOG_ERR || @@ -267,6 +314,8 @@ pjdlog(int loglevel, const char *fmt, ...) { va_list ap; + assert(pjdlog_initialized); + va_start(ap, fmt); pjdlogv(loglevel, fmt, ap); va_end(ap); @@ -279,6 +328,8 @@ void pjdlogv_debug(int debuglevel, const char *fmt, va_list ap) { + assert(pjdlog_initialized); + pjdlogv_common(LOG_DEBUG, debuglevel, -1, fmt, ap); } @@ -290,6 +341,8 @@ pjdlog_debug(int debuglevel, const char *fmt, ...) { va_list ap; + assert(pjdlog_initialized); + va_start(ap, fmt); pjdlogv_debug(debuglevel, fmt, ap); va_end(ap); @@ -302,6 +355,8 @@ void pjdlogv_errno(int loglevel, const char *fmt, va_list ap) { + assert(pjdlog_initialized); + pjdlogv_common(loglevel, 0, errno, fmt, ap); } @@ -313,6 +368,8 @@ pjdlog_errno(int loglevel, const char *fmt, ...) { va_list ap; + assert(pjdlog_initialized); + va_start(ap, fmt); pjdlogv_errno(loglevel, fmt, ap); va_end(ap); @@ -325,6 +382,8 @@ void pjdlogv_exit(int exitcode, const char *fmt, va_list ap) { + assert(pjdlog_initialized); + pjdlogv_errno(LOG_ERR, fmt, ap); exit(exitcode); /* NOTREACHED */ @@ -338,6 +397,8 @@ pjdlog_exit(int exitcode, const char *fmt, ...) { va_list ap; + assert(pjdlog_initialized); + va_start(ap, fmt); pjdlogv_exit(exitcode, fmt, ap); /* NOTREACHED */ @@ -351,6 +412,8 @@ void pjdlogv_exitx(int exitcode, const char *fmt, va_list ap) { + assert(pjdlog_initialized); + pjdlogv(LOG_ERR, fmt, ap); exit(exitcode); /* NOTREACHED */ @@ -364,6 +427,8 @@ pjdlog_exitx(int exitcode, const char *fmt, ...) { va_list ap; + assert(pjdlog_initialized); + va_start(ap, fmt); pjdlogv_exitx(exitcode, fmt, ap); /* NOTREACHED */ diff --git a/sbin/hastd/pjdlog.h b/sbin/hastd/pjdlog.h index b81efa6d90f7..e9d1740a6703 100644 --- a/sbin/hastd/pjdlog.h +++ b/sbin/hastd/pjdlog.h @@ -1,5 +1,6 @@ /*- * Copyright (c) 2009-2010 The FreeBSD Foundation + * Copyright (c) 2011 Pawel Jakub Dawidek * All rights reserved. * * This software was developed by Pawel Jakub Dawidek under sponsorship from @@ -41,6 +42,9 @@ #define PJDLOG_MODE_STD 0 #define PJDLOG_MODE_SYSLOG 1 +void pjdlog_init(int mode); +void pjdlog_fini(void); + void pjdlog_mode_set(int mode); int pjdlog_mode_get(void);