Code::Blocks  SVN r11506
cclogger.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  */
5 
6 #ifndef CCLOGGER_H
7 #define CCLOGGER_H
8 
9 #include <wx/string.h>
10 #include <wx/thread.h>
11 
12 #include <memory> // unique_ptr
13 
14 #include <cbexception.h> // cbAssert
15 #include <logmanager.h> // F()
16 #include <prep.h> // nullptr
17 
18 class wxEvtHandler;
19 
20 #ifndef CC_PROCESS_LOG_EVENT_TO_PARENT
21  #define CC_PROCESS_LOG_EVENT_TO_PARENT 0
22 #endif
23 
24 #ifdef CC_PARSER_TEST
25  #undef CC_PROCESS_LOG_EVENT_TO_PARENT
26  #define CC_PROCESS_LOG_EVENT_TO_PARENT 1
27 #endif
28 
29 extern bool g_EnableDebugTrace;
30 extern const wxString g_DebugTraceFile;
31 extern long g_idCCAddToken;
32 extern long g_idCCLogger;
33 extern long g_idCCDebugLogger;
34 
35 class CCLogger
36 {
37 public:
38  static CCLogger* Get();
39 
40  void Init(wxEvtHandler* parent, int logId, int debugLogId, int addTokenId = -1);
41  void AddToken(const wxString& msg);
42  void Log(const wxString& msg);
43  void DebugLog(const wxString& msg);
44 
45 protected:
46  CCLogger();
47  virtual ~CCLogger() { ; }
48  CCLogger(const CCLogger&) { ; }
49  CCLogger& operator=(const CCLogger&) { return *this; }
50 
51  // static member variables (instance and critical section for Parser)
52  friend class std::default_delete<CCLogger>;
53  static std::unique_ptr<CCLogger> s_Inst;
54 
55 private:
57  int m_LogId;
60 };
61 
62 // For tracking, either uncomment:
63 //#define CC_ENABLE_LOCKER_TRACK
64 // ...or:
65 #define CC_ENABLE_LOCKER_ASSERT
66 // ..or none of the above.
67 
68 #if defined(CC_ENABLE_LOCKER_TRACK)
69  // TRACKING MUTXES
70  // [1] Implementations for tracking mutexes:
71  #define THREAD_LOCKER_MTX_LOCK(NAME) \
72  CCLogger::Get()->DebugLog(F(_T("%s.Lock() : %s(), %s, %d"), \
73  wxString(#NAME, wxConvUTF8).wx_str(), \
74  wxString(__FUNCTION__, wxConvUTF8).wx_str(), \
75  wxString(__FILE__, wxConvUTF8).wx_str(), \
76  __LINE__))
77  #define THREAD_LOCKER_MTX_LOCK_SUCCESS(NAME) \
78  CCLogger::Get()->DebugLog(F(_T("%s.Lock().Success() : %s(), %s, %d"), \
79  wxString(#NAME, wxConvUTF8).wx_str(), \
80  wxString(__FUNCTION__, wxConvUTF8).wx_str(), \
81  wxString(__FILE__, wxConvUTF8).wx_str(), \
82  __LINE__))
83  #define THREAD_LOCKER_MTX_UNLOCK(NAME) \
84  CCLogger::Get()->DebugLog(F(_T("%s.Unlock() : %s(), %s, %d"), \
85  wxString(#NAME, wxConvUTF8).wx_str(), \
86  wxString(__FUNCTION__, wxConvUTF8).wx_str(), \
87  wxString(__FILE__, wxConvUTF8).wx_str(), \
88  __LINE__))
89  #define THREAD_LOCKER_MTX_UNLOCK_SUCCESS(NAME) \
90  CCLogger::Get()->DebugLog(F(_T("%s.Unlock().Success() : %s(), %s, %d"), \
91  wxString(#NAME, wxConvUTF8).wx_str(), \
92  wxString(__FUNCTION__, wxConvUTF8).wx_str(), \
93  wxString(__FILE__, wxConvUTF8).wx_str(), \
94  __LINE__))
95  #define THREAD_LOCKER_MTX_FAIL(NAME) \
96  CCLogger::Get()->DebugLog(F(_T("%s.Fail() : %s(), %s, %d"), \
97  wxString(#NAME, wxConvUTF8).wx_str(), \
98  wxString(__FUNCTION__, wxConvUTF8).wx_str(), \
99  wxString(__FILE__, wxConvUTF8).wx_str(), \
100  __LINE__))
101  // [2] Cumulative convenient macros for tracking mutexes [USE THESE!]:
102  #define CC_LOCKER_TRACK_TT_MTX_LOCK(M) \
103  { \
104  THREAD_LOCKER_MTX_LOCK(M); \
105  if (M.Lock()==wxMUTEX_NO_ERROR) \
106  THREAD_LOCKER_MTX_LOCK_SUCCESS(M); \
107  else \
108  THREAD_LOCKER_MTX_FAIL(M); \
109  }
110  #define CC_LOCKER_TRACK_TT_MTX_UNLOCK(M) \
111  { \
112  THREAD_LOCKER_MTX_UNLOCK(M); \
113  if (M.Unlock()==wxMUTEX_NO_ERROR) \
114  THREAD_LOCKER_MTX_UNLOCK_SUCCESS(M); \
115  else \
116  THREAD_LOCKER_MTX_FAIL(M); \
117  }
118  #define CC_LOCKER_TRACK_CBBT_MTX_LOCK CC_LOCKER_TRACK_TT_MTX_LOCK
119  #define CC_LOCKER_TRACK_CBBT_MTX_UNLOCK CC_LOCKER_TRACK_TT_MTX_UNLOCK
120  #define CC_LOCKER_TRACK_P_MTX_LOCK CC_LOCKER_TRACK_TT_MTX_LOCK
121  #define CC_LOCKER_TRACK_P_MTX_UNLOCK CC_LOCKER_TRACK_TT_MTX_UNLOCK
122 
123  // TRACKING CRITICAL SECIONS
124  // [2] Implementations for tracking critical sections:
125  #define THREAD_LOCKER_CS_ENTER(NAME) \
126  CCLogger::Get()->DebugLog(F(_T("%s.Enter() : %s(), %s, %d"), \
127  wxString(#NAME, wxConvUTF8).wx_str(), \
128  wxString(__FUNCTION__, wxConvUTF8).wx_str(), \
129  wxString(__FILE__, wxConvUTF8).wx_str(), \
130  __LINE__))
131  #define THREAD_LOCKER_CS_ENTERED(NAME) \
132  CCLogger::Get()->DebugLog(F(_T("%s.Entered() : %s(), %s, %d"), \
133  wxString(#NAME, wxConvUTF8).wx_str(), \
134  wxString(__FUNCTION__, wxConvUTF8).wx_str(), \
135  wxString(__FILE__, wxConvUTF8).wx_str(), \
136  __LINE__))
137  #define THREAD_LOCKER_CS_LEAVE(NAME) \
138  CCLogger::Get()->DebugLog(F(_T("%s.Leave() : %s(), %s, %d"), \
139  wxString(#NAME, wxConvUTF8).wx_str(), \
140  wxString(__FUNCTION__, wxConvUTF8).wx_str(), \
141  wxString(__FILE__, wxConvUTF8).wx_str(), \
142  __LINE__))
143  // [2] Cumulative convenient macros for tracking critical sections [USE THESE!]:
144  #define CC_LOCKER_TRACK_CS_ENTER(CS) \
145  { \
146  THREAD_LOCKER_CS_ENTER(CS); \
147  CS.Enter(); \
148  THREAD_LOCKER_CS_ENTERED(CS); \
149  }
150  #define CC_LOCKER_TRACK_CS_LEAVE(CS) \
151  { \
152  THREAD_LOCKER_CS_LEAVE(CS); \
153  CS.Leave(); \
154  }
155 #elif defined CC_ENABLE_LOCKER_ASSERT
156  #define CC_LOCKER_TRACK_CS_ENTER(CS) CS.Enter();
157  #define CC_LOCKER_TRACK_CS_LEAVE(CS) CS.Leave();
158 
159  #define CC_LOCKER_TRACK_TT_MTX_LOCK(M) \
160  do { \
161  auto result = M.Lock(); \
162  cbAssert(result==wxMUTEX_NO_ERROR); \
163  } while (false);
164 
165  #define CC_LOCKER_TRACK_TT_MTX_UNLOCK(M) \
166  do { \
167  auto result = M.Unlock(); \
168  cbAssert(result==wxMUTEX_NO_ERROR); \
169  } while (false);
170 
171  #define CC_LOCKER_TRACK_CBBT_MTX_LOCK CC_LOCKER_TRACK_TT_MTX_LOCK
172  #define CC_LOCKER_TRACK_CBBT_MTX_UNLOCK CC_LOCKER_TRACK_TT_MTX_UNLOCK
173  #define CC_LOCKER_TRACK_P_MTX_LOCK CC_LOCKER_TRACK_TT_MTX_LOCK
174  #define CC_LOCKER_TRACK_P_MTX_UNLOCK CC_LOCKER_TRACK_TT_MTX_UNLOCK
175 #else
176  #define CC_LOCKER_TRACK_CS_ENTER(CS) CS.Enter();
177  #define CC_LOCKER_TRACK_CS_LEAVE(CS) CS.Leave();
178 
179  #define CC_LOCKER_TRACK_TT_MTX_LOCK(M) M.Lock();
180  #define CC_LOCKER_TRACK_TT_MTX_UNLOCK(M) M.Unlock();
181  #define CC_LOCKER_TRACK_CBBT_MTX_LOCK CC_LOCKER_TRACK_TT_MTX_LOCK
182  #define CC_LOCKER_TRACK_CBBT_MTX_UNLOCK CC_LOCKER_TRACK_TT_MTX_UNLOCK
183  #define CC_LOCKER_TRACK_P_MTX_LOCK CC_LOCKER_TRACK_TT_MTX_LOCK
184  #define CC_LOCKER_TRACK_P_MTX_UNLOCK CC_LOCKER_TRACK_TT_MTX_UNLOCK
185 #endif
186 
187 #endif // CCLOGGER_H
long g_idCCLogger
Definition: cclogger.cpp:25
const wxString g_DebugTraceFile
Trace file name (if above is enabled).
Definition: cclogger.cpp:23
static CCLogger * Get()
Definition: cclogger.cpp:60
void Log(const wxString &msg)
Definition: cclogger.cpp:90
static std::unique_ptr< CCLogger > s_Inst
Definition: cclogger.h:53
CCLogger(const CCLogger &)
Definition: cclogger.h:48
void AddToken(const wxString &msg)
Definition: cclogger.cpp:77
int m_DebugLogId
Definition: cclogger.h:58
long g_idCCDebugLogger
Definition: cclogger.cpp:26
void DebugLog(const wxString &msg)
Definition: cclogger.cpp:107
CCLogger & operator=(const CCLogger &)
Definition: cclogger.h:49
long g_idCCAddToken
Definition: cclogger.cpp:24
wxEvtHandler * m_Parent
Definition: cclogger.h:56
bool g_EnableDebugTrace
Toggles tracing into file.
Definition: cclogger.cpp:21
int m_AddTokenId
Definition: cclogger.h:59
CCLogger()
Definition: cclogger.cpp:52
void Init(wxEvtHandler *parent, int logId, int debugLogId, int addTokenId=-1)
Definition: cclogger.cpp:69
int m_LogId
Definition: cclogger.h:57
virtual ~CCLogger()
Definition: cclogger.h:47