cisst-saw
Loading...
Searching...
No Matches
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-2018 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://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
36
37#include <string>
38#include <vector>
39#include <fstream>
40
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) \
90if (!(cmnLogger::GetMask() & objectPointer->Services()->GetLogMask() & lod)) ; else \
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)
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)
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)) ; else \
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)
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 static std::string DefaultLogFileName;
282
284 static bool InstanceCreated;
285
288 void SetMaskInstance(cmnLogMask mask);
289
292 cmnLogMask GetMaskInstance(void);
293
296 void SetMaskFunctionInstance(cmnLogMask mask);
297
300 cmnLogMask GetMaskFunctionInstance(void);
301
304 StreamBufType * GetMultiplexerInstance(void);
305
307 std::ofstream * DefaultLogFile(const std::string & defaultLogFileName = DefaultLogFileName);
308
310 void HaltDefaultLogInstance(void);
311
313 void ResumeDefaultLogInstance(cmnLogMask newMask);
314
316 void AddChannelInstance(std::ostream & outputStream, cmnLogMask mask);
317
319 void RemoveChannelInstance(std::ostream & outputStream);
320
322 void KillInstance(void);
323
324 protected:
327 cmnLogger(const std::string & defaultLogFileName = DefaultLogFileName);
328
329 public:
336 static cmnLogger * Instance(void);
337
338
342 static void SetMask(cmnLogMask mask) {
343 Instance()->SetMaskInstance(mask);
344 }
345
347 Instance()->SetMaskInstance(mask);
348 }
349
353 static cmnLogMask GetMask(void) {
354 return Instance()->GetMaskInstance();
355 }
356
358 return Instance()->GetMaskInstance();
359 }
360
361
365 static void SetMaskFunction(cmnLogMask mask) {
366 Instance()->SetMaskFunctionInstance(mask);
367 }
368
369
374 return Instance()->GetMaskFunctionInstance();
375 }
376
377
386 static bool SetMaskClass(const std::string & className, cmnLogMask mask);
387
388
397 static bool SetMaskClassAll(cmnLogMask mask);
398
399
409 static bool SetMaskClassMatching(const std::string & stringToMatch, cmnLogMask mask);
410
411
421 return Instance()->GetMultiplexerInstance();
422 }
423
424
428 static void HaltDefaultLog(void) {
429 Instance()->HaltDefaultLogInstance();
430 }
431
432
439 Instance()->ResumeDefaultLogInstance(newLoD);
440 }
441
442
446 Instance()->HaltDefaultLogInstance();
447 Instance()->ResumeDefaultLogInstance(newLoD);
448 }
449
450
455 static inline void AddChannel(std::ostream & outputStream,
457 Instance()->AddChannelInstance(outputStream, mask);
458 }
459
460
464 Instance()->AddChannelInstance(std::cout, mask);
465 }
466
470 Instance()->AddChannelInstance(std::cerr, mask);
471 }
472
473 static inline void RemoveChannel(std::ostream & outputStream) {
474 Instance()->RemoveChannelInstance(outputStream);
475 }
476
477 static const char * ExtractFileName(const char * file);
478
481 static inline void Kill(void) {
482 Instance()->KillInstance();
483 }
484
488 static bool SetDefaultLogFileName(const std::string &defaultLogFileName);
489
491 static std::string GetDefaultLogFileName(void) { return DefaultLogFileName; }
492
494 static bool IsCreated() { return InstanceCreated; }
495};
496
497
498#endif // _cmnLogger_h
A Streambuffer class that allows output to multiple streambuf objects for Level of Detail information...
Definition cmnLODMultiplexerStreambuf.h:109
static bool IsCreated()
Definition cmnLogger.h:494
static void SetMaskDefaultLog(cmnLogMask newLoD=CMN_LOG_ALLOW_DEFAULT)
Definition cmnLogger.h:445
static void AddChannelToStdOut(cmnLogMask mask=CMN_LOG_ALLOW_ERRORS_AND_WARNINGS)
Definition cmnLogger.h:463
static StreamBufType * GetMultiplexer(void)
Definition cmnLogger.h:420
cmnLogger(const std::string &defaultLogFileName=DefaultLogFileName)
static void RemoveChannel(std::ostream &outputStream)
Definition cmnLogger.h:473
cmnLODMultiplexerStreambuf< char > StreamBufType
Definition cmnLogger.h:246
static bool SetMaskClassAll(cmnLogMask mask)
static const char * ExtractFileName(const char *file)
static cmnLogMask GetMask(void)
Definition cmnLogger.h:353
static void AddChannelToStdErr(cmnLogMask mask=CMN_LOG_ALLOW_ERRORS_AND_WARNINGS)
Definition cmnLogger.h:469
static void Kill(void)
Definition cmnLogger.h:481
static void AddChannel(std::ostream &outputStream, cmnLogMask mask=CMN_LOG_ALLOW_ALL)
Definition cmnLogger.h:455
static void SetMask(cmnLogMask mask)
Definition cmnLogger.h:342
static cmnLogMask CISST_DEPRECATED GetLoD(void)
Definition cmnLogger.h:357
static bool SetDefaultLogFileName(const std::string &defaultLogFileName)
static std::string GetDefaultLogFileName(void)
Definition cmnLogger.h:491
static bool SetMaskClass(const std::string &className, cmnLogMask mask)
static void HaltDefaultLog(void)
Definition cmnLogger.h:428
static cmnLogMask GetMaskFunction(void)
Definition cmnLogger.h:373
static cmnLogger * Instance(void)
static void ResumeDefaultLog(cmnLogMask newLoD=CMN_LOG_ALLOW_DEFAULT)
Definition cmnLogger.h:438
static bool SetMaskClassMatching(const std::string &stringToMatch, cmnLogMask mask)
static void CISST_DEPRECATED SetLoD(cmnLogMask mask)
Definition cmnLogger.h:346
static void SetMaskFunction(cmnLogMask mask)
Definition cmnLogger.h:365
Macros to export the symbols of cisstCommon (in a Dll).
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Type Definitions for dynamic control of output messages.
Types for dynamic control of output messages.
Declaration of Levels of Detail for cmnLogger (human readable logging).
#define CMN_LOG_ALLOW_ERRORS_AND_WARNINGS
Definition cmnLogLoD.h:72
short cmnLogMask
Definition cmnLogLoD.h:67
#define CMN_LOG_ALLOW_DEFAULT
Definition cmnLogLoD.h:76
#define CMN_LOG_ALLOW_ALL
Definition cmnLogLoD.h:75
Types for dynamic control of output messages.
Portability across compilers and operating systems tools.
#define CISST_DEPRECATED
Definition cmnPortability.h:314