From d729032ba2e8da8b4efa4b3112231662c6912f98 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 11 Nov 2015 08:32:32 -0500 Subject: Add file and line number to log in Debug build --- include/log.h | 11 ++++++++++- sway/log.c | 13 ++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/log.h b/include/log.h index 4b769c93..eada025e 100644 --- a/include/log.h +++ b/include/log.h @@ -16,7 +16,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(log_importance_t verbosity, const char* format, ...) __attribute__((format(printf,2,3))); 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))); @@ -24,6 +23,16 @@ bool _sway_assert(bool condition, const char* format, ...) __attribute__((format #define sway_assert(COND, FMT, ...) \ _sway_assert(COND, "%s:" FMT, __PRETTY_FUNCTION__, ##__VA_ARGS__) +#ifndef NDEBUG +void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) __attribute__((format(printf,4,5))); +#define sway_log(VERBOSITY, FMT, ...) \ + _sway_log(__FILE__, __LINE__, VERBOSITY, FMT, ##__VA_ARGS__) +#else +void _sway_log(log_importance_t verbosity, const char* format, ...) __attribute__((format(printf,2,3))); +#define sway_log(VERBOSITY, FMT, ...) \ + _sway_log(VERBOSITY, FMT, ##__VA_ARGS__) +#endif + void error_handler(int sig); void layout_log(const swayc_t *c, int depth); diff --git a/sway/log.c b/sway/log.c index 2ad9657d..f2b828f9 100644 --- a/sway/log.c +++ b/sway/log.c @@ -3,11 +3,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include int colored = 1; @@ -59,7 +61,11 @@ void sway_abort(const char *format, ...) { sway_terminate(); } -void sway_log(log_importance_t verbosity, const char* format, ...) { +#ifndef NDEBUG +void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) { +#else +void _sway_log(log_importance_t verbosity, const char* format, ...) { +#endif if (verbosity <= v) { unsigned int c = verbosity; if (c > sizeof(verbosity_colors) / sizeof(char *)) { @@ -72,6 +78,11 @@ void sway_log(log_importance_t verbosity, const char* format, ...) { va_list args; va_start(args, format); +#ifndef NDEBUG + char *file = strdup(filename); + fprintf(stderr, "[%s:%d] ", basename(file), line); + free(file); +#endif vfprintf(stderr, format, args); va_end(args); -- cgit v1.2.3-54-g00ecf