From ed470d1a59f9b0e0fc4b065664e63aa77461a5a3 Mon Sep 17 00:00:00 2001 From: taiyu Date: Thu, 10 Sep 2015 12:51:00 -0700 Subject: [PATCH] changed logging --- include/log.h | 9 ++++++--- sway/log.c | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/include/log.h b/include/log.h index f8deaf268..de0cb8400 100644 --- a/include/log.h +++ b/include/log.h @@ -12,13 +12,16 @@ typedef enum { void init_log(log_importance_t verbosity); void sway_log_colors(int mode); -void sway_log(log_importance_t verbosity, const char* format, ...) __attribute__((format(printf,2,3))); +void sway_log_func(log_importance_t verbosity, const char *func, const char* format, ...) __attribute__((format(printf,3,4))); 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_func(bool condition, const char *func, const char* format, ...) __attribute__((format(printf,3,4))); + +#define sway_log(V, FMT, ...) \ + sway_log_func(V, __PRETTY_FUNCTION__, FMT, ##__VA_ARGS__) -bool _sway_assert(bool condition, const char* format, ...) __attribute__((format(printf,2,3))); #define sway_assert(COND, FMT, ...) \ - _sway_assert(COND, "%s:" FMT, __PRETTY_FUNCTION__, ##__VA_ARGS__) + sway_assert_func(COND, __PRETTY_FUNCTION__, FMT, ##__VA_ARGS__) void error_handler(int sig); diff --git a/sway/log.c b/sway/log.c index cf5c2092f..231863a3a 100644 --- a/sway/log.c +++ b/sway/log.c @@ -20,6 +20,14 @@ static const char *verbosity_colors[] = { "\x1B[1;30m", // L_DEBUG }; +static const char *log_str[] = { + [L_SILENT] = "Msg", + [L_DEBUG] = "Debug", + [L_INFO] = "Info", + [L_ERROR] = "Error", +}; + + void init_log(log_importance_t verbosity) { v = verbosity; signal(SIGSEGV, error_handler); @@ -40,7 +48,7 @@ void sway_abort(const char *format, ...) { sway_terminate(); } -void sway_log(log_importance_t verbosity, const char* format, ...) { +void sway_log_func(log_importance_t verbosity, const char *func, const char* format, ...) { if (verbosity <= v) { unsigned int c = verbosity; if (c > sizeof(verbosity_colors) / sizeof(char *)) { @@ -48,7 +56,8 @@ void sway_log(log_importance_t verbosity, const char* format, ...) { } if (colored && isatty(STDERR_FILENO)) { - fprintf(stderr, "%s", verbosity_colors[c]); + fprintf(stderr, "%s%-5s| %-32s | ", verbosity_colors[c], + log_str[verbosity], func); } va_list args; @@ -91,14 +100,14 @@ void sway_log_errno(log_importance_t verbosity, char* format, ...) { } } -bool _sway_assert(bool condition, const char* format, ...) { +bool sway_assert_func(bool condition, const char *func, const char *format, ...) { if (condition) { return true; } va_list args; va_start(args, format); - sway_log(L_ERROR, format, args); + sway_log_func(L_ERROR, func, format, args); va_end(args); #ifndef NDEBUG