From c2ef73ab945d0c668e000aef58a1ad227d8910c1 Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Mon, 28 Oct 2013 00:13:14 -0400 Subject: [PATCH] Refactor log functions --- src/app-config.c | 2 +- src/log.c | 62 ++++++++++++++++++++++++++++-------------------- src/log.h | 16 ++++++------- src/memory.c | 5 ++-- src/options.c | 9 +++---- src/options.h | 1 + 6 files changed, 52 insertions(+), 43 deletions(-) diff --git a/src/app-config.c b/src/app-config.c index ba9a18a..1ac28af 100644 --- a/src/app-config.c +++ b/src/app-config.c @@ -98,7 +98,7 @@ app_config_load_from_key_file(GKeyFile *kf) /* View */ app_config_key_file_get_int(kf, "view", "layout", &cfg->layout); if ((cfg->layout != UI_LAYOUT1) && (cfg->layout != UI_LAYOUT2)) { - log_fail_no_exit("Invalid layout, defaulting to layout 1"); + log_err("Invalid layout, defaulting to layout 1"); cfg->layout = UI_LAYOUT1; } app_config_key_file_get_int(kf, "view", "int_memory", diff --git a/src/log.c b/src/log.c index 2810390..b378575 100644 --- a/src/log.c +++ b/src/log.c @@ -20,12 +20,26 @@ #include "common.h" #include "options.h" -static int log_level = LOG_LEVEL_ERR; /* LEVEL = ERROR */ +#define PREFIX_PACKAGE_NAME 1 +#define ADD_LINEFEED 1 -void -log_set_level(int new_log_level) +extern struct options_t options; + +static void +log_prefix_package_name(FILE *stream, const char *severity) { - log_level = new_log_level; +#if PREFIX_PACKAGE_NAME + /* Printing the name of the program first if desired. */ + fprintf(stream, "%s %s: ", PACKAGE_NAME, severity); +#endif /* PREFIX_PACKAGE_NAME */ +} + +static void +log_suffix_newline(FILE *stream) +{ +#if ADD_LINEFEED + fprintf(stream, "\n"); +#endif /* ADD_LINEFEED */ } void @@ -33,17 +47,16 @@ log_debug(const char *format, ...) { FILE *stream = stdout; - if (log_level >= LOG_LEVEL_DEBUG) { + if (options.log >= LOG_LEVEL_DEBUG) { va_list ap; - /* Printing the name of the program first. */ - fprintf(stream, "%s debug : ", PACKAGE_NAME); + log_prefix_package_name(stream, "debug"); va_start(ap, format); vfprintf(stream, format, ap); va_end(ap); - fprintf(stream, "\n"); + log_suffix_newline(stream); } } @@ -52,17 +65,16 @@ log_info(const char *format, ...) { FILE *stream = stdout; - if (log_level >= LOG_LEVEL_INFO) { + if (options.log >= LOG_LEVEL_INFO) { va_list ap; - /* Printing the name of the program first. */ - fprintf(stream, "%s info : ", PACKAGE_NAME); + log_prefix_package_name(stream, "info"); va_start(ap, format); vfprintf(stream, format, ap); va_end(ap); - fprintf(stream, "\n"); + log_suffix_newline(stream); } } @@ -71,50 +83,48 @@ log_warn(const char *format, ...) { FILE *stream = stderr; - if (log_level >= LOG_LEVEL_WARN) { + if (options.log >= LOG_LEVEL_WARN) { va_list ap; - /* Printing the name of the program first. */ - fprintf(stream, "%s warning: ", PACKAGE_NAME); + log_prefix_package_name(stream, "warn"); va_start(ap, format); vfprintf(stream, format, ap); va_end(ap); - fprintf(stream, "\n"); + log_suffix_newline(stream); } } void -log_fail(const char *format, ...) +log_err(const char *format, ...) { FILE *stream = stderr; va_list ap; - /* Printing the name of the program first. */ - fprintf(stream, "%s error : ", PACKAGE_NAME); + log_prefix_package_name(stream, "error"); va_start(ap, format); vfprintf(stream, format, ap); va_end(ap); - fprintf(stream, "\n"); - - exit(EXIT_FAILURE); + log_suffix_newline(stream); } +/* Log error message and exits with error code. */ void -log_fail_no_exit(const char *format, ...) +log_fail(const char *format, ...) { FILE *stream = stderr; va_list ap; - /* Printing the name of the program first. */ - fprintf(stream, "%s error: ", PACKAGE_NAME); + log_prefix_package_name(stream, "error"); va_start(ap, format); vfprintf(stream, format, ap); va_end(ap); - fprintf(stream, "\n"); + log_suffix_newline(stream); + + exit(EXIT_FAILURE); } diff --git a/src/log.h b/src/log.h index f305797..6b3e2e9 100644 --- a/src/log.h +++ b/src/log.h @@ -21,15 +21,12 @@ #include "common.h" enum LOG_LEVEL { - LOG_LEVEL_ERR, - LOG_LEVEL_WARN, - LOG_LEVEL_INFO, - LOG_LEVEL_DEBUG + LOG_LEVEL_ERR = 0, /* Display only errors */ + LOG_LEVEL_WARN, /* Display warnings */ + LOG_LEVEL_INFO, /* Display information messages */ + LOG_LEVEL_DEBUG, /* Display all messages */ }; -void -log_set_level(int new_log_level); - void log_debug(const char *format, ...); @@ -40,9 +37,10 @@ void log_warn(const char *format, ...); void -log_fail(const char *format, ...); +log_err(const char *format, ...); +/* Log error message and exits with error code. */ void -log_fail_no_exit(const char *format, ...); +log_fail(const char *format, ...); #endif /* LOG_H */ diff --git a/src/memory.c b/src/memory.c index fec85c1..f8753d3 100644 --- a/src/memory.c +++ b/src/memory.c @@ -60,14 +60,13 @@ memory_init(void) m = &mem_infos[k]; if (m->size > m->max_size) { - log_fail_no_exit("Memory size invalid (max = %d)", - m->max_size); + log_err("Memory size invalid (max = %d)", m->max_size); exit(1); } m->buf = malloc(m->size); if (m->buf == NULL) { - log_fail_no_exit("%s", strerror(errno)); + log_err("%s", strerror(errno)); exit(1); } } diff --git a/src/options.c b/src/options.c index 138507e..39efd57 100644 --- a/src/options.c +++ b/src/options.c @@ -68,16 +68,16 @@ decode_debug_option(char *arg, struct argp_state *state) log_level = strtol(arg, &endptr, 0); if (*endptr != '\0') { - log_fail_no_exit("Invalid log level"); + log_err("Invalid log level"); argp_usage(state); } if (log_level > LOG_LEVEL_DEBUG) { - log_fail_no_exit("Invalid log level"); + log_err("Invalid log level (0 to 3)"); argp_usage(state); } - log_set_level(log_level); + options.log = log_level; } static void @@ -102,7 +102,7 @@ decode_memory_size(char *arg, struct argp_state *state, int memid) *dest = strtol(arg, &endptr, 0); if (*endptr != '\0') { - log_fail_no_exit("Invalid memory size"); + log_err("Invalid memory size"); argp_usage(state); } } @@ -160,6 +160,7 @@ parse_command_line_options(int argc, char *argv[]) options.pram_size = PGM_MEM_DEFAULT_SIZE; options.iram_size = INT_MEM_MAX_SIZE; options.xram_size = EXT_MEM_DEFAULT_SIZE; + options.log = LOG_LEVEL_ERR; /* Parse our arguments. */ argp_parse(&argp, argc, argv, 0, 0, NULL); diff --git a/src/options.h b/src/options.h index b75a827..a03173e 100644 --- a/src/options.h +++ b/src/options.h @@ -32,6 +32,7 @@ struct options_t { int iram_size; /* Maximum internal ram size. */ int xram_size; /* Maximum external ram size. */ char *filename; + int log; } options_t; void -- 2.20.1