diff options
Diffstat (limited to 'tools/common/debug.h')
-rw-r--r-- | tools/common/debug.h | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/tools/common/debug.h b/tools/common/debug.h new file mode 100644 index 00000000..ace00876 --- /dev/null +++ b/tools/common/debug.h @@ -0,0 +1,68 @@ +#pragma once + +#include <functional> +#include <vector> +#include <sstream> + +typedef ::std::function<void(::std::ostream& os)> dbg_cb_t; +extern void Debug_SetPhase(const char* phase_name); +extern void Debug_DisablePhase(const char* phase_name); +extern bool Debug_IsEnabled(); +extern void Debug_EnterScope(const char* name, dbg_cb_t ); +extern void Debug_LeaveScope(const char* name, dbg_cb_t ); +extern void Debug_Print(dbg_cb_t cb); + +#if defined(NOLOG) +# define DEBUG(fmt) do { } while(0) +# define TRACE_FUNCTION_F(fmt) do{}while(0) +#else +# define DEBUG(fmt) do { Debug_Print([&](auto& os){ os << "DEBUG: " << fmt; }); } while(0) +# define TRACE_FUNCTION_F(fmt) DebugFunctionScope trace_function_hdr { __FUNCTION__, [&](auto& os){ os << fmt; } } +#endif +#define TODO(fmt) do { ::std::cerr << "TODO: " << fmt << ::std::endl; abort(); } while(0) + +namespace { + static inline void format_to_stream(::std::ostream& os) { + } + template<typename T, typename... A> + static inline void format_to_stream(::std::ostream& os, const T& v, const A&... a) { + os << v; + format_to_stream(os, a...); + } +} + +struct DebugFunctionScope { + const char* m_name; + DebugFunctionScope(const char* name, dbg_cb_t cb): + m_name(name) + { + Debug_EnterScope(m_name, cb); + } + ~DebugFunctionScope() + { + Debug_LeaveScope(m_name, [](auto& ){}); + } +}; + +template<typename ...T> +::std::string format(const T&... v) +{ + ::std::stringstream ss; + format_to_stream(ss, v...); + return ss.str(); +} + +template<typename T> +::std::ostream& operator<<(::std::ostream& os, const ::std::vector<T>& v) +{ + bool first = true; + for(const auto& e : v) + { + if(!first) + os << ","; + os << e; + first = false; + } + return os; +} + |