From 5b0450395af81ceba0d0ac27fc73b16f966bd7d3 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 18 Mar 2018 10:48:26 +0800 Subject: All - Move toml parser and path header to a common library, start on custom target specs. --- tools/common/debug.h | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tools/common/debug.h (limited to 'tools/common/debug.h') 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 +#include +#include + +typedef ::std::function 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 + 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 +::std::string format(const T&... v) +{ + ::std::stringstream ss; + format_to_stream(ss, v...); + return ss.str(); +} + +template +::std::ostream& operator<<(::std::ostream& os, const ::std::vector& v) +{ + bool first = true; + for(const auto& e : v) + { + if(!first) + os << ","; + os << e; + first = false; + } + return os; +} + -- cgit v1.2.3