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.
17 struct syslog_stream {
22 static int syslog_open_count;
25 syslog_close(struct log_stream *ls UNUSED)
27 if (!--syslog_open_count)
31 /* Convert syslog facility to its identifier. */
33 syslog_facility(const char *name)
35 // Unfortunately, there is no standard way how to get at the list of facility names
41 { "authpriv", LOG_AUTHPRIV },
43 { "daemon", LOG_DAEMON },
49 { "syslog", LOG_SYSLOG },
52 { "local0", LOG_LOCAL0 },
53 { "local1", LOG_LOCAL1 },
54 { "local2", LOG_LOCAL2 },
55 { "local3", LOG_LOCAL3 },
56 { "local4", LOG_LOCAL4 },
57 { "local5", LOG_LOCAL5 },
58 { "local6", LOG_LOCAL6 },
59 { "local7", LOG_LOCAL7 },
62 for (uns i=0; i < ARRAY_SIZE(facilities); i++)
63 if (!strcmp(facilities[i].name, name))
64 return facilities[i].id;
68 /* Convert severity level to syslog constants */
70 syslog_level(int level)
72 static const int levels[] = {
73 [L_DEBUG] = LOG_DEBUG,
75 [L_INFO_R] = LOG_INFO,
76 [L_WARN] = LOG_WARNING,
77 [L_WARN_R] = LOG_WARNING,
79 [L_ERROR_R] = LOG_ERR,
82 return ((level < (int)ARRAY_SIZE(levels)) ? levels[level] : LOG_NOTICE);
85 /* simple syslog write handler */
87 syslog_handler(struct log_stream *ls, struct log_msg *m)
89 struct syslog_stream *ss = (struct syslog_stream *) ls;
94 prio = syslog_level(LS_GET_LEVEL(m->flags)) | ss->facility;
95 syslog(prio, "%s", m->m);
100 log_new_syslog(const char *facility, int options)
102 int fac = syslog_facility(facility);
104 die("No such syslog facility: %s", facility);
106 struct log_stream *ls = log_new_stream(sizeof(struct syslog_stream));
107 struct syslog_stream *ss = (struct syslog_stream *) ls;
110 ls->handler = syslog_handler;
111 ls->close = syslog_close;
114 if (!syslog_open_count++)
115 openlog(log_title, options, LOG_INFO);
120 log_syslog_facility_exists(const char *facility)
122 return (syslog_facility(facility) >= 0);