CARMA C++
Trace.h
Go to the documentation of this file.
1 #ifndef CARMA_UTIL_TRACE_H
2 #define CARMA_UTIL_TRACE_H
3 
9 
10 
11 #include <sstream>
12 
13 #include "carma/util/FacilityType.h"
14 
15 
16 namespace log4cpp {
17  class Category;
18 }
19 
20 
21 // **** MACRO DEFINITIONS ****
22 
23 
27 #define CARMA_TRACELINE " From " << __FILE__ << ":" << __LINE__ << ": "
28 
29 
37 #define CARMA_TRACE(traceObject, priorityLevel, message) \
38  { \
39  carma::util::Trace * const carmaUtilTraceTO = traceObject; \
40  const carma::util::Trace::TraceLevel carmaUtilTracePL = priorityLevel; \
41  \
42  if ( (carmaUtilTraceTO != 0) && \
43  (carmaUtilTraceTO->willWrite( carmaUtilTracePL )) ) { \
44  ::std::ostringstream carmaUtilTraceOss; \
45  \
46  carmaUtilTraceOss << CARMA_TRACELINE << message; \
47  \
48  carmaUtilTraceTO->write( carmaUtilTracePL, carmaUtilTraceOss ); \
49  } \
50  }
51 
52 
59 #define CARMA_CPTRACE(priorityLevel, message) \
60  { \
61  const carma::util::Trace::TraceLevel carmaUtilTracePL = priorityLevel; \
62  carma::util::Trace * const carmaUtilTraceTO = \
63  carma::util::Trace::getProgramTraceIfWillWrite( carmaUtilTracePL ); \
64  \
65  if ( carmaUtilTraceTO != 0 ) { \
66  ::std::ostringstream carmaUtilTraceOss; \
67  \
68  carmaUtilTraceOss << CARMA_TRACELINE << message; \
69  \
70  carmaUtilTraceTO->write( carmaUtilTracePL, carmaUtilTraceOss ); \
71  } \
72  }
73 
74 
78 #define CPTRACE(priorityLevel, message) CARMA_CPTRACE(priorityLevel, message)
79 
80 
81 namespace carma {
82  namespace util {
83 
97  class Trace {
98  public:
99 
107  typedef enum {
108  TRACE0 = 1,
109  TRACE1 = 1,
110  TRACE2 = 2,
111  TRACE3 = 3,
112  TRACE4 = 4,
113  TRACE5 = 5,
114  TRACE6 = 6,
115  TRACE7 = 7,
116  TRACEALL = 7 // Deprecated
117  } TraceLevel;
118 
126  typedef enum {
127  STDOUT,
128  FILE,
129  SYSLOG,
130  SIMPLE
132 
148  Trace(TraceLevel traceLevel,
149  const std::string& traceDestination, bool traceVerbose=true,
150  const std::string& objectName="Trace",
151  carma::util::facilityType facility = DEFAULT_FACILITY
152  );
153 
154  virtual ~Trace();
155 
160 
161  static carma::util::Trace * getProgramTraceIfAvailable();
162 
163  static carma::util::Trace *
164  getProgramTraceIfWillWrite( TraceLevel traceLevel );
165 
170  void setObjectTraceLevel(const TraceLevel traceLevel);
171 
172  // output methods
177  void writeStdout(const std::string& debugMessage);
178 
183  void writeStdout(const std::ostringstream &debugMessage);
184 
189  void writeFile(const std::string& debugMessage);
190 
195  void writeFile(const std::ostringstream &debugMessage);
196 
201  void writeSysLog(const std::string& debugMessage);
202 
207  void writeSysLog(const std::ostringstream &debugMessage);
208 
215  void write(TraceLevel traceLevel, const std::string& debugMessage);
216 
223  void write(TraceLevel traceLevel, const std::ostringstream &debugMessage);
224 
230  {
231  return traceDestination_;
232  }
233 
234  bool willWrite( TraceLevel traceLevel ) const;
235 
236  private:
237  // used to obtain machine name for syslog category
238  // must be declared early so that it is initialised early
239  const ::std::string systemNodeName_;
240 
241  // destination where Trace output will be sent
242  const TraceDestination traceDestination_;
243 
244  // trace level for the Trace object
245  TraceLevel traceLevel_;
246 
247  // Set verbose in tracing, defaults to true
248  // If false, uses log4cpp::SimpleLayout
249  const bool traceVerbose_;
250 
251  // categories for sending trace information
252  log4cpp::Category & sysLogCategory;
253  log4cpp::Category & fileCategory;
254  log4cpp::Category & ostreamCategory;
255 
256  // methods for actually writing to stdout/file/syslog
257  void stdoutMethod(const std::string & debugMessage);
258  void fileMethod(const std::string & debugMessage);
259  void sysLogMethod(const std::string & debugMessage);
260  void writeMethod(TraceLevel traceLevel, const std::string & debugMessage);
261 
262  }; // end class Trace
263 
264 
265  // define output methods as inline functions
266 #ifdef NOTRACE
267  inline void Trace::writeStdout(const std::string& debugMessage) {}
268  inline void Trace::writeStdout(const std::ostringstream &debugMessage) {}
269  inline void Trace::writeFile(const std::string& debugMessage) {}
270  inline void Trace::writeFile(const std::ostringstream &debugMessage) {}
271  inline void Trace::writeSysLog(const std::string& debugMessage) {}
272  inline void Trace::writeSysLog(const std::ostringstream &debugMessage) {}
273  inline void Trace::write(const std::string& debugMessage) {}
274  inline void Trace::write(const std::ostringstream &debugMessage) {}
275 #else
276 
277  inline void Trace::writeStdout(const std::string& debugMessage)
278  {
279  stdoutMethod(debugMessage);
280  }
281 
282  inline void Trace::writeStdout(const std::ostringstream &debugMessage)
283  {
284  stdoutMethod(debugMessage.str());
285  }
286 
287  inline void Trace::writeFile(const std::string& debugMessage)
288  {
289  fileMethod(debugMessage);
290  }
291 
292  inline void Trace::writeFile(const std::ostringstream &debugMessage)
293  {
294  fileMethod(debugMessage.str());
295  }
296 
297  inline void Trace::writeSysLog(const std::string& debugMessage)
298  {
299  sysLogMethod(debugMessage);
300  }
301 
302  inline void Trace::writeSysLog(const std::ostringstream &debugMessage)
303  {
304  sysLogMethod(debugMessage.str());
305  }
306 
307  inline void Trace::write(TraceLevel traceLevel,
308  const std::string& debugMessage)
309  {
310  writeMethod(traceLevel, debugMessage);
311  }
312 
313  inline void Trace::write(TraceLevel traceLevel,
314  const std::ostringstream &debugMessage)
315  {
316  writeMethod(traceLevel, debugMessage.str());
317  }
318 
319 #endif // #ifndef TRACE_ON
320 
321  } // end namespace util
322 } // end namespace carma
323 
324 #endif // #ifndef CARMA_UTIL_TRACE_H
void writeFile(const std::string &debugMessage)
Send trace messages to a file.
Definition: Trace.h:287
void writeStdout(const std::string &debugMessage)
Send trace messages to standard output.
Definition: Trace.h:277
TraceDestination
Enumeration of possible output destinations.
Definition: Trace.h:126
TraceLevel
These values are the indices for the private priorities array, which corresponds directly to log4cpp/...
Definition: Trace.h:107
void writeSysLog(const std::string &debugMessage)
Send trace messages to syslog.
Definition: Trace.h:297
void write(TraceLevel traceLevel, const std::string &debugMessage)
Generic output method, output will go to destination specified in constructor.
Definition: Trace.h:307
static carma::util::Trace * getProgramTrace()
Trace(TraceLevel traceLevel, const std::string &traceDestination, bool traceVerbose=true, const std::string &objectName="Trace", carma::util::facilityType facility=DEFAULT_FACILITY)
Constructor.
void setObjectTraceLevel(const TraceLevel traceLevel)
method allowing traceLevel to be modified
facilityType
A type for syslog facilities.
Definition: FacilityType.h:15
TraceDestination getDestination() const
Definition: Trace.h:229
The Trace class provides an efficient means for managing debug statements throughout CARMA code...
Definition: Trace.h:97