From c88ee2d6e9db6b67900cf2bc5556988efa3a0ea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Thu, 4 Jun 2020 11:46:35 +0200 Subject: [PATCH] Add debugging constructs --- util.cpp | 14 ++++++++++++++ util.hpp | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/util.cpp b/util.cpp index 185cd1c..5c9b2e2 100644 --- a/util.cpp +++ b/util.cpp @@ -1,8 +1,22 @@ #include #include +#include #include "util.hpp" +int debug; + +void do_debugf(int level, const char *format, ...) +{ + va_list args; + va_start(args, format); + if(debug >= level) { + vfprintf(stderr, format, args); + fflush(stderr); + } + va_end(args); +} + MsgException::MsgException(const std::string& msg, int code, bool is_perror) : _msg(msg), _code(code) { diff --git a/util.hpp b/util.hpp index 2adff8a..8f5ad63 100644 --- a/util.hpp +++ b/util.hpp @@ -3,6 +3,47 @@ #include #include +/* Debugging -- taken from babeld */ + +extern int debug; + +#if defined(__GNUC__) && (__GNUC__ >= 3) +#define ATTRIBUTE(x) __attribute__ (x) +#define LIKELY(_x) __builtin_expect(!!(_x), 1) +#define UNLIKELY(_x) __builtin_expect(!!(_x), 0) +#else +#define ATTRIBUTE(x) /**/ +#define LIKELY(_x) !!(_x) +#define UNLIKELY(_x) !!(_x) +#endif + +#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L +#define debugf(...) \ + do { \ + if(UNLIKELY(debug >= 2)) do_debugf(2, __VA_ARGS__); \ + } while(0) +#define kdebugf(...) \ + do { \ + if(UNLIKELY(debug >= 3)) do_debugf(3, __VA_ARGS__); \ + } while(0) +#elif defined __GNUC__ +#define debugf(_args...) \ + do { \ + if(UNLIKELY(debug >= 2)) do_debugf(2, _args); \ + } while(0) +#define kdebugf(_args...) \ + do { \ + if(UNLIKELY(debug >= 3)) do_debugf(3, _args); \ + } while(0) +#else +static inline void debugf(const char *format, ...) { return; } +static inline void kdebugf(const char *format, ...) { return; } +#endif + +void do_debugf(int level, const char *format, ...); + + + /** MsgException -- an exception bearing a passed explanation message * * If `is_perror` is true, then the `strerror` corresponding message is appened