[Toybox] [CLEANUP] logger

Felix Janda felix.janda at posteo.de
Sat Jul 27 01:27:04 PDT 2013


Rob Landley wrote:
> On 07/24/2013 05:58:01 PM, Felix Janda wrote:
> > Various fixes to logger in below patch.
> > 
> > Mainly inline parse_priority(). Many other small changes e.g.  
> > s/const//,
> > move around some variables, ...
> > 
> > -Felix
> > 
> > # HG changeset patch
> > # User Felix Janda <felix.janda at posteo.de>
> > # Date 1374706109 -7200
> > # Node ID 8cc26924c2ef2e862c975a31167a685e18f555ec
> > # Parent  8ad85a95f7c3b75214fbd3a46fe834de45c39417
> > logger: Some cleanup
> > 
> > diff -r 8ad85a95f7c3 -r 8cc26924c2ef toys/pending/logger.c
> 
> Um, for future reference: could I get the more elaborate description in  
> the commit message, and the "some cleanup" in the bit before it? :)
> 
> Yes, I realize I've been setting a horrible example with "ifconfig  
> cleanup", but on the mailing list I've been writing outright _novels_  
> of description of each one of those (and indexing them from  
> cleanup.html), and those descriptions generally _are_ too long to go in  
> the commit message.

Ah, I hadn't realized that this is the reason for the short commmit
messages for the cleanups.

Since the patch isn't (or doesn't seem to be) applied yet. Below there is
an edited patch with some more useful commit message.

Felix


# HG changeset patch
# User Felix Janda <felix.janda at posteo.de>
# Date 1374706109 -7200
# Node ID 8cc26924c2ef2e862c975a31167a685e18f555ec
# Parent  8ad85a95f7c3b75214fbd3a46fe834de45c39417
logger: Some cleanup

- Inline parse_priority()
- Simplify loop in lookup()
- Ensure that priority and facility are initialized in all cases
- Remove const keyword
- Move around some variables
- Remove unnecessary headers

diff -r 8ad85a95f7c3 -r 8cc26924c2ef toys/pending/logger.c
--- a/toys/pending/logger.c	Tue Jul 23 20:19:31 2013 -0500
+++ b/toys/pending/logger.c	Thu Jul 25 00:48:29 2013 +0200
@@ -18,23 +18,18 @@
 #define FOR_logger
 #include "toys.h"
 #include <syslog.h>
-#include <strings.h>
-#include <string.h>
 
 GLOBALS(
   char *priority_arg;
   char *ident;
-
-  int facility;
-  int priority;
 )
 
 struct mapping {
-  const char *key;
+  char *key;
   int value;
 };
 
-static const struct mapping facilities[] = {
+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},
@@ -44,64 +39,51 @@
   {NULL, 0}
 };
 
-static const struct mapping priorities[] = {
+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(const struct mapping *where, const char *key)
+static int lookup(struct mapping *where, char *key)
 {
-  int i;
-  for (i = 0; where[i].key; i++)
-    if (!strcasecmp(key, where[i].key))
-      return where[i].value;
+  for (; where->key; where++)
+    if (!strcasecmp(key, where->key)) return where->value;
 
   return -1;
 }
 
-static void parse_priority()
+void logger_main(void)
 {
-  char *sep = strchr(TT.priority_arg, '.');
+  int facility = LOG_USER, priority = LOG_NOTICE;
+  char *message = NULL;
 
-  if (sep)
-  {
-    *sep = '\0';
-    if ((TT.facility = lookup(facilities, TT.priority_arg)) == -1)
-      error_exit("bad facility: %s", TT.priority_arg);
-    TT.priority_arg = sep+1;
+  if (toys.optflags & FLAG_p) {
+    char *sep = strchr(TT.priority_arg, '.');
+
+    if (sep) {
+      *sep = '\0';
+      if ((facility = lookup(facilities, 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)
+      error_exit("bad priority: %s", TT.priority_arg);
   }
 
-  if ((TT.priority = lookup(priorities, TT.priority_arg)) == -1)
-    error_exit("bad priority: %s", TT.priority_arg);
-}
+  if (!(toys.optflags & FLAG_t)) {
+    struct passwd *pw = getpwuid(geteuid());
 
-void logger_main(void)
-{
-  if (toys.optflags & FLAG_p)
-    parse_priority();
-  else
-  {
-    TT.facility = LOG_USER;
-    TT.priority = LOG_NOTICE;
-  }
-
-  if (!(toys.optflags & FLAG_t))
-  {
-    struct passwd *pw = getpwuid(geteuid());
-    if (!pw)
-      perror_exit("getpwuid");
+    if (!pw) perror_exit("getpwuid");
     TT.ident = xstrdup(pw->pw_name);
   }
 
-  char *message = NULL;
   if (toys.optc) {
-    int length = 0;
-    int pos = 0;
+    int length = 0, pos = 0;
 
-    for (;*toys.optargs; (void) *(toys.optargs)++) // shut up gcc
-    {
+    for (;*toys.optargs; toys.optargs++) {
       length += strlen(*(toys.optargs)) + 1; // plus one for the args spacing
       message = xrealloc(message, length + 1); // another one for the null byte
 
@@ -113,7 +95,7 @@
     message = toybuf;
   }
 
-  openlog(TT.ident, (toys.optflags & FLAG_s ? LOG_PERROR : 0) , TT.facility);
-  syslog(TT.priority, "%s", message);
+  openlog(TT.ident, (toys.optflags & FLAG_s ? LOG_PERROR : 0) , facility);
+  syslog(priority, "%s", message);
   closelog();
 }
_______________________________________________
Toybox mailing list
Toybox at lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

 1374913624.0


More information about the Toybox mailing list