From 36c1e62388a69be22c6f0028f6e7820202cfdbb6 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Thu, 9 Nov 2017 04:54:58 +0100 Subject: [PATCH] Improve error handling --- src/configure.c | 29 ++++++++++++++++++++--------- src/configure.h | 4 ++-- src/makefile.c | 2 +- src/settings.c | 3 ++- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/configure.c b/src/configure.c index a83be29..62b357b 100644 --- a/src/configure.c +++ b/src/configure.c @@ -34,6 +34,7 @@ # include #endif #include +#include #include #include #include @@ -149,19 +150,29 @@ ObjectType _source_type(String const * source); /* functions */ /* configure_error */ -int configure_error(char const * message, int ret) +int configure_error(int ret, char const * format, ...) { + va_list ap; + fputs(PROGNAME ": ", stderr); - perror(message); + va_start(ap, format); + vfprintf(stderr, format, ap); + fputc('\n', stderr); + va_end(ap); return ret; } /* configure_warning */ -int configure_warning(char const * message, int ret) +int configure_warning(int ret, char const * format, ...) { + va_list ap; + fputs(PROGNAME ": warning: ", stderr); - perror(message); + va_start(ap, format); + vfprintf(stderr, format, ap); + fputc('\n', stderr); + va_end(ap); return ret; } @@ -273,7 +284,7 @@ static void _configure_detect(Configure * configure) if(uname(&un) < 0) { - configure_error("system detection failed", 0); + configure_error(0, "%s", "system detection failed"); configure->arch = HA_UNKNOWN; configure->os = HO_UNKNOWN; configure->kernel = HK_UNKNOWN; @@ -361,8 +372,8 @@ static int _configure_detect_programs(Configure * configure) programs[i].program) != 0) return -1; if(config_load(configure->programs, filename) != 0) - configure_warning(DATADIR "/" PACKAGE "/" PACKAGE ".conf: " - "Could not load program definitions", 0); + configure_warning(0, "%s: %s", filename, + "Could not load program definitions"); /* platform-specific */ switch(configure->os) { @@ -391,13 +402,13 @@ static int _configure_load(ConfigurePrefs * prefs, String const * directory, String const * subdirs = NULL; if((path = string_new(directory)) == NULL) - return configure_error(directory, 1); + return configure_error(1, "%s", error_get(NULL)); if(string_append(&path, "/") != 0 || string_append(&path, PROJECT_CONF) != 0 || (config = config_new()) == NULL) { string_delete(path); - return configure_error(directory, 1); + return configure_error(1, "%s", error_get(NULL)); } config_set(config, "", "directory", directory); if(prefs->flags & PREFS_v) diff --git a/src/configure.h b/src/configure.h index efbaf55..61f4e3b 100644 --- a/src/configure.h +++ b/src/configure.h @@ -162,8 +162,8 @@ String const * configure_get_soext(Configure * configure); unsigned int configure_is_flag_set(Configure * configure, unsigned int flags); /* useful */ -int configure_error(char const * message, int ret); -int configure_warning(char const * message, int ret); +int configure_error(int ret, char const * format, ...); +int configure_warning(int ret, char const * format, ...); /* generic */ unsigned int enum_map_find(unsigned int last, EnumMap const * map, diff --git a/src/makefile.c b/src/makefile.c index 3b7fd14..418f679 100644 --- a/src/makefile.c +++ b/src/makefile.c @@ -93,7 +93,7 @@ int makefile(Configure * configure, String const * directory, configArray * ca, return -1; if(!_makefile_is_flag_set(configure, PREFS_n) && (fp = fopen(makefile, "w")) == NULL) - ret = configure_error(makefile, 1); + ret = configure_error(1, "%s: %s", makefile, strerror(errno)); else { if(_makefile_is_flag_set(configure, PREFS_v)) diff --git a/src/settings.c b/src/settings.c index c80f7bc..5105d31 100644 --- a/src/settings.c +++ b/src/settings.c @@ -31,6 +31,7 @@ #include #include #include +#include #include "configure.h" #include "settings.h" @@ -119,7 +120,7 @@ static int _settings_do(Configure * configure, String const * directory, return 1; } if((fp = fopen(filename, "w")) == NULL) - configure_error(filename, 0); + configure_error(0, "%s: %s", filename, strerror(errno)); string_delete(filename); if(fp == NULL) return 1;