2 * UCW Library -- Logging to Syslog
4 * (c) 2009 Martin Mares <mj@ucw.cz>
5 * (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
16 struct syslog_stream {
23 syslog_close(struct log_stream *ls)
29 /* convert severity level to syslog constants */
31 syslog_level(int level)
33 static const int levels[] = {
34 [L_DEBUG] = LOG_DEBUG,
36 [L_INFO_R] = LOG_INFO,
37 [L_WARN] = LOG_WARNING,
38 [L_WARN_R] = LOG_WARNING,
40 [L_ERROR_R] = LOG_ERR,
43 return ((level < (int)ARRAY_SIZE(levels)) ? levels[level] : LOG_NOTICE);
46 /* simple syslog write handler */
48 syslog_handler(struct log_stream *ls, struct log_msg *m)
50 struct syslog_stream *ss = (struct syslog_stream *) ls;
55 // FIXME: Logging of PID
56 prio = syslog_level(LS_GET_LEVEL(m->flags)) | ss->facility;
58 syslog(prio, "%s: %s", ls->name, m->m);
60 syslog(prio, "%s", m->m);
64 /* assign log to a syslog facility */
65 /* initialize with no formatting (syslog adds these inforamtion) */
66 /* name is optional prefix (NULL for none) */
68 log_new_syslog(int facility, const char *name)
70 struct log_stream *ls = log_new_stream(sizeof(struct syslog_stream));
71 struct syslog_stream *ss = (struct syslog_stream *) ls;
73 ls->name = xstrdup(name);
74 ls->msgfmt = LSFMT_NONE;
75 ls->handler = syslog_handler;
76 ls->close = syslog_close;
77 ss->facility = facility;