diff --git a/backend/genesys_error.cc b/backend/genesys_error.cc index 29439ba99..c98a49003 100644 --- a/backend/genesys_error.cc +++ b/backend/genesys_error.cc @@ -44,6 +44,11 @@ #define DEBUG_DECLARE_ONLY #include "genesys_error.h" +#include +#include + +extern "C" void sanei_debug_msg(int level, int max_level, const char *be, const char *fmt, + va_list ap); #if (defined(__GNUC__) || defined(__CLANG__)) && (defined(__linux__) || defined(__APPLE__)) extern "C" char* __cxa_get_globals(); @@ -68,18 +73,43 @@ DebugMessageHelper::DebugMessageHelper(const char* func) { func_ = func; num_exceptions_on_enter_ = num_uncaught_exceptions(); - DBG(DBG_proc, "%s: start", func_); + msg_[0] = '\0'; + DBG(DBG_proc, "%s: start\n", func_); } +DebugMessageHelper::DebugMessageHelper(const char* func, const char* format, ...) +{ + func_ = func; + num_exceptions_on_enter_ = num_uncaught_exceptions(); + msg_[0] = '\0'; + DBG(DBG_proc, "%s: start\n", func_); + DBG(DBG_proc, "%s: ", func_); + + std::va_list args; + va_start(args, format); + sanei_debug_msg(DBG_proc, DBG_LEVEL, STRINGIFY(BACKEND_NAME), format, args); + va_end(args); + DBG(DBG_proc, "\n"); +} + + DebugMessageHelper::~DebugMessageHelper() { if (num_exceptions_on_enter_ < num_uncaught_exceptions()) { - if (status_) { - DBG(DBG_error, "%s: failed during %s", func_, status_); + if (msg_[0] != '\0') { + DBG(DBG_error, "%s: failed during %s\n", func_, msg_); } else { - DBG(DBG_error, "%s: failed", func_); + DBG(DBG_error, "%s: failed\n", func_); } } else { - DBG(DBG_proc, "%s: completed", func_); + DBG(DBG_proc, "%s: completed\n", func_); } } + +void DebugMessageHelper::vstatus(const char* format, ...) +{ + std::va_list args; + va_start(args, format); + std::vsnprintf(msg_, MAX_BUF_SIZE, format, args); + va_end(args); +} diff --git a/backend/genesys_error.h b/backend/genesys_error.h index cec9ecd30..d4565812f 100644 --- a/backend/genesys_error.h +++ b/backend/genesys_error.h @@ -129,19 +129,34 @@ private: class DebugMessageHelper { public: + static constexpr unsigned MAX_BUF_SIZE = 120; + DebugMessageHelper(const char* func); + DebugMessageHelper(const char* func, const char* format, ...) + #ifdef __GNUC__ + __attribute__((format(printf, 3, 4))) + #endif + ; + ~DebugMessageHelper(); - void status(const char* status) { status_ = status; } - void clear() { status_ = nullptr; } + void status(const char* msg) { vstatus("%s", msg); } + void vstatus(const char* format, ...) + #ifdef __GNUC__ + __attribute__((format(printf, 2, 3))) + #endif + ; + + void clear() { msg_[0] = '\n'; } private: const char* func_ = nullptr; - const char* status_ = nullptr; + char msg_[MAX_BUF_SIZE]; unsigned num_exceptions_on_enter_ = 0; }; #define DBG_HELPER(var) DebugMessageHelper var(__func__) +#define DBG_HELPER_ARGS(var, ...) DebugMessageHelper var(__func__, __VA_ARGS__) template SANE_Status wrap_exceptions_to_status_code(const char* func, F&& function)