chiark / gitweb /
client: Better logging infrastructure.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 20 Dec 2008 17:06:11 +0000 (17:06 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 20 Dec 2008 17:06:11 +0000 (17:06 +0000)
Logging is currently done in a rather ad-hoc fashion.  This wants
tidying up.

client/tripectl.c

index e2411afb304cf6b8776c2fbd0c96ef9f865dab20..4c8dde5a5a246f8375f54417981b4a066ecc9dfb 100644 (file)
@@ -120,6 +120,30 @@ static void checkbg(char **p)
     die(EXIT_FAILURE, "unexpected background tag `%s'", q);
 }
 
+static void dolog(int prio, const char *msg, ...)
+{
+  va_list ap;
+  dstr d = DSTR_INIT;
+  const char *cat;
+
+  va_start(ap, msg);
+  dstr_vputf(&d, msg, &ap);
+  va_end(ap);
+  if (f & f_syslog) syslog(prio, "%s", d.buf);
+  if (logfp) {
+    switch (prio) {
+      case LOG_WARN: cat = "warning"; break;
+      case LOG_DEBUG: cat = "debug"; break;
+      case LOG_ERR: cat = "error"; break;
+      default: cat = "message"; break;
+    }
+    writelog(cat, d.buf);
+  }
+  if (prio == LOG_WARN && (f & f_warn))
+    fprintf(stderr, "Warning: %s\n", d.buf);
+  dstr_destroy(&d);
+}
+
 static void checkfg(void)
   { if (bgtag) die(EXIT_FAILURE, "unexpected foreground response"); }
 
@@ -134,28 +158,13 @@ static void cline(char *p, size_t len, void *b)
   q = str_getword(&p);
   if (!q)
     return;
-  if (strcmp(q, "WARN") == 0) {
-    if (f & f_syslog)
-      syslog(LOG_WARNING, "%s", p);
-    if (logfp)
-      writelog("warning", p);
-    if (f & f_warn)
-      fprintf(stderr, "Warning: %s\n", p);
-  } else if (strcmp(q, "TRACE") == 0) {
-    if (f & f_syslog)
-      syslog(LOG_DEBUG, "%s", p);
-    if (logfp)
-      writelog("debug", p);
-  } else if (!(f & f_command)) {
-    if (f & f_syslog)
-      syslog(LOG_ERR, "unexpected output `%s %s'", q, p);
-    if (logfp) {
-      dstr d = DSTR_INIT;
-      dstr_putf(&d, "unexpected output `%s %s'", q, p);
-      writelog("error", d.buf);
-      dstr_destroy(&d);
-    }
-  } else if (strcmp(q, "FAIL") == 0) {
+  if (strcmp(q, "WARN") == 0)
+    dolog(LOG_WARNING, p);
+  else if (strcmp(q, "TRACE") == 0)
+    dolog(LOG_DEBUG, p);
+  else if (!(f & f_command))
+    dolog(LOG_ERR, "unexpected output `%s %s'", q, p);
+  else if (strcmp(q, "FAIL") == 0) {
     checkfg();
     die(EXIT_FAILURE, "%s", p);
   } else if (strcmp(q, "INFO") == 0) {