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.
18 syslog_close(struct log_stream *ls)
24 /* convert severity level to syslog constants */
26 syslog_level(int level)
28 static const int levels[] = {
29 [L_DEBUG] = LOG_DEBUG,
31 [L_INFO_R] = LOG_INFO,
32 [L_WARN] = LOG_WARNING,
33 [L_WARN_R] = LOG_WARNING,
35 [L_ERROR_R] = LOG_ERR,
38 return ((level < (int)ARRAY_SIZE(levels)) ? levels[level] : LOG_NOTICE);
41 /* simple syslog write handler */
43 syslog_handler(struct log_stream *ls, const char *m, uns flags)
49 // FIXME: Logging of PID
50 prio = syslog_level(LS_GET_LEVEL(flags)) | (ls->idata);
52 syslog(prio | (ls->idata), "%s: %s", ls->name, m);
54 syslog(prio | (ls->idata), "%s", m);
58 /* assign log to a syslog facility */
59 /* initialize with no formatting (syslog adds these inforamtion) */
60 /* name is optional prefix (NULL for none) */
62 log_new_syslog(int facility, const char *name)
64 struct log_stream *ls = log_new_stream();
66 ls->name = xstrdup(name);
68 ls->msgfmt = LSFMT_NONE;
69 ls->handler = syslog_handler;
70 ls->close = syslog_close;