[Toybox] Put syslogd and logger into the same file?

Felix Janda felix.janda at posteo.de
Mon Aug 19 13:15:25 PDT 2013


Felix Janda wrote:
[...]
> So how about the following hack?
> 
> Keep them in separate files. Have one common config symbol, tear everything
> out of logger.c but logger_main, declare SYSLOG_NAMES in syslogd.c, put
> something like the current lookup() in logger.c to syslogd.c and make an
> extern declaration in logger.c. The new lookup() (better use a less generic
> name then) should accept an integer as its first argument instead of the
> table, say 0 stands for facilities and 1 for priorities.

To be more concrete, below is a corresponding patch.

Felix

# HG changeset patch
# User Felix Janda <felix.janda at posteo.de>
# Date 1376943082 -7200
# Node ID f9271a80fedc23b91559f0527da8e9c22d152b5d
# Parent  133ef0885cceaa53f1c75cf834e838896a3c3081
In logger and syslogd remove duplicated definitions of facilities and priorities

In syslogd.c get the definitions from <syslog.h>. For logger.c we
can't do this as well since it causes multiply defined symbols.
Instead we define a non-static lookup function in syslog.c for
logger.

diff -r 133ef0885cce -r f9271a80fedc toys/pending/logger.c
--- a/toys/pending/logger.c	Mon Aug 19 04:23:16 2013 -0500
+++ b/toys/pending/logger.c	Mon Aug 19 22:11:22 2013 +0200
@@ -8,51 +8,23 @@
 
 config LOGGER
   bool "logger"
+  depends on SYSLOGD
   default n
   help
-    usage: hello [-s] [-t tag] [-p [facility.]priority] [message]
+    usage: logger [-s] [-t tag] [-p [facility.]priority] [message]
 
     Log message (or stdin) to syslog.
 */
 
 #define FOR_logger
 #include "toys.h"
-#include <syslog.h>
 
 GLOBALS(
   char *priority_arg;
   char *ident;
 )
 
-struct mapping {
-  char *key;
-  int value;
-};
-
-static struct mapping facilities[] = {
-  {"user", LOG_USER}, {"main", LOG_MAIL}, {"news", LOG_NEWS},
-  {"uucp", LOG_UUCP}, {"daemon", LOG_DAEMON}, {"auth", LOG_AUTH},
-  {"cron", LOG_CRON}, {"lpr", LOG_LPR}, {"local0", LOG_LOCAL0},
-  {"local1", LOG_LOCAL1}, {"local2", LOG_LOCAL2}, {"local3", LOG_LOCAL3},
-  {"local4", LOG_LOCAL4}, {"local5", LOG_LOCAL5}, {"local6", LOG_LOCAL6},
-  {"local7", LOG_LOCAL7},
-  {NULL, 0}
-};
-
-static struct mapping priorities[] = {
-  {"emerg", LOG_EMERG}, {"alert", LOG_ALERT}, {"crit", LOG_CRIT},
-  {"err", LOG_ERR}, {"warning", LOG_WARNING}, {"notice", LOG_NOTICE},
-  {"info", LOG_INFO}, {"debug", LOG_DEBUG},
-  {NULL, 0}
-};
-
-static int lookup(struct mapping *where, char *key)
-{
-  for (; where->key; where++)
-    if (!strcasecmp(key, where->key)) return where->value;
-
-  return -1;
-}
+extern int logger_lookup(int where, char *key);
 
 void logger_main(void)
 {
@@ -64,12 +36,12 @@
 
     if (sep) {
       *sep = '\0';
-      if ((facility = lookup(facilities, TT.priority_arg)) == -1)
+      if ((facility = logger_lookup(0, TT.priority_arg)) == -1)
         error_exit("bad facility: %s", TT.priority_arg);
       TT.priority_arg = sep+1;
     }
 
-    if ((priority = lookup(priorities, TT.priority_arg)) == -1)
+    if ((priority = logger_lookup(1, TT.priority_arg)) == -1)
       error_exit("bad priority: %s", TT.priority_arg);
   }
 
diff -r 133ef0885cce -r f9271a80fedc toys/pending/syslogd.c
--- a/toys/pending/syslogd.c	Mon Aug 19 04:23:16 2013 -0500
+++ b/toys/pending/syslogd.c	Mon Aug 19 22:11:22 2013 +0200
@@ -33,6 +33,7 @@
 */
 
 #define FOR_syslogd
+#define SYSLOG_NAMES
 #include "toys.h"
 #include "toynet.h"
 
@@ -56,60 +57,6 @@
 #define flag_get(f,v,d)  ((toys.optflags & f) ? v : d)
 #define flag_chk(f)    ((toys.optflags & f) ? 1 : 0)
 
-#ifndef SYSLOG_NAMES
-#define  INTERNAL_NOPRI  0x10
-#define  INTERNAL_MARK  LOG_MAKEPRI(LOG_NFACILITIES, 0)
-
-typedef struct _code {
-  char *c_name;
-  int c_val;
-} CODE;
-
-static CODE prioritynames[] =
-{
-  { "alert", LOG_ALERT },
-  { "crit", LOG_CRIT },
-  { "debug", LOG_DEBUG },
-  { "emerg", LOG_EMERG },
-  { "err", LOG_ERR },
-  { "error", LOG_ERR },    /* DEPRECATED */
-  { "info", LOG_INFO },
-  { "none", INTERNAL_NOPRI },    /* INTERNAL */
-  { "notice", LOG_NOTICE },
-  { "panic", LOG_EMERG },    /* DEPRECATED */
-  { "warn", LOG_WARNING },    /* DEPRECATED */
-  { "warning", LOG_WARNING },
-  { NULL, -1 }
-};
-
-static CODE facilitynames[] =
-{
-  { "auth", LOG_AUTH },
-  { "authpriv", LOG_AUTHPRIV },
-  { "cron", LOG_CRON },
-  { "daemon", LOG_DAEMON },
-  { "ftp", LOG_FTP },
-  { "kern", LOG_KERN },
-  { "lpr", LOG_LPR },
-  { "mail", LOG_MAIL },
-  { "mark", INTERNAL_MARK },    /* INTERNAL */
-  { "news", LOG_NEWS },
-  { "security", LOG_AUTH },    /* DEPRECATED */
-  { "syslog", LOG_SYSLOG },
-  { "user", LOG_USER },
-  { "uucp", LOG_UUCP },
-  { "local0", LOG_LOCAL0 },
-  { "local1", LOG_LOCAL1 },
-  { "local2", LOG_LOCAL2 },
-  { "local3", LOG_LOCAL3 },
-  { "local4", LOG_LOCAL4 },
-  { "local5", LOG_LOCAL5 },
-  { "local6", LOG_LOCAL6 },
-  { "local7", LOG_LOCAL7 },
-  { NULL, -1 }
-};
-#endif
-
 
 // Signal handling 
 struct fd_pair { int rd; int wr; };
@@ -504,6 +451,18 @@
   return write(tf->logfd, toybuf, len);
 }
 
+// Lookup numerical code from name
+// Only used in logger
+int logger_lookup(int where, char *key)
+{
+  CODE *w = ((CODE *[]){facilitynames, prioritynames})[where];
+
+  for (; w->c_name; w++)
+    if (!strcasecmp(key, w->c_name)) return w->c_val;
+
+  return -1;
+}
+
 //search the given name and return its value
 static char *dec(int val, CODE *clist)
 {

 1376943325.0


More information about the Toybox mailing list