#include <syslog.h>
-/* destructor for syslog logs */
-static void ls_syslog_close(struct log_stream *ls)
+struct syslog_stream {
+ struct log_stream ls;
+ int facility;
+};
+
+static void
+syslog_close(struct log_stream *ls)
{
- ASSERT(ls);
- if(ls->name)
+ if (ls->name)
xfree(ls->name);
}
-/* convert severity level to syslog constants */
-static int ls_syslog_convert_level(int level)
+/* Convert severity level to syslog constants */
+static int
+syslog_level(int level)
{
- switch(level)
- {
- case L_DEBUG: return LOG_DEBUG;
- case L_INFO: return LOG_INFO;
- case L_INFO_R: return LOG_INFO;
- case L_WARN: return LOG_WARNING;
- case L_WARN_R: return LOG_WARNING;
- case L_ERROR: return LOG_ERR;
- case L_ERROR_R: return LOG_ERR;
- case L_FATAL: return LOG_CRIT;
- default: return LOG_NOTICE;
- }
+ static const int levels[] = {
+ [L_DEBUG] = LOG_DEBUG,
+ [L_INFO] = LOG_INFO,
+ [L_INFO_R] = LOG_INFO,
+ [L_WARN] = LOG_WARNING,
+ [L_WARN_R] = LOG_WARNING,
+ [L_ERROR] = LOG_ERR,
+ [L_ERROR_R] = LOG_ERR,
+ [L_FATAL] = LOG_CRIT,
+ };
+ return ((level < (int)ARRAY_SIZE(levels)) ? levels[level] : LOG_NOTICE);
}
/* simple syslog write handler */
-static int ls_syslog_handler(struct log_stream *ls, const char *m, u32 flags)
+static int
+syslog_handler(struct log_stream *ls, struct log_msg *m)
{
+ struct syslog_stream *ss = (struct syslog_stream *) ls;
int prio;
ASSERT(ls);
ASSERT(m);
- prio = ls_syslog_convert_level(LS_GET_LEVEL(flags)) | (ls->idata);
+ // FIXME: Logging of PID
+ prio = syslog_level(LS_GET_LEVEL(m->flags)) | ss->facility;
if (ls->name)
- syslog(prio | (ls->idata), "%s: %s", ls->name, m);
+ syslog(prio, "%s: %s", ls->name, m->m);
else
- syslog(prio | (ls->idata), "%s", m);
+ syslog(prio, "%s", m->m);
return 0;
}
-/* assign log to a syslog facility */
-/* initialize with no formatting (syslog adds these inforamtion) */
-/* name is optional prefix (NULL for none) */
-struct log_stream *ls_syslog_new(int facility, const char *name)
+struct log_stream *
+log_new_syslog(int facility, const char *name)
{
- struct log_stream *ls=ls_new();
- if (name) ls->name = xstrdup(name);
- ls->idata = facility;
- ls->msgfmt = LSFMT_NONE;
- ls->handler = ls_syslog_handler;
- ls->close = ls_syslog_close;
+ struct log_stream *ls = log_new_stream(sizeof(struct syslog_stream));
+ struct syslog_stream *ss = (struct syslog_stream *) ls;
+ if (name)
+ ls->name = xstrdup(name);
+ ls->msgfmt = 0;
+ ls->handler = syslog_handler;
+ ls->close = syslog_close;
+ ss->facility = facility;
return ls;
+ // FIXME: L_SIGHANDLER?
}