VICI  0.11.815
Visual Chart Interpreter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
trace.h
Go to the documentation of this file.
1 /*
2  * trace.h
3  *
4  * Copyright 2006 - 2016 Brenton Ross
5  *
6  * This file is part of VICI.
7  *
8  * VICI is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * VICI is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with VICI. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
26 #ifndef CDI_TRACE_H
27 #define CDI_TRACE_H
28 
29 
30 #include "stringy.h"
31 #include <map>
32 #include <sstream>
33 #include <mutex>
34 
35 namespace VICI
36 {
37 
38 namespace cdi
39 {
41 #ifdef _VICI_TRACING
42 #define TRACE(n) VICI::cdi::Trace( n, __FILE__ ) << __LINE__ << ": "
43 #else
44 #define TRACE(n) VICI::cdi::NullTrace()
45 #endif
46 
48 #ifdef _VICI_FN_TRACING
49 #define FN_TRACE(n,x) VICI::cdi::CallTrace _trace( n, x, __FILE__ )
50 #else
51 #define FN_TRACE(n,x)
52 #endif
53 
54 // ---------------------------------------------------------------
55 
57 
65 class CallTrace
66 {
67 private:
68  int debugLevel; // level of the statement
69  std::string mFile; // source file
70 
71 public:
73 
80  CallTrace( int level, csr methodName, csr file );
81 
83  ~CallTrace();
84 
85 };
86 
87 // ---------------------------------------------------------------
88 //
89 
91 
98 class NullTrace
99 {
100 public:
103 
105  template <class T> NullTrace & operator << ( T x )
106  {
107  return *this;
108  }
109 };
110 
111 // ---------------------------------------------------------------
112 
114 
123 class Trace
124 {
125 private:
126  int debugLevel; // level of the statement
127  std::ostringstream ss; // build message in here
128  std::string mFile; // source file
129 
130 public:
132 
135  explicit Trace( int n );
136 
138 
142  Trace( int n, csr file );
143 
145  template < class T > Trace & operator << ( T x )
146  {
147  ss << x;
148  return *this;
149  }
150 
152  ~Trace();
153 };
154 
155 // ---------------------------------------------------------------
156 
158 
173 class Tracer
174 {
175 private:
176  //static Tracer *me;
177  Tracer();
178  std::map< pid_t, int > callLevel; // track indent level
179  std::map< pid_t, bool > doBraces;
180  std::mutex mx;
181  int debugLevel; // only statements <= this get reported
182  static const int DISABLED = -1; // turn off all tracing
183  std::ostream * mLog;
184  std::string appName;
185  std::string logName;
186 
187  std::map< std::string, int > sourceMap; // debug level for source files
188 
189  static void signalHandler( int );
190  bool reread;
191 
192  void getConfig();
193  // void trim( std::string & );
194  // void basename( std::string & );
195 
196 public:
198  static Tracer & instance();
199 
201  enum CallEntry{ ENTER=+1, EXIT=-1, NONE=0 };
202 
204 
210  void log( csr file, int level, CallEntry call, csr text );
211 };
212 
213 // ---------------------------------------------------------------
214 
215 
216 
217 } // namespace cdi
218 } // namespace VICI
219 
220 
221 #endif /* TRACE_H_ */
Trace(int n)
Constructor.
Definition: trace.cpp:69
CallEntry
Indicator for entry or exit of a function.
Definition: trace.h:201
This class is used to create trace log entries.
Definition: trace.h:123
Useful string functions.
A class for dummy trace objects.
Definition: trace.h:98
~CallTrace()
Destructor.
Definition: trace.cpp:58
This class manages the tracing for an application.
Definition: trace.h:173
NullTrace & operator<<(T x)
Noop version of stream function.
Definition: trace.h:105
NullTrace()
Constructor.
Definition: trace.h:102
Trace & operator<<(T x)
Stream output operator.
Definition: trace.h:145
Class to create a call trace.
Definition: trace.h:65
~Trace()
Destructor.
Definition: trace.cpp:83
const std::string & csr
short cut for string constants
Definition: vici.h:80
CallTrace(int level, csr methodName, csr file)
Constructor.
Definition: trace.cpp:48
void log(csr file, int level, CallEntry call, csr text)
Write a trace line to the log stream.
Definition: trace.cpp:189
static Tracer & instance()
Get an instance of the singleton object.
Definition: trace.cpp:179