genesys: Add utility to print debug messages upon function exit

merge-requests/84/head
Povilas Kanapickas 2019-06-30 14:05:26 +03:00
rodzic 397994b215
commit bba75702b6
2 zmienionych plików z 55 dodań i 0 usunięć

Wyświetl plik

@ -2115,6 +2115,45 @@ void run_functions_at_backend_exit()
s_functions_run_at_backend_exit.release();
}
#if (defined(__GNUC__) || defined(__CLANG__)) && (defined(__linux__) || defined(__APPLE__))
extern "C" char* __cxa_get_globals();
#endif
static unsigned num_uncaught_exceptions()
{
#if __cplusplus >= 201703L
int count = std::uncaught_exceptions();
return count >= 0 ? count : 0;
#elif (defined(__GNUC__) || defined(__CLANG__)) && (defined(__linux__) || defined(__APPLE__))
// the format of the __cxa_eh_globals struct is enshrined into the Itanium C++ ABI and it's
// very unlikely we'll get issues referencing it directly
char* cxa_eh_globals_ptr = __cxa_get_globals();
return *reinterpret_cast<unsigned*>(cxa_eh_globals_ptr + sizeof(void*));
#else
return std::uncaught_exception() ? 1 : 0;
#endif
}
DebugMessageHelper::DebugMessageHelper(const char* func)
{
func_ = func;
num_exceptions_on_enter_ = num_uncaught_exceptions();
DBG(DBG_proc, "%s: start", func_);
}
DebugMessageHelper::~DebugMessageHelper()
{
if (num_exceptions_on_enter_ < num_uncaught_exceptions()) {
if (status_) {
DBG(DBG_error, "%s: failed during %s", func_, status_);
} else {
DBG(DBG_error, "%s: failed", func_);
}
} else {
DBG(DBG_proc, "%s: completed", func_);
}
}
void debug_dump(unsigned level, const Genesys_Settings& settings)
{
DBG(level, "settings:\n"

Wyświetl plik

@ -1851,6 +1851,22 @@ extern void sanei_genesys_usleep(unsigned int useconds);
// same as sanei_genesys_usleep just that the duration is in milliseconds
extern void sanei_genesys_sleep_ms(unsigned int milliseconds);
class DebugMessageHelper {
public:
DebugMessageHelper(const char* func);
~DebugMessageHelper();
void status(const char* status) { status_ = status; }
void clear() { status_ = nullptr; }
private:
const char* func_ = nullptr;
const char* status_ = nullptr;
unsigned num_exceptions_on_enter_ = 0;
};
#define DBG_HELPER(var) DebugMessageHelper var(__func__)
template<class F>
SANE_Status wrap_exceptions_to_status_code(const char* func, F&& function)
{