File: Trace.cpp

package info (click to toggle)
criticalmass 1%3A1.0.2-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,188 kB
  • sloc: ansic: 47,628; cpp: 25,201; sh: 11,834; xml: 3,532; perl: 3,271; makefile: 610; python: 72; awk: 40; lisp: 33
file content (202 lines) | stat: -rw-r--r-- 3,996 bytes parent folder | download | duplicates (11)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Description:
//   Tracing for the debug-minded person.
//
// Copyright (C) 2001 Frank Becker
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation;  either version 2 of the License,  or (at your option) any  later
// version.
//
// This program is distributed in the hope that it will be useful,  but  WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
//
#include <Trace.hpp>
#define TRACE

#include <iomanip>
#include <fstream>

int Trace::indent_=0;

#ifdef WIN32
static inline ofstream &tcout(void)
{
    static ofstream *_tcout = 0;
    if( _tcout==0)
    {
	_tcout = new ofstream("trace.txt");
    }
    return *_tcout;
}
#else
static inline ostream &tcout(void)
{
    return cout;
}
#endif

Trace::Trace( const char *class_name, const char *method, const char *params)
{
    tcout() << setw( indent_)  << ""
	 << class_name << "::" << method << "(" << params << ") {" << endl;
    indent_++;
}

Trace::Trace( const char *function, const char *params)
{
    tcout() << setw( indent_) << ""
	 << "::" << function << "(" << params << ") {" << endl;
    indent_++;
}

Trace::Trace( const char *str)
{
    tcout() << setw( indent_) << "" << str << " {" << endl;
    indent_++;
}

Trace::~Trace()
{
    indent_--;
    tcout() << setw( indent_) << "" << "}" << endl;
}

#ifdef TRACE
#ifdef WIN32
ofstream mcout("critterlog.txt");
#else
#define mcout cout
#endif

ostream &Trace::Log( int severity)
{
    char *type;

    switch( severity)
    {
	case Trace::eDEBUG:
	    type = "DEBUG";
	    break;
	case Trace::eINFO:
	    type = "INFO";
	    break;
	case Trace::eWARNING:
	    type = "WARNING";
	    break;
	case Trace::eERROR:
	    type = "*ERROR*";
	    break;
	case Trace::eFATAL:
	    type = "FATAL";
	    break;

	case Trace::eVOID:
            return mcout;

	default:
	    type = "TRACE";
	    break;
    }

    mcout << setw( indent_) << "" << type << ": ";
#else
ostream &Trace::Log( int)
{
#endif
    return mcout;
}

#ifdef DEBUG_TRACE
class MyClass
{
public:
    MyClass( void);
    ~MyClass();
};

MyClass::MyClass( void)
{
    MTRACE("MyClass", "MyClass", "void");

    if( 1)
    {
	STRACE("if(1)");
	LOG_INFO << "We always do this!" << endl;
    }
}

MyClass::~MyClass()
{
    MTRACE("MyClass", "~MyClass", "");
}

void myFunc( void)
{
    FTRACE("myFunc","void");
    int severity = Trace::FATAL;

    LOG(severity) << "hallo" << endl;
    LOG_INFO      << "info" << endl;
    LOG_WARNING   << "warning" << endl;
    LOG_ERROR     << "error" << endl;
}

int main( int, char *[])
{
    FTRACE("main","int, char *[]");
    MyClass c;

    myFunc();

    return 0;
}
#endif

#ifdef IDEAS
{
    Trace trace("CLASS::METHOD(PARAMS)");
    Trace trace("CLASS","METHOD","PARAMS");

    //timestamping
    Trace::EnableTimestamp();
    Trace::DisableTimestamp();

    //logging with severity
    Trace::Debug(...)
	Trace::Info(...)
	Trace::Warning(...)
	Trace::Error(...)
	Trace::Fatal(...)
	Trace::Log( severity, ...);

    //enables debug for given severities
    Trace::EnableSeverity( DEBUG | INFO | ALL_SEVERITIES);
    Trace::DisableSeverity( DEBUG);

    //sends output to file
    Trace::AddOutput( file);
    Trace::DelOutput( file);

    //config options
    Trace::ProcessCommandline( argc, argv);

#ifdef FANCY_TRACE
    //config options
    Trace::ProcessConfigFile( file);

    //listens and sends output to this ip:port
    Trace::AddOutput( ip,port);
    Trace::DelOutput( ip,port);

    //listens and accepts commands from port
    Trace::AddCommandPort( ip,port);
    Trace::DelCommandPort( ip,port);

    //select which classes and methods to trace
    Trace::Disable( "CLASS" | NULL, "METHOD" | NULL, "PARAMS" | NULL);
    Trace::Enable( "CLASS" | NULL, "METHOD" | NULL, "PARAMS" | NULL);
#endif //FANCE_TRACE
}
#endif