summaryrefslogtreecommitdiff
path: root/tools/common/debug.h
blob: 86c88de99a9f962d0e1c3b3869c8bdb671836d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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 void Debug_EnablePhase(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;
}