]> mj.ucw.cz Git - libucw.git/commitdiff
Logging: Cleanup of log-syslog.c.
authorMartin Mares <mj@ucw.cz>
Fri, 13 Feb 2009 17:26:53 +0000 (18:26 +0100)
committerMartin Mares <mj@ucw.cz>
Fri, 13 Feb 2009 17:26:53 +0000 (18:26 +0100)
  o  Use a static array instead of switch() to translate log levels.
  o  Coding style and namespace cleanups.

ucw/log-syslog.c
ucw/log.h

index e4ec85ed558cc14168b25f384b9849a87eade967..712a7d8433c05373f3b77cac9bb47982f5999ba3 100644 (file)
 
 #include <syslog.h>
 
-/* destructor for syslog logs */
-static void ls_syslog_close(struct log_stream *ls)
+/* Destructor */
+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)
+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, uns flags)
+static int
+syslog_handler(struct log_stream *ls, const char *m, uns flags)
 {
   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(flags)) | (ls->idata);
   if (ls->name)
     syslog(prio | (ls->idata), "%s: %s", ls->name, m);
   else
@@ -56,13 +58,15 @@ static int ls_syslog_handler(struct log_stream *ls, const char *m, uns flags)
 /* 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=log_new_stream();
-  if (name) ls->name = xstrdup(name);
+  struct log_stream *ls = log_new_stream();
+  if (name)
+    ls->name = xstrdup(name);
   ls->idata = facility;
   ls->msgfmt = LSFMT_NONE;
-  ls->handler = ls_syslog_handler;
-  ls->close = ls_syslog_close;
+  ls->handler = syslog_handler;
+  ls->close = syslog_close;
   return ls;
 }
index d77632b23e989a095a3e78ab14fc40f11650f37c..93d422b7e6a5be4a09707bd883758cc585f67a09 100644 (file)
--- a/ucw/log.h
+++ b/ucw/log.h
@@ -188,6 +188,6 @@ struct log_stream *ls_file_new(const char *path);
 /* 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);
 
 #endif