Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cLogMgr.h
Go to the documentation of this file.
1 //
6 //
7 
8 #ifndef _INC_cLogMgr_H
9 #define _INC_cLogMgr_H
10 #ifndef NO_PRAGMA_ONCE
11 #pragma once
12 #endif
13 
14 #include "cLogAppender.h"
15 #include "cDebugAssert.h"
16 #include "cTimeSys.h"
17 #include "IUnknown.h"
18 #include "cSingleton.h"
19 #include "cTimeUnits.h"
20 #include "cArrayRef.h"
21 #include "cArrayIUnk.h"
22 #include "cException.h"
23 #include "HResult.h"
24 #include "cUnitTestDecl.h"
25 #include "cTypeInfo.h"
26 
27 // #define _DEBUG_FAST // put debug in release mode optimized code.
28 
29 namespace Gray
30 {
32  {
40 
41  public:
42  const char* m_pszSubject;
43  public:
44  cLogSubject(const char* pszSubject);
45  virtual ~cLogSubject();
46 
47  virtual HRESULT addEvent(cLogEvent* pEvent) override; // ILogProcessor
48  };
49 
51  : public cLogProcessor // can submit directly instead of using cLogSubject = default
52  {
59 
60  public:
64 
65  protected:
67 
68  public:
69  cLogNexus(LOG_ATTR_MASK_t uAttrMask = LOG_ATTR_ALL_MASK, LOGLEV_TYPE eLogLevel = LOGLEV_ANY);
70  virtual ~cLogNexus();
71 
72  virtual const cLogNexus* get_ThisLogNexus() const override
73  {
75  return this;
76  }
77  virtual bool IsLogged(LOG_ATTR_MASK_t uAttrMask, LOGLEV_TYPE eLogLevel) const override // fast pre-check.
78  {
80  return m_LogFilter.IsLogged(uAttrMask, eLogLevel);
81  }
82  virtual HRESULT addEvent(cLogEvent* pEvent) override;
83  virtual HRESULT FlushLogs() override;
84 
85  // manage appenders
87  {
88  return m_aAppenders.GetAtCheck(i);
89  }
91  {
92  return m_aAppenders.GetAtCheck(i);
93  }
94  bool HasAppender(cLogAppender* pAppender, bool bDescend = false) const;
95  HRESULT AddAppender(cLogAppender* pAppender);
96  bool RemoveAppender(cLogAppender* pAppender, bool bDescend = false);
97  cLogAppender* FindAppenderType(const TYPEINFO_t& rType, bool bDescend = false) const;
98 
99  bool RemoveAppenderType(const TYPEINFO_t& rType, bool bDescend=true)
100  {
101  return RemoveAppender(FindAppenderType(rType, bDescend), bDescend);
102  }
103  };
104 
106  : public cSingleton < cLogMgr >
107  , public cLogNexus
108  {
112  friend class cSingleton < cLogMgr >;
113 
114  private:
115  static TIMESECD_t sm_TimePrevException;
116 
117  protected:
118  cLogMgr();
119  virtual ~cLogMgr();
120 
121  public:
122 
123 #ifdef _CPPUNWIND
124  // Logging of cException.
125  // has no arguments
126  void LogExceptionV(cExceptionHolder* pEx, const LOGCHAR_t* pszCatchContext, va_list vargs);
127  void _cdecl LogExceptionF(cExceptionHolder* pEx, const LOGCHAR_t* pszCatchContext, ...);
128 #endif
129 
131  virtual HRESULT WriteString(const LOGCHAR_t* pszStr) override;
132  virtual HRESULT WriteString(const wchar_t* pszStr) override;
133 
136  };
137 
138  //***********************************************************************************
139  // Stuff that should stay in release mode.
140 #define LOGF(_x_) ::Gray::cLogMgr::I().addEventF _x_ // needs attributes and log level.
141 
142  // We usually leave this stuff in for release code ?
143 #ifndef DEBUG_ERR
144 #define DEBUG_ERR(_x_) ::Gray::cLogMgr::I().addDebugErrorF _x_
145 #define DEBUG_WARN(_x_) ::Gray::cLogMgr::I().addDebugWarnF _x_
146 #endif
147 
148  // Log messages that compile out when not wanted. only in debug mode.
149 #if ! defined(DEBUG_MSG) && ! defined(USE_DEBUG_LOG) && ( defined(_DEBUG) || defined(_DEBUG_FAST))
150 #define USE_DEBUG_LOG
151 #endif
152 #ifdef USE_DEBUG_LOG
153 #define DEBUG_LOGF(_x_) ::Gray::cLogMgr::I().addEventF _x_ // needs attributes and log level.
154 #define DEBUG_MSG(_x_) ::Gray::cLogMgr::I().addDebugInfoF _x_
155 #define DEBUG_TRACE(_x_) ::Gray::cLogMgr::I().addDebugTraceF _x_
156 #else
157 #define DEBUG_LOGF(_x_) __noop // compiled out.
158 #define DEBUG_MSG(_x_) __noop // compiled out.
159 #define DEBUG_TRACE(_x_) __noop // this allows all the variable args to be compiled out
160 #endif
161 
162 #ifdef _CPPUNWIND
163 
164 #define GEXCEP_CATCH_LOG(desc) GRAY_TRY_CATCH( ::Gray::cExceptionBase, ex )\
165  {\
166  ::Gray::cExceptionHolder exh(ex);\
167  ::Gray::cLogMgr::I().LogExceptionV(&exh,desc,nullptr);\
168  }
169  // has 1 arg for context
170 #define GEXCEP_CATCH_LOG1(desc,arg) GRAY_TRY_CATCH( ::Gray::cExceptionBase, ex )\
171  {\
172  ::Gray::cExceptionHolder exh(ex);\
173  ::Gray::cLogMgr::I().LogExceptionF(&exh,desc,arg);\
174  }
175 #else
176 #define GEXCEP_CATCH_LOG(desc) GRAY_TRY_CATCH( ::Gray::cExceptionBase, ex )
177 #define GEXCEP_CATCH_LOG1(desc,arg) GRAY_TRY_CATCH( ::Gray::cExceptionBase, ex )
178 #endif
179 };
180 #endif // _INC_cLogMgr_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define CHEAPOBJECT_IMPL
Definition: cHeapObject.h:32
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
REF_t GetAtCheck(ITERATE_t index) const
Definition: cArray.h:834
Definition: cArrayIUnk.h:19
Definition: cException.h:28
Definition: cLogAppender.h:257
Definition: cLogAppender.h:72
bool IsLogged(LOG_ATTR_MASK_t uAttrMask, LOGLEV_TYPE eLogLevel) const
Definition: cLogAppender.h:117
Definition: cLogEvent.h:18
Definition: cLogMgr.h:108
Definition: cLogMgr.h:52
virtual const cLogNexus * get_ThisLogNexus() const override
Definition: cLogMgr.h:72
virtual bool IsLogged(LOG_ATTR_MASK_t uAttrMask, LOGLEV_TYPE eLogLevel) const override
Definition: cLogMgr.h:77
const cLogAppender * EnumAppender(ITERATE_t i) const
Definition: cLogMgr.h:90
cLogAppender * EnumAppender(ITERATE_t i)
Definition: cLogMgr.h:86
cArrayIUnk< cLogAppender > m_aAppenders
where do the log messages go? child appenders.
Definition: cLogMgr.h:66
cThreadLockCount m_LockLog
serialize multiple threads for m_aAppenders
Definition: cLogMgr.h:63
cLogEventParams m_LogFilter
Union filter what goes out to ALL appenders.
Definition: cLogMgr.h:61
cLogThrottle m_LogThrottle
Measure how fast messages are going.
Definition: cLogMgr.h:62
Definition: cLogAppender.h:168
Definition: cLogMgr.h:32
const char * m_pszSubject
static string. general subject matter tag.
Definition: cLogMgr.h:42
Definition: cLogAppender.h:129
Definition: cSingleton.h:127
Definition: cThreadLock.h:498
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
@ LOG_ATTR_ALL_MASK
Definition: cLogAppender.h:54
LOGLEV_TYPE
Definition: cLogLevel.h:22
UINT32 LOG_ATTR_MASK_t
Definition: cLogAppender.h:56
int ITERATE_t
like size_t but signed
Definition: Index.h:28
int TIMESECD_t
signed delta seconds. like TIMESEC_t. redefined in TimeUnits.h.
Definition: cTimeSys.h:19
std::type_info TYPEINFO_t
Definition: cTypeInfo.h:29
char LOGCHAR_t
always just use UTF8 for logs, don't bother with UNICODE.
Definition: cLogLevel.h:17