summaryrefslogtreecommitdiff
path: root/tools/minicargo/debug.cpp
blob: 64d8020927ff8e10272b9aaa48e89d6c14be7b1a (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
/*
 * MiniCargo - mrustc's minimal clone of cargo
 * - By John Hodge (Mutabah/thePowersGang)
 *
 * debug.cpp
 * - Debugging helpers
 */
#include <set>
#include <iostream>
#include "debug.h"

static int giIndentLevel = 0;
static const char* gsDebugPhase = "";
static ::std::set<::std::string> gmDisabledDebug;
void Debug_SetPhase(const char* phase_name)
{
    gsDebugPhase = phase_name;
}
bool Debug_IsEnabled()
{
    if( gmDisabledDebug.find(gsDebugPhase) != gmDisabledDebug.end() )
        return false;
    return true;
}
void Debug_DisablePhase(const char* phase_name)
{
    gmDisabledDebug.insert( ::std::string(phase_name) );
}
void Debug_Print(::std::function<void(::std::ostream& os)> cb)
{
    if( !Debug_IsEnabled() )
        return ;
    ::std::cout << gsDebugPhase << "- ";
    for(auto i = giIndentLevel; i --; )
        ::std::cout << " ";
    cb(::std::cout);
    ::std::cout << ::std::endl;
}
void Debug_EnterScope(const char* name, dbg_cb_t cb)
{
    if( !Debug_IsEnabled() )
        return ;
    ::std::cout << gsDebugPhase << "- ";
    for(auto i = giIndentLevel; i --; )
        ::std::cout << " ";
    ::std::cout << ">>> " << name << "(";
    cb(::std::cout);
    ::std::cout << ")" << ::std::endl;
    giIndentLevel ++;
}
void Debug_LeaveScope(const char* name, dbg_cb_t cb)
{
    if( !Debug_IsEnabled() )
        return ;
    ::std::cout << gsDebugPhase << "- ";
    giIndentLevel --;
    for(auto i = giIndentLevel; i --; )
        ::std::cout << " ";
    ::std::cout << "<<< " << name << ::std::endl;
}