From a32f4adec40e5759933a46b36fa722182a8e2e61 Mon Sep 17 00:00:00 2001 From: David Eklov Date: Mon, 27 Jun 2016 02:21:28 -0500 Subject: [PATCH 1/2] Implement sway_log_errno using _sway_log to reduce code duplication --- common/log.c | 28 ---------------------------- include/log.h | 6 +++++- 2 files changed, 5 insertions(+), 29 deletions(-) diff --git a/common/log.c b/common/log.c index ef791becf..018ba7acb 100644 --- a/common/log.c +++ b/common/log.c @@ -8,8 +8,6 @@ #include #include #include -#include -#include #include int colored = 1; @@ -90,32 +88,6 @@ void _sway_log(const char *filename, int line, log_importance_t verbosity, const } } -void sway_log_errno(log_importance_t verbosity, char* format, ...) { - if (verbosity <= v) { - unsigned int c = verbosity; - if (c > sizeof(verbosity_colors) / sizeof(char *) - 1) { - c = sizeof(verbosity_colors) / sizeof(char *) - 1; - } - - if (colored && isatty(STDERR_FILENO)) { - fprintf(stderr, "%s", verbosity_colors[c]); - } - - va_list args; - va_start(args, format); - vfprintf(stderr, format, args); - va_end(args); - - fprintf(stderr, ": "); - fprintf(stderr, "%s", strerror(errno)); - - if (colored && isatty(STDERR_FILENO)) { - fprintf(stderr, "\x1B[0m"); - } - fprintf(stderr, "\n"); - } -} - bool _sway_assert(bool condition, const char* format, ...) { if (condition) { return true; diff --git a/include/log.h b/include/log.h index efacf90f9..266920f52 100644 --- a/include/log.h +++ b/include/log.h @@ -1,6 +1,8 @@ #ifndef _SWAY_LOG_H #define _SWAY_LOG_H #include +#include +#include typedef enum { L_SILENT = 0, @@ -15,7 +17,6 @@ void reset_log_level(void); // returns whether debug logging is on after switching. bool toggle_debug_logging(void); void sway_log_colors(int mode); -void sway_log_errno(log_importance_t verbosity, char* format, ...) __attribute__((format(printf,2,3))); void sway_abort(const char* format, ...) __attribute__((format(printf,1,2))); bool _sway_assert(bool condition, const char* format, ...) __attribute__((format(printf,2,3))); @@ -32,6 +33,9 @@ void _sway_log(const char *filename, int line, log_importance_t verbosity, const _sway_log(NULL, 0, VERBOSITY, FMT, ##__VA_ARGS__) #endif +#define sway_log_errno(VERBOSITY, FMT, ...) \ + _sway_log(NULL, 0, VERBOSITY, FMT ": %s", ##__VA_ARGS__, strerror(errno)) + void error_handler(int sig); #endif From 40d67b13d9be10bf9adb3db50eec51ecbe56c022 Mon Sep 17 00:00:00 2001 From: David Eklov Date: Mon, 27 Jun 2016 02:29:37 -0500 Subject: [PATCH 2/2] Add get_log_level() to encapsulate v (current log level) This patch also makes all global variable in log.c static. --- common/log.c | 10 +++++++--- include/log.h | 1 + sway/debug_log.c | 4 +--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/common/log.c b/common/log.c index 018ba7acb..492ec33d8 100644 --- a/common/log.c +++ b/common/log.c @@ -10,9 +10,9 @@ #include #include -int colored = 1; -log_importance_t loglevel_default = L_ERROR; -log_importance_t v = L_SILENT; +static int colored = 1; +static log_importance_t loglevel_default = L_ERROR; +static log_importance_t v = L_SILENT; static const char *verbosity_colors[] = { [L_SILENT] = "", @@ -36,6 +36,10 @@ void set_log_level(log_importance_t verbosity) { v = verbosity; } +log_importance_t get_log_level(void) { + return v; +} + void reset_log_level(void) { v = loglevel_default; } diff --git a/include/log.h b/include/log.h index 266920f52..798cf1df9 100644 --- a/include/log.h +++ b/include/log.h @@ -13,6 +13,7 @@ typedef enum { void init_log(log_importance_t verbosity); void set_log_level(log_importance_t verbosity); +log_importance_t get_log_level(void); void reset_log_level(void); // returns whether debug logging is on after switching. bool toggle_debug_logging(void); diff --git a/sway/debug_log.c b/sway/debug_log.c index f804a5415..7c9884647 100644 --- a/sway/debug_log.c +++ b/sway/debug_log.c @@ -12,8 +12,6 @@ #include #include "workspace.h" -extern log_importance_t v; - /* XXX:DEBUG:XXX */ static void container_log(const swayc_t *c, int depth) { fprintf(stderr, "focus:%c", @@ -49,7 +47,7 @@ static void container_log(const swayc_t *c, int depth) { fprintf(stderr, "name:%.16s\n", c->name); } void layout_log(const swayc_t *c, int depth) { - if (L_DEBUG > v) return; + if (L_DEBUG > get_log_level()) return; int i, d; int e = c->children ? c->children->length : 0; container_log(c, depth);