syslog.c

00001 /*
00002  * Copyright (c) 2005-2012 by KoanLogic s.r.l. <http://www.koanlogic.com>
00003  * All rights reserved.
00004  *
00005  * This file is part of KLone, and as such it is subject to the license stated
00006  * in the LICENSE file which you have received as part of this distribution.
00007  *
00008  * $Id: syslog.c,v 1.7 2006/01/09 12:38:38 tat Exp $
00009  */
00010 
00011 #include "klone_conf.h"
00012 #include <time.h>
00013 #include <u/libu.h>
00014 #include <klone/klog.h>
00015 #include <klone/klogprv.h>
00016 
00017 static int klog_to_syslog (int lev);
00018 static void klog_free_syslog (klog_syslog_t *kls);
00019 
00020 static int klog_syslog (klog_t *kl, int level, const char *fmt, va_list ap);
00021 static void klog_close_syslog (klog_t *kl);
00022 
00023 /* must be ordered */
00024 static int sysloglev[] =
00025 {
00026     LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING, 
00027     LOG_ERR, LOG_CRIT, LOG_ALERT, LOG_EMERG
00028 };  
00029 
00030 /* we've a problem here because syslog(3) mantains a global context, and
00031  * we can't bind some syslog state specific to the supplied klog_t object.
00032  * instead we are forced to do openlog->vsyslog->closelog for each logged
00033  * message which is rather silly ... anyway this should be not so heavy as
00034  * it seems: in fact (a) the underlying transport is connectionless (UDP) 
00035  * and (b) most of the time is spent inside the vsyslog call. */
00036 int klog_open_syslog (klog_t *kl, int fac, int logopt)
00037 {
00038     klog_syslog_t *kls = NULL;
00039     
00040     dbg_return_if (kl == NULL, ~0);
00041 
00042     kls = u_zalloc(sizeof(klog_syslog_t));
00043     dbg_err_if (kls == NULL);
00044 
00045     kls->facility = fac;
00046     kls->logopt = logopt;
00047     
00048     /* set private methods */
00049     kl->cb_log = klog_syslog;
00050     kl->cb_close = klog_close_syslog;
00051     kl->cb_getln = NULL;
00052     kl->cb_countln = NULL;
00053     kl->cb_clear = NULL;
00054     kl->cb_flush = NULL;
00055 
00056     /* stick child to its parent */
00057     kl->u.s = kls, kls = NULL;
00058 
00059     return 0;
00060 err:
00061     if (kls)
00062         klog_free_syslog(kls);
00063     return ~0;
00064 }
00065 
00066 static void klog_close_syslog (klog_t *kl)
00067 {
00068     if (kl == NULL || kl->type != KLOG_TYPE_SYSLOG || kl->u.s == NULL)
00069         return;
00070 
00071     klog_free_syslog(kl->u.s), kl->u.s = NULL;
00072  
00073     return;
00074 }
00075 
00076 static void klog_free_syslog (klog_syslog_t *kls)
00077 {
00078     if (kls == NULL)
00079         return;
00080     U_FREE(kls);
00081     return;
00082 }
00083 
00084 static int klog_syslog (klog_t *kl, int level, const char *fmt, va_list ap)
00085 {
00086     dbg_return_if (kl == NULL, ~0);
00087     dbg_return_if (kl->type != KLOG_TYPE_SYSLOG, ~0);
00088     dbg_return_if (kl->u.s == NULL, ~0);
00089     dbg_return_if (fmt == NULL, ~0);
00090     
00091 #ifdef HAVE_SYSLOG
00092     openlog(kl->ident, kl->u.s->logopt, kl->u.s->facility);
00093     vsyslog(klog_to_syslog(level), fmt, ap);
00094     closelog();
00095 #else
00096     vsyslog(kl->u.s->facility | klog_to_syslog(level), fmt, ap);
00097 #endif /* HAVE_SYSLOG */
00098 
00099     return 0;
00100 }
00101 
00102 static int klog_to_syslog (int lev)
00103 {
00104     return (lev < KLOG_DEBUG || lev > KLOG_EMERG) ? -1 : sysloglev[lev];
00105 }

←Products
Copyright © 2005-2012 - KoanLogic S.r.l. - All rights reserved