congestvpn/util.hpp

63 lines
1.6 KiB
C++
Raw Normal View History

2020-06-02 13:08:23 +02:00
#pragma once
#include <exception>
#include <string>
2020-06-04 11:46:35 +02:00
/* 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
* to the message in `what()`.
*/
2020-06-02 13:08:23 +02:00
class MsgException : public std::exception {
public:
MsgException(const std::string& msg, int code=0, bool is_perror=false);
2020-06-02 13:08:23 +02:00
int errcode() const noexcept { return _code; }
const char* what() const noexcept { return _what.c_str(); };
private:
std::string _msg;
int _code;
std::string _what;
};