1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>KLone: syslog.c Source File</title>
<link href="kl.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.9.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a></div>
<div class="nav">
<a class="el" href="dir_000000.html">src</a> / <a class="el" href="dir_000007.html">libklog</a></div>
<h1>syslog.c</h1><a href="syslog_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
00002 <span class="comment"> * Copyright (c) 2005, 2006 by KoanLogic s.r.l. <http://www.koanlogic.com></span>
00003 <span class="comment"> * All rights reserved.</span>
00004 <span class="comment"> *</span>
00005 <span class="comment"> * This file is part of KLone, and as such it is subject to the license stated</span>
00006 <span class="comment"> * in the LICENSE file which you have received as part of this distribution.</span>
00007 <span class="comment"> *</span>
00008 <span class="comment"> * $Id: syslog.c,v 1.7 2006/01/09 12:38:38 tat Exp $</span>
00009 <span class="comment"> */</span>
00010
00011 <span class="preprocessor">#include "klone_conf.h"</span>
00012 <span class="preprocessor">#include <time.h></span>
00013 <span class="preprocessor">#include <u/libu.h></span>
00014 <span class="preprocessor">#include <<a class="code" href="klog_8h.html">klone/klog.h</a>></span>
00015 <span class="preprocessor">#include <<a class="code" href="klogprv_8h.html">klone/klogprv.h</a>></span>
00016
00017 <span class="keyword">static</span> <span class="keywordtype">int</span> klog_to_syslog (<span class="keywordtype">int</span> lev);
00018 <span class="keyword">static</span> <span class="keywordtype">void</span> klog_free_syslog (<a class="code" href="structklog__syslog__s.html">klog_syslog_t</a> *kls);
00019
00020 <span class="keyword">static</span> <span class="keywordtype">int</span> klog_syslog (<a class="code" href="structklog__s.html">klog_t</a> *kl, <span class="keywordtype">int</span> level, <span class="keyword">const</span> <span class="keywordtype">char</span> *fmt, va_list ap);
00021 <span class="keyword">static</span> <span class="keywordtype">void</span> klog_close_syslog (<a class="code" href="structklog__s.html">klog_t</a> *kl);
00022
00023 <span class="comment">/* must be ordered */</span>
00024 <span class="keyword">static</span> <span class="keywordtype">int</span> sysloglev[] =
00025 {
00026 LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING,
00027 LOG_ERR, LOG_CRIT, LOG_ALERT, LOG_EMERG
00028 };
00029
00030 <span class="comment">/* we've a problem here because syslog(3) mantains a global context, and</span>
00031 <span class="comment"> * we can't bind some syslog state specific to the supplied klog_t object.</span>
00032 <span class="comment"> * instead we are forced to do openlog->vsyslog->closelog for each logged</span>
00033 <span class="comment"> * message which is rather silly ... anyway this should be not so heavy as</span>
00034 <span class="comment"> * it seems: in fact (a) the underlying transport is connectionless (UDP) </span>
00035 <span class="comment"> * and (b) most of the time is spent inside the vsyslog call. */</span>
<a name="l00036"></a><a class="code" href="klogprv_8h.html#a3">00036</a> <span class="keywordtype">int</span> <a class="code" href="klogprv_8h.html#a3">klog_open_syslog</a> (<a class="code" href="structklog__s.html">klog_t</a> *kl, <span class="keywordtype">int</span> fac, <span class="keywordtype">int</span> logopt)
00037 {
00038 <a class="code" href="structklog__syslog__s.html">klog_syslog_t</a> *kls = NULL;
00039
00040 dbg_return_if (kl == NULL, ~0);
00041
00042 kls = u_zalloc(<span class="keyword">sizeof</span>(<a class="code" href="structklog__syslog__s.html">klog_syslog_t</a>));
00043 dbg_err_if (kls == NULL);
00044
00045 kls-><a class="code" href="structklog__syslog__s.html#o0">facility</a> = fac;
00046 kls-><a class="code" href="structklog__syslog__s.html#o1">logopt</a> = logopt;
00047
00048 <span class="comment">/* set private methods */</span>
00049 kl-><a class="code" href="structklog__s.html#o7">cb_log</a> = klog_syslog;
00050 kl-><a class="code" href="structklog__s.html#o8">cb_close</a> = klog_close_syslog;
00051 kl-><a class="code" href="structklog__s.html#o9">cb_getln</a> = NULL;
00052 kl-><a class="code" href="structklog__s.html#o10">cb_countln</a> = NULL;
00053 kl-><a class="code" href="structklog__s.html#o11">cb_clear</a> = NULL;
00054 kl-><a class="code" href="structklog__s.html#o12">cb_flush</a> = NULL;
00055
00056 <span class="comment">/* stick child to its parent */</span>
00057 kl-><a class="code" href="structklog__s.html#o6">u</a>.<a class="code" href="structklog__s.html#o4">s</a> = kls, kls = NULL;
00058
00059 <span class="keywordflow">return</span> 0;
00060 err:
00061 <span class="keywordflow">if</span> (kls)
00062 klog_free_syslog(kls);
00063 <span class="keywordflow">return</span> ~0;
00064 }
00065
00066 <span class="keyword">static</span> <span class="keywordtype">void</span> klog_close_syslog (<a class="code" href="structklog__s.html">klog_t</a> *kl)
00067 {
00068 <span class="keywordflow">if</span> (kl == NULL || kl-><a class="code" href="structklog__s.html#o0">type</a> != KLOG_TYPE_SYSLOG || kl-><a class="code" href="structklog__s.html#o6">u</a>.<a class="code" href="structklog__s.html#o4">s</a> == NULL)
00069 <span class="keywordflow">return</span>;
00070
00071 klog_free_syslog(kl-><a class="code" href="structklog__s.html#o6">u</a>.<a class="code" href="structklog__s.html#o4">s</a>), kl-><a class="code" href="structklog__s.html#o6">u</a>.<a class="code" href="structklog__s.html#o4">s</a> = NULL;
00072
00073 <span class="keywordflow">return</span>;
00074 }
00075
00076 <span class="keyword">static</span> <span class="keywordtype">void</span> klog_free_syslog (<a class="code" href="structklog__syslog__s.html">klog_syslog_t</a> *kls)
00077 {
00078 <span class="keywordflow">if</span> (kls == NULL)
00079 <span class="keywordflow">return</span>;
00080 U_FREE(kls);
00081 <span class="keywordflow">return</span>;
00082 }
00083
00084 <span class="keyword">static</span> <span class="keywordtype">int</span> klog_syslog (<a class="code" href="structklog__s.html">klog_t</a> *kl, <span class="keywordtype">int</span> level, <span class="keyword">const</span> <span class="keywordtype">char</span> *fmt, va_list ap)
00085 {
00086 dbg_return_if (kl == NULL, ~0);
00087 dbg_return_if (kl-><a class="code" href="structklog__s.html#o0">type</a> != KLOG_TYPE_SYSLOG, ~0);
00088 dbg_return_if (kl-><a class="code" href="structklog__s.html#o6">u</a>.<a class="code" href="structklog__s.html#o4">s</a> == NULL, ~0);
00089 dbg_return_if (fmt == NULL, ~0);
00090
00091 <span class="preprocessor">#ifdef HAVE_SYSLOG</span>
00092 <span class="preprocessor"></span> openlog(kl-><a class="code" href="structklog__s.html#o2">ident</a>, kl-><a class="code" href="structklog__s.html#o6">u</a>.<a class="code" href="structklog__s.html#o4">s</a>-><a class="code" href="structklog__syslog__s.html#o1">logopt</a>, kl-><a class="code" href="structklog__s.html#o6">u</a>.<a class="code" href="structklog__s.html#o4">s</a>-><a class="code" href="structklog__syslog__s.html#o0">facility</a>);
00093 vsyslog(klog_to_syslog(level), fmt, ap);
00094 closelog();
00095 <span class="preprocessor">#else</span>
00096 <span class="preprocessor"></span> vsyslog(kl-><a class="code" href="structklog__s.html#o6">u</a>.<a class="code" href="structklog__s.html#o4">s</a>-><a class="code" href="structklog__syslog__s.html#o0">facility</a> | klog_to_syslog(level), fmt, ap);
00097 <span class="preprocessor">#endif </span><span class="comment">/* HAVE_SYSLOG */</span>
00098
00099 <span class="keywordflow">return</span> 0;
00100 }
00101
00102 <span class="keyword">static</span> <span class="keywordtype">int</span> klog_to_syslog (<span class="keywordtype">int</span> lev)
00103 {
00104 <span class="keywordflow">return</span> (lev < KLOG_DEBUG || lev > KLOG_EMERG) ? -1 : sysloglev[lev];
00105 }
</pre></div><hr>
<div>
<div style="text-align:left">
<a href="http://www.koanlogic.com/kl/cont/gb/html/products.html">←Products</a>
</div>
<div style="text-align:center;">
© 2005-2006 - <a href="http://www.koanlogic.com">KoanLogic S.r.l.</a> - All rights reserved
</div>
</div>
</body>
</html>
|