-
Notifications
You must be signed in to change notification settings - Fork 3
/
trace.cpp
163 lines (129 loc) · 2.6 KB
/
trace.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// trace.cpp
// Revision 25-apr-2006
#include "trace.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <stdexcept>
#include <stdlib.h>
#include <string.h>
using std::cerr;
using std::endl;
using std::ostream;
using std::ofstream;
using std::string;
using std::find;
namespace {
ostream * pout= 0;
bool flag= true;
size_t indent= 0;
TraceFunc * initial= NULL;
TraceFunc * * lastpos= & initial;
ostream * opentracefile (const char * filename)
{
std::ofstream * pof=
new ofstream (filename, std::ios::app | std::ios::out);
if (! pof->is_open () )
{
cerr << "Error opening " << filename << endl;
delete pof;
pof= 0;
}
return pof;
}
string traceindent ()
{
//return string (indent / 8, '\t') + string (indent % 8, ' ');
return string (indent, ' ');
}
void showinfo (const char * enterexit, const char * funcname)
{
if (pout)
{
* pout << traceindent () << enterexit << ' ';
if (std::uncaught_exception () )
* pout << "(throwing) ";
* pout << funcname << endl;
}
}
const char BAD_USE []= "Bad use of TraceFunc";
} // namespace
TraceFunc::TraceFunc (const char * funcname_n, const char * shortname_n) :
funcname (funcname_n),
shortname (shortname_n ? shortname_n : funcname_n),
next (NULL)
{
if (flag)
{
flag= false;
char * aux= getenv ("TRFILE");
if (aux)
{
if (strcmp (aux, "-") == 0)
pout= & std::cerr;
else
pout= opentracefile (aux);
}
}
previous= lastpos;
* lastpos= this;
lastpos= & next;
showinfo ("Enter", funcname);
++indent;
}
TraceFunc::~TraceFunc ()
{
--indent;
showinfo ("Exit", funcname);
if (next != NULL)
{
cerr << BAD_USE << endl;
abort ();
}
if (lastpos != & next)
{
cerr << BAD_USE << endl;
abort ();
}
lastpos= previous;
if (* lastpos != this)
{
cerr << BAD_USE << endl;
abort ();
}
* lastpos= NULL;
}
void TraceFunc::message (const std::string & text)
{
if (pout)
{
* pout << traceindent () << shortname;
if (std::uncaught_exception () )
* pout << "(throwing) ";
* pout << ": " << text << endl;
}
}
void TraceFunc::show (int)
{
cerr << "\r\n";
if (initial == NULL)
cerr << "TraceFunc: no calls.";
else
{
cerr << "TraceFunc dump of calls: \r\n";
for (TraceFunc * act= initial; act != NULL; act= act->next)
cerr << act->funcname << "\r\n";
cerr << "TraceFunc dump ended.";
}
cerr << "\r\n";
}
void trace_assertion_failed (const char * a, const char * file, size_t line)
{
std::ostringstream oss;
oss << "Assertion failed: '" << a << "' in file " << file <<
" line " << line;
throw std::logic_error (oss.str () );
}
// Fin de trace.cpp