Add functions to initialize/finalize pjdlog. This allows to open/close log

file at will.

MFC after:	1 week
This commit is contained in:
Pawel Jakub Dawidek 2011-01-27 19:24:07 +00:00
parent 6ef7ddd788
commit eeb3cd677d
4 changed files with 72 additions and 1 deletions

View File

@ -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);

View File

@ -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();

View File

@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$");
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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 */

View File

@ -1,5 +1,6 @@
/*-
* Copyright (c) 2009-2010 The FreeBSD Foundation
* Copyright (c) 2011 Pawel Jakub Dawidek <pjd@FreeBSD.org>
* 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);