aboutsummaryrefslogtreecommitdiffstats
path: root/trace.cpp
blob: f5b50cc4f125c4b86841de665c13475a26d3c6fa (plain) (blame)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
// trace.cpp
// Revision 7-feb-2005

#include "trace.h"


#if __BORLANDC__ >= 0x0560
#pragma warn -8091
#endif

#include <iostream>
#include <fstream>
#include <string>

#ifdef HAVE_CSTDLIB
#include <cstdlib>
#else
#include <stdlib.h>
#endif

#include <stdexcept>

#include <string.h>

using std::cerr;
using std::endl;
using std::ostream;
using std::ofstream;
using std::string;


namespace {

ostream * pout= 0;
bool flag= true;
size_t indent= 0;

TraceFunc * initial= NULL;
TraceFunc * * lastpos= & initial;

ostream * opentracefile (const char * str)
{
	std::ofstream * pof=
		new ofstream (str, std::ios::app | std::ios::out);
	if (! pof->is_open () )
	{
		cerr << "Error opening " << str << endl;
		delete pof;
		pof= 0;
	}
	return pof;
}

void showinfo (const char * strenterexit, const char * strfunc)
{
	if (pout)
	{
		* pout << string (indent, ' ') << strenterexit << ' ';

		if (std::uncaught_exception () )
			* pout << "(throwing) ";

		* pout << strfunc << endl;
	}
}

const char BAD_USE []= "Bad use of TraceFunc";

} // namespace

TraceFunc::TraceFunc (const char * strFuncName) :
	strfunc (strFuncName),
	next (NULL)
{
	if (flag)
	{
		flag= false;
		char * aux= getenv ("TRACEFUNC");
		if (aux)
		{
			if (strcmp (aux, "-") == 0)
				pout= & std::cerr;
			else
				pout= opentracefile (aux);
		}
	}

	//tracelist.push_back (this);
	previous= lastpos;
	* lastpos= this;
	lastpos= & next;

	#if 0
	if (pout)
	{
		* pout << string (indent, ' ') << "Enter ";

		if (std::uncaught_exception () )
			* pout << "(throwing) ";

		* pout << strfunc << endl;
	}
	#else
	showinfo ("Enter", strfunc);
	#endif
	++indent;
}

TraceFunc::~TraceFunc ()
{
	--indent;

	#if 0
	if (pout)
	{
		* pout << string (indent, ' ') << "Exit ";

		if (std::uncaught_exception () )
			* pout << "(throwing) ";

		* pout << strfunc << endl;
	}
	#else
	showinfo ("Exit", strfunc);
	#endif

	if (next != NULL)
	{
		cerr << BAD_USE << endl;
		abort ();
	}
	if (lastpos != & next)
	{
		//throw std::logic_error ("Bad use of TraceFunc");
		cerr << BAD_USE << endl;
		abort ();
	}
	lastpos= previous;
	if (* lastpos != this)
	{
		cerr << BAD_USE << endl;
		abort ();
	}
	* lastpos= NULL;
}

void TraceFunc::message (const std::string & strMes)
{
	if (pout)
	{
		* pout << string (indent, ' ') << strfunc;

		if (std::uncaught_exception () )
			* pout << "(throwing) ";

		* pout << ": " << strMes << 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->strfunc << "\r\n";
		cerr << "TraceFunc dump ended.";
	}
	cerr << "\r\n";
}

// Fin de trace.cpp
Un proyecto texto-plano.xyz