-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogicDebugger.hpp
67 lines (61 loc) · 1.43 KB
/
LogicDebugger.hpp
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
#ifndef LOGICDEBUGGER_H
#define LOGICDEBUGGER_H
#include <stack>
#include <iostream>
#include <sstream>
namespace LogicDebugger {
class Debug;
static stack<Debug*> _stack;
static char indent[] = "* ";
class Debug {
protected:
stringstream& inner() {
return m_inner;
}
public:
template <class T>
Debug( T clause )
: first( true ) {
for ( unsigned int i = 0; i < LogicDebugger::_stack.size(); ++i )
m_indention << indent;
LogicDebugger::_stack.push( this );
m_output << m_indention.str() << clause << ": ";
m_result_indent = string( m_output.str().length(), ' ' );
}
template <class T>
void operator()( T result ) {
if ( first ) {
m_output << result;
first = false;
}
else
m_output << '\n' << m_result_indent << result;
}
~Debug() {
assert( LogicDebugger::_stack.top() == this );
LogicDebugger::_stack.pop();
if ( LogicDebugger::_stack.size() > 0 ) {
Debug* parent = LogicDebugger::_stack.top();
parent->m_inner << m_output.str();
if ( m_inner.str().length() > 0 )
parent->m_inner << '\n' << m_inner.str();
else
parent->m_inner << '\n' ;
}
else {
cout << m_output.str();
if ( m_inner.str().length() > 0 )
cout << '\n' << m_inner.str();
else
cout << endl;
}
}
protected:
stringstream m_inner;
stringstream m_output;
stringstream m_indention;
string m_result_indent;
bool first;
};
}
#endif //LOGICDEBUGGER_H