chiark / gitweb /
log: more general error message formatting
authorRichard Kettlewell <rjk@greenend.org.uk>
Sun, 17 Nov 2013 10:47:42 +0000 (10:47 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sun, 17 Nov 2013 10:47:42 +0000 (10:47 +0000)
lib/addr.c
lib/log.c
lib/log.h
lib/queue.c

index f23da74b1646e07796de7beabe650a8937d09426..d1c9d011db7382befb4dbac82e32d4ba5adbabbb 100644 (file)
@@ -66,12 +66,14 @@ struct addrinfo *get_address(const struct stringlist *a,
   struct addrinfo *res;
   char *name;
   int rc;
+  char errbuf[1024];
 
   switch(a->n) {  
   case 1:
     byte_xasprintf(&name, "host * service %s", a->s[0]);
     if((rc = getaddrinfo(0, a->s[0], pref, &res))) {
-      disorder_error(0, "getaddrinfo %s: %s", a->s[0], gai_strerror(rc));
+      disorder_error(0, "getaddrinfo %s: %s", a->s[0],
+                     format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
       return 0;
     }
     break;
@@ -79,7 +81,8 @@ struct addrinfo *get_address(const struct stringlist *a,
     byte_xasprintf(&name, "host %s service %s", a->s[0], a->s[1]);
     if((rc = getaddrinfo(a->s[0], a->s[1], pref, &res))) {
       disorder_error(0, "getaddrinfo %s %s: %s",
-                    a->s[0], a->s[1], gai_strerror(rc));
+                     a->s[0], a->s[1],
+                     format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
       return 0;
     }
     break;
@@ -339,6 +342,7 @@ struct addrinfo *netaddress_resolve(const struct netaddress *na,
   struct addrinfo *res, hints[1];
   char service[64];
   int rc;
+  char errbuf[1024];
 
   memset(hints, 0, sizeof hints);
   hints->ai_family = na->af;
@@ -349,7 +353,8 @@ struct addrinfo *netaddress_resolve(const struct netaddress *na,
   if(rc) {
     disorder_error(0, "getaddrinfo %s %d: %s",
                   na->address ? na->address : "*",
-                  na->port, gai_strerror(rc));
+                   na->port,
+                   format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
     return NULL;
   }
   return res;
index 988a8452bcd9ecaa3bae3737feb6de93eed99d32..741059a8ff2b75bd19a8516c1c9eea20680065ed 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -205,12 +205,14 @@ static void logger(int pri, const char *fmt, ...) {
 
 /** @brief Format and log a message
  * @param pri Message priority (as per syslog)
+ * @param ec Error class
  * @param fmt Format string
  * @param errno_value Errno value to include as a string, or 0
  * @param ap Argument list
  */
-void elog(int pri, int errno_value, const char *fmt, va_list ap) {
+void elog(int pri, enum error_class ec, int errno_value, const char *fmt, va_list ap) {
   char buffer[1024];
+  char errbuf[1024];
 
   if(errno_value == 0)
     vlogger(pri, fmt, ap);
@@ -218,7 +220,8 @@ void elog(int pri, int errno_value, const char *fmt, va_list ap) {
     memset(buffer, 0, sizeof buffer);
     byte_vsnprintf(buffer, sizeof buffer, fmt, ap);
     buffer[sizeof buffer - 1] = 0;
-    logger(pri, "%s: %s", buffer, strerror(errno_value));
+    logger(pri, "%s: %s", buffer,
+           format_error(ec, errno_value, errbuf, sizeof errbuf));
   }
 }
 
@@ -231,7 +234,22 @@ void disorder_fatal(int errno_value, const char *msg, ...) {
   va_list ap;
 
   va_start(ap, msg);
-  elog(LOG_CRIT, errno_value, msg, ap);
+  elog(LOG_CRIT, ec_errno, errno_value, msg, ap);
+  va_end(ap);
+  if(getenv("DISORDER_FATAL_ABORT")) abort();
+  exitfn(EXIT_FAILURE);
+}
+
+/** @brief Log an error and quit
+ *
+ * If @c ${DISORDER_FATAL_ABORT} is defined (as anything) then the process
+ * is aborted, so you can get a backtrace.
+ */
+void disorder_fatal_ec(enum error_class ec, int errno_value, const char *msg, ...) {
+  va_list ap;
+
+  va_start(ap, msg);
+  elog(LOG_CRIT, ec, errno_value, msg, ap);
   va_end(ap);
   if(getenv("DISORDER_FATAL_ABORT")) abort();
   exitfn(EXIT_FAILURE);
@@ -242,7 +260,16 @@ void disorder_error(int errno_value, const char *msg, ...) {
   va_list ap;
 
   va_start(ap, msg);
-  elog(LOG_ERR, errno_value, msg, ap);
+  elog(LOG_ERR, ec_errno, errno_value, msg, ap);
+  va_end(ap);
+}
+
+/** @brief Log an error */
+void disorder_error_ec(enum error_class ec, int errno_value, const char *msg, ...) {
+  va_list ap;
+
+  va_start(ap, msg);
+  elog(LOG_ERR, ec, errno_value, msg, ap);
   va_end(ap);
 }
 
@@ -251,7 +278,7 @@ void disorder_info(const char *msg, ...) {
   va_list ap;
 
   va_start(ap, msg);
-  elog(LOG_INFO, 0, msg, ap);
+  elog(LOG_INFO, ec_none, 0, msg, ap);
   va_end(ap);
 }
 
@@ -272,6 +299,27 @@ void set_progname(char **argv) {
     progname = argv[0];
 }
 
+/** @brief Format an error string
+ * @param ec Error class
+ * @param err Error code (interpretation defined by @p ec)
+ * @param buffer Output buffer
+ * @param bufsize Size of output buffer
+ * @return Pointer to error string
+ *
+ * The return value may or may not be @p buffer.
+ */
+#pragma GCC diagnostic ignored "-Wunused-parameter"
+const char *format_error(enum error_class ec, int err, char buffer[], size_t bufsize) {
+  switch(ec) {
+  default:
+    return strerror(err);
+  case ec_getaddrinfo:
+    return gai_strerror(err);
+  case ec_none:
+    return "(none)";
+  }
+}
+
 /*
 Local Variables:
 c-basic-offset:2
index bacccb386292909daf92b6f73832f728f2f0ac84..31e6f5f0426716eedf31c49350d426954d219ca9 100644 (file)
--- a/lib/log.h
+++ b/lib/log.h
@@ -28,12 +28,31 @@ struct log_output;
 
 void set_progname(char **argv);
 
-void elog(int pri, int errno_value, const char *fmt, va_list ap);
+/** @brief Possible error number spaces */
+enum error_class {
+  /** @brief Invalid number space */
+  ec_none,
+
+  /** @brief @c errno number space */
+  ec_errno,
+
+  /** @brief getaddrinfo() return value */
+  ec_getaddrinfo,
+};
+
+# define ec_native ec_errno
+# define ec_socket ec_errno
+
+void elog(int pri, enum error_class, int errno_value, const char *fmt, va_list ap);
 
 void disorder_fatal(int errno_value, const char *msg, ...) attribute((noreturn))
   attribute((format (printf, 2, 3)));
+void disorder_fatal_ec(enum error_class ec, int errno_value, const char *msg, ...) attribute((noreturn))
+  attribute((format (printf, 3, 4)));
 void disorder_error(int errno_value, const char *msg, ...)
   attribute((format (printf, 2, 3)));
+void disorder_error_ec(enum error_class ec, int errno_value, const char *msg, ...)
+  attribute((format (printf, 3, 4)));
 void disorder_info(const char *msg, ...)
   attribute((format (printf, 1, 2)));
 void disorder_debug(const char *msg, ...)
@@ -41,6 +60,8 @@ void disorder_debug(const char *msg, ...)
 /* report a message of the given class.  @errno_value@ if present an
  * non-zero is included.  @fatal@ terminates the process. */
 
+const char *format_error(enum error_class ec, int err, char buffer[], size_t bufsize);
+
 extern int debugging;
 /* set when debugging enabled */
 
index e39ca5f3f92fb36c28429bdb0fb74335833d9036..789d2eaa7120581824ee16ef55ee3c0e6cc59f86 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004-2009 Richard Kettlewell
+ * Copyright (C) 2004-2009, 2011, 2013 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -77,8 +77,9 @@ static int unmarshall_long(char *data, struct queue_entry *q,
                           size_t offset,
                           void (*error_handler)(const char *, void *),
                           void *u) {
+  char errbuf[1024];
   if(xstrtol(&VALUE(q, offset, long), data, 0, 0)) {
-    error_handler(strerror(errno), u);
+    error_handler(format_error(ec_errno, errno, errbuf, sizeof errbuf), u);
     return -1;
   }
   return 0;
@@ -123,9 +124,10 @@ static int unmarshall_time_t(char *data, struct queue_entry *q,
                             void (*error_handler)(const char *, void *),
                             void *u) {
   long_long ul;
+  char errbuf[1024];
 
   if(xstrtoll(&ul, data, 0, 0)) {
-    error_handler(strerror(errno), u);
+    error_handler(format_error(ec_errno, errno, errbuf, sizeof errbuf), u);
     return -1;
   }
   VALUE(q, offset, time_t) = ul;