From 92db088e5b292c1180a090ed369b9851e933e610 Mon Sep 17 00:00:00 2001 Message-Id: <92db088e5b292c1180a090ed369b9851e933e610.1715363293.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sun, 17 Nov 2013 10:47:42 +0000 Subject: [PATCH] log: more general error message formatting Organization: Straylight/Edgeware From: Richard Kettlewell --- lib/addr.c | 11 +++++++--- lib/log.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++----- lib/log.h | 23 ++++++++++++++++++++- lib/queue.c | 8 +++++--- 4 files changed, 88 insertions(+), 12 deletions(-) diff --git a/lib/addr.c b/lib/addr.c index f23da74..d1c9d01 100644 --- a/lib/addr.c +++ b/lib/addr.c @@ -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; diff --git a/lib/log.c b/lib/log.c index 988a845..741059a 100644 --- 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 diff --git a/lib/log.h b/lib/log.h index bacccb3..31e6f5f 100644 --- 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 */ diff --git a/lib/queue.c b/lib/queue.c index e39ca5f..789d2ea 100644 --- a/lib/queue.c +++ b/lib/queue.c @@ -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; -- [mdw]