From 276a99f9fe7c2819ea61accfcb35088707882b58 Mon Sep 17 00:00:00 2001 From: Felix Janda Date: Mon, 19 Aug 2013 22:11:22 +0200 Subject: In logger and syslogd remove duplicated definitions of facilities and priorities In syslogd.c get the definitions from . 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. --- toys/pending/logger.c | 38 ++++------------------------ toys/pending/syslogd.c | 67 ++++++++++---------------------------------------- 2 files changed, 18 insertions(+), 87 deletions(-) (limited to 'toys/pending') diff --git a/toys/pending/logger.c b/toys/pending/logger.c index 87e204d2..d94e6f88 100644 --- a/toys/pending/logger.c +++ b/toys/pending/logger.c @@ -8,51 +8,23 @@ USE_LOGGER(NEWTOY(logger, "st:p:", TOYFLAG_USR|TOYFLAG_BIN)) 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 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 @@ void logger_main(void) 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 --git a/toys/pending/syslogd.c b/toys/pending/syslogd.c index 8cabcc81..4cce1c5f 100644 --- a/toys/pending/syslogd.c +++ b/toys/pending/syslogd.c @@ -33,6 +33,7 @@ config SYSLOGD */ #define FOR_syslogd +#define SYSLOG_NAMES #include "toys.h" #include "toynet.h" @@ -56,60 +57,6 @@ GLOBALS( #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 @@ static int write_rotate( logfile_t *tf, int len) 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) { -- cgit v1.2.3