]> mj.ucw.cz Git - libucw.git/blob - ucw/log-syslog.c
Logging: Let log_close_all() ASSERT that all references are gone.
[libucw.git] / ucw / log-syslog.c
1 /*
2  *      UCW Library -- Logging to Syslog
3  *
4  *      (c) 2009 Martin Mares <mj@ucw.cz>
5  *      (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "ucw/lib.h"
12 #include "ucw/log.h"
13
14 #include <syslog.h>
15
16 struct syslog_stream {
17   struct log_stream ls;
18   int facility;
19 };
20
21 static void
22 syslog_close(struct log_stream *ls)
23 {
24   if (ls->name)
25     xfree(ls->name);
26 }
27
28 /* Convert severity level to syslog constants */
29 static int
30 syslog_level(int level)
31 {
32   static const int levels[] = {
33     [L_DEBUG] =         LOG_DEBUG,
34     [L_INFO] =          LOG_INFO,
35     [L_INFO_R] =        LOG_INFO,
36     [L_WARN] =          LOG_WARNING,
37     [L_WARN_R] =        LOG_WARNING,
38     [L_ERROR] =         LOG_ERR,
39     [L_ERROR_R] =       LOG_ERR,
40     [L_FATAL] =         LOG_CRIT,
41   };
42   return ((level < (int)ARRAY_SIZE(levels)) ? levels[level] : LOG_NOTICE);
43 }
44
45 /* simple syslog write handler */
46 static int
47 syslog_handler(struct log_stream *ls, struct log_msg *m)
48 {
49   struct syslog_stream *ss = (struct syslog_stream *) ls;
50   int prio;
51   ASSERT(ls);
52   ASSERT(m);
53
54   // FIXME: Logging of PID
55   prio = syslog_level(LS_GET_LEVEL(m->flags)) | ss->facility;
56   if (ls->name)
57     syslog(prio, "%s: %s", ls->name, m->m);
58   else
59     syslog(prio, "%s", m->m);
60   return 0;
61 }
62
63 struct log_stream *
64 log_new_syslog(int facility, const char *name)
65 {
66   struct log_stream *ls = log_new_stream(sizeof(struct syslog_stream));
67   struct syslog_stream *ss = (struct syslog_stream *) ls;
68   if (name)
69     ls->name = xstrdup(name);
70   ls->msgfmt = 0;
71   ls->handler = syslog_handler;
72   ls->close = syslog_close;
73   ss->facility = facility;
74   return ls;
75   // FIXME: L_SIGHANDLER?
76 }