cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnLogger.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5  Author(s): Anton Deguet
6  Created on: 2004-08-31
7 
8  (C) Copyright 2004-2015 Johns Hopkins University (JHU), All Rights Reserved.
9 
10 --- begin cisst license - do not edit ---
11 
12 This software is provided "as is" under an open source license, with
13 no warranty. The complete license can be found in license.txt and
14 http://www.cisst.org/cisst/license.txt.
15 
16 --- end cisst license ---
17 
18 */
19 
20 
26 #pragma once
27 
28 #ifndef _cmnLogger_h
29 #define _cmnLogger_h
30 
32 #include <cisstCommon/cmnLogLoD.h>
36 
37 #include <string>
38 #include <vector>
39 #include <fstream>
40 
41 #include <cisstCommon/cmnExport.h>
42 
43 // MJ: some thirdparty drivers on QNX make CMN_LOG macro throw exceptions
44 // (e.g., std::bad_cast) and the following preprocessor can be used to bypass
45 // this issue by replacing CMN_LOG with std::cout.
46 #if (CISST_OS == CISST_QNX)
47 #define BYPASS_CMN_LOG
48 #endif
49 
50 
82 #ifdef BYPASS_CMN_LOG
83 
84  #undef CMN_LOG_CLASS_INSTANCE
85  #define CMN_LOG_CLASS_INSTANCE(objectPointer, lod) std::cout
86 
87 #else
88 
89  #define CMN_LOG_CLASS_INSTANCE(objectPointer, lod) \
90  if (cmnLogger::GetMask() & objectPointer->Services()->GetLogMask() & lod) \
91  ((cmnLODOutputMultiplexer(objectPointer->GetLogMultiplexer(), lod).Ref()) << cmnLogLevelToString(lod) << " Class " << objectPointer->Services()->GetName() << ": ")
92 
93 #endif
94 
95 #define CMN_LOG_CLASS(lod) CMN_LOG_CLASS_INSTANCE(this, lod)
96 
97 
100 #define CMN_LOG_CLASS_INSTANCE_INIT_ERROR(objectPointer) CMN_LOG_CLASS_INSTANCE(objectPointer, CMN_LOG_LEVEL_INIT_ERROR) << CMN_LOG_DETAILS << " "
101 #define CMN_LOG_CLASS_INSTANCE_INIT_WARNING(objectPointer) CMN_LOG_CLASS_INSTANCE(objectPointer, CMN_LOG_LEVEL_INIT_WARNING)
102 #define CMN_LOG_CLASS_INSTANCE_INIT_VERBOSE(objectPointer) CMN_LOG_CLASS_INSTANCE(objectPointer, CMN_LOG_LEVEL_INIT_VERBOSE)
103 #define CMN_LOG_CLASS_INSTANCE_INIT_DEBUG(objectPointer) CMN_LOG_CLASS_INSTANCE(objectPointer, CMN_LOG_LEVEL_INIT_DEBUG)
104 #define CMN_LOG_CLASS_INSTANCE_RUN_ERROR(objectPointer) CMN_LOG_CLASS_INSTANCE(objectPointer, CMN_LOG_LEVEL_RUN_ERROR) << CMN_LOG_DETAILS << " "
105 #define CMN_LOG_CLASS_INSTANCE_RUN_WARNING(objectPointer) CMN_LOG_CLASS_INSTANCE(objectPointer, CMN_LOG_LEVEL_RUN_WARNING)
106 #define CMN_LOG_CLASS_INSTANCE_RUN_VERBOSE(objectPointer) CMN_LOG_CLASS_INSTANCE(objectPointer, CMN_LOG_LEVEL_RUN_VERBOSE)
107 #define CMN_LOG_CLASS_INSTANCE_RUN_DEBUG(objectPointer) CMN_LOG_CLASS_INSTANCE(objectPointer, CMN_LOG_LEVEL_RUN_DEBUG)
108 
109 
110 
113 #define CMN_LOG_CLASS_INIT_ERROR CMN_LOG_CLASS(CMN_LOG_LEVEL_INIT_ERROR) << CMN_LOG_DETAILS << " "
114 #define CMN_LOG_CLASS_INIT_WARNING CMN_LOG_CLASS(CMN_LOG_LEVEL_INIT_WARNING)
115 #define CMN_LOG_CLASS_INIT_VERBOSE CMN_LOG_CLASS(CMN_LOG_LEVEL_INIT_VERBOSE)
116 #define CMN_LOG_CLASS_INIT_DEBUG CMN_LOG_CLASS(CMN_LOG_LEVEL_INIT_DEBUG)
117 #define CMN_LOG_CLASS_RUN_ERROR CMN_LOG_CLASS(CMN_LOG_LEVEL_RUN_ERROR) << CMN_LOG_DETAILS << " "
118 #define CMN_LOG_CLASS_RUN_WARNING CMN_LOG_CLASS(CMN_LOG_LEVEL_RUN_WARNING)
119 #define CMN_LOG_CLASS_RUN_VERBOSE CMN_LOG_CLASS(CMN_LOG_LEVEL_RUN_VERBOSE)
120 #define CMN_LOG_CLASS_RUN_DEBUG CMN_LOG_CLASS(CMN_LOG_LEVEL_RUN_DEBUG)
121 
122 
123 
147 #ifdef BYPASS_CMN_LOG
148 
149  #undef CMN_LOG
150  #define CMN_LOG(lod) std::cout
151 
152 #else
153 
154  #define CMN_LOG(lod) \
155  if (cmnLogger::GetMask() & cmnLogger::GetMaskFunction() & lod) \
156  ((cmnLODOutputMultiplexer(cmnLogger::GetMultiplexer(), lod).Ref()) << cmnLogLevelToString(lod) << " ")
157 
158 #endif
159 
162 #define CMN_LOG_INIT_ERROR CMN_LOG(CMN_LOG_LEVEL_INIT_ERROR) << CMN_LOG_DETAILS << " "
163 #define CMN_LOG_INIT_WARNING CMN_LOG(CMN_LOG_LEVEL_INIT_WARNING)
164 #define CMN_LOG_INIT_VERBOSE CMN_LOG(CMN_LOG_LEVEL_INIT_VERBOSE)
165 #define CMN_LOG_INIT_DEBUG CMN_LOG(CMN_LOG_LEVEL_INIT_DEBUG)
166 #define CMN_LOG_RUN_ERROR CMN_LOG(CMN_LOG_LEVEL_RUN_ERROR) << CMN_LOG_DETAILS << " "
167 #define CMN_LOG_RUN_WARNING CMN_LOG(CMN_LOG_LEVEL_RUN_WARNING)
168 #define CMN_LOG_RUN_VERBOSE CMN_LOG(CMN_LOG_LEVEL_RUN_VERBOSE)
169 #define CMN_LOG_RUN_DEBUG CMN_LOG(CMN_LOG_LEVEL_RUN_DEBUG)
170 
171 
172 
186 #define CMN_LOG_DETAILS \
187  "File: " << cmnLogger::ExtractFileName(__FILE__) << " Line: " << __LINE__ << " - "
188 
189 
190 
243 
244  public:
247 
248  private:
268  cmnLogMask Mask;
269 
274  cmnLogMask FunctionMask;
275 
277  StreamBufType LoDMultiplexerStreambuf;
278 
281  void SetMaskInstance(cmnLogMask mask);
282 
285  cmnLogMask GetMaskInstance(void);
286 
289  void SetMaskFunctionInstance(cmnLogMask mask);
290 
293  cmnLogMask GetMaskFunctionInstance(void);
294 
297  StreamBufType * GetMultiplexerInstance(void);
298 
300  std::ofstream * DefaultLogFile(const std::string & defaultLogFileName = "cisstLog.txt");
301 
303  void HaltDefaultLogInstance(void);
304 
306  void ResumeDefaultLogInstance(cmnLogMask newMask);
307 
309  void AddChannelInstance(std::ostream & outputStream, cmnLogMask mask);
310 
312  void RemoveChannelInstance(std::ostream & outputStream);
313 
315  void KillInstance(void);
316 
317  protected:
320  cmnLogger(const std::string & defaultLogFileName = "cisstLog.txt");
321 
322  public:
329  static cmnLogger * Instance(void);
330 
331 
335  static void SetMask(cmnLogMask mask) {
336  Instance()->SetMaskInstance(mask);
337  }
338 
339  static void CISST_DEPRECATED SetLoD(cmnLogMask mask) {
340  Instance()->SetMaskInstance(mask);
341  }
342 
346  static cmnLogMask GetMask(void) {
347  return Instance()->GetMaskInstance();
348  }
349 
351  return Instance()->GetMaskInstance();
352  }
353 
354 
358  static void SetMaskFunction(cmnLogMask mask) {
359  Instance()->SetMaskFunctionInstance(mask);
360  }
361 
362 
367  return Instance()->GetMaskFunctionInstance();
368  }
369 
370 
379  static bool SetMaskClass(const std::string & className, cmnLogMask mask);
380 
381 
390  static bool SetMaskClassAll(cmnLogMask mask);
391 
392 
402  static bool SetMaskClassMatching(const std::string & stringToMatch, cmnLogMask mask);
403 
404 
413  static StreamBufType * GetMultiplexer(void) {
414  return Instance()->GetMultiplexerInstance();
415  }
416 
417 
421  static void HaltDefaultLog(void) {
422  Instance()->HaltDefaultLogInstance();
423  }
424 
425 
432  Instance()->ResumeDefaultLogInstance(newLoD);
433  }
434 
435 
439  Instance()->HaltDefaultLogInstance();
440  Instance()->ResumeDefaultLogInstance(newLoD);
441  }
442 
443 
448  static inline void AddChannel(std::ostream & outputStream,
449  cmnLogMask mask = CMN_LOG_ALLOW_ALL) {
450  Instance()->AddChannelInstance(outputStream, mask);
451  }
452 
453 
457  Instance()->AddChannelInstance(std::cout, mask);
458  }
459 
463  Instance()->AddChannelInstance(std::cerr, mask);
464  }
465 
466  static inline void RemoveChannel(std::ostream & outputStream) {
467  Instance()->RemoveChannelInstance(outputStream);
468  }
469 
470  static const char * ExtractFileName(const char * file);
471 
474  static inline void Kill(void) {
475  Instance()->KillInstance();
476  }
477 
478 };
479 
480 
481 #endif // _cmnLogger_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
static void AddChannelToStdOut(cmnLogMask mask=CMN_LOG_ALLOW_ERRORS_AND_WARNINGS)
Definition: cmnLogger.h:456
static void ResumeDefaultLog(cmnLogMask newLoD=CMN_LOG_ALLOW_DEFAULT)
Definition: cmnLogger.h:431
static void SetMask(cmnLogMask mask)
Definition: cmnLogger.h:335
Types for dynamic control of output messages.
#define CISST_DEPRECATED
Definition: cmnPortability.h:310
static cmnLogMask GetMaskFunction(void)
Definition: cmnLogger.h:366
static void SetMaskFunction(cmnLogMask mask)
Definition: cmnLogger.h:358
Portability across compilers and operating systems tools.
static void AddChannel(std::ostream &outputStream, cmnLogMask mask=CMN_LOG_ALLOW_ALL)
Definition: cmnLogger.h:448
Declaration of Levels of Detail for cmnLogger (human readable logging)
Class to glue the class register, the output multiplexer and the message level of detail to form the ...
Definition: cmnLogger.h:242
static void AddChannelToStdErr(cmnLogMask mask=CMN_LOG_ALLOW_ERRORS_AND_WARNINGS)
Definition: cmnLogger.h:462
#define CMN_LOG_ALLOW_ERRORS_AND_WARNINGS
Definition: cmnLogLoD.h:72
Macros to export the symbols of cisstCommon (in a Dll).
Type Definitions for dynamic control of output messages.
static StreamBufType * GetMultiplexer(void)
Definition: cmnLogger.h:413
short cmnLogMask
Definition: cmnLogLoD.h:67
static cmnLogMask CISST_DEPRECATED GetLoD(void)
Definition: cmnLogger.h:350
static void Kill(void)
Definition: cmnLogger.h:474
static void RemoveChannel(std::ostream &outputStream)
Definition: cmnLogger.h:466
static void CISST_DEPRECATED SetLoD(cmnLogMask mask)
Definition: cmnLogger.h:339
static void HaltDefaultLog(void)
Definition: cmnLogger.h:421
#define CMN_LOG_ALLOW_ALL
Definition: cmnLogLoD.h:75
Types for dynamic control of output messages.
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76
static void SetMaskDefaultLog(cmnLogMask newLoD=CMN_LOG_ALLOW_DEFAULT)
Definition: cmnLogger.h:438
static cmnLogMask GetMask(void)
Definition: cmnLogger.h:346
cmnLODMultiplexerStreambuf< char > StreamBufType
Definition: cmnLogger.h:246