Separatista
debug.h
1 /***************************************************************************
2 * Copyright (C) 2014 by Okkel Klaver *
3 * info@vanhetland.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 
21 #ifndef SEPARATISTA_DEBUG_H
22 #define SEPARATISTA_DEBUG_H
23 
24 #ifdef SEPARATISTADEBUG_BUILDINGDLL
25 # define SEPARATISTADEBUG_EXTERN __declspec(dllexport)
26 #elif defined SEPARATISTADEBUG_USINGDLL
27 # define SEPARATISTADEBUG_EXTERN __declspec(dllimport)
28 #else
29 # define SEPARATISTADEBUG_EXTERN
30 #endif
31 
32 #ifdef SEPARATISTA_DEBUG
33 
34 #include <windows.h>
35 #include <array>
36 #include <unordered_map>
37 #include <cstdlib>
38 
39 // First declare macro's
40 #define LOG(message) Separatista::Debug::DebugLogger::log(message, TEXT(__FILE__), __LINE__)
41 #define DEBUG_METHOD Separatista::Debug::DebugLogger __debugLogger(TEXT(__FUNCTION__), this)
42 #define DEBUG_STATIC_METHOD Separatista::Debug::DebugLogger __debugLogger(TEXT(__FUNCTION__))
43 
44 namespace Separatista
45 {
46  namespace Debug
47  {
51  class SEPARATISTADEBUG_EXTERN DebugLogger
52  {
53  public:
59  DebugLogger(const wchar_t *pMethodname, const void* pObject = NULL);
60 
64  ~DebugLogger();
65 
69  static void log(const wchar_t *pMessage, const wchar_t *pFilename, const unsigned int line);
70 
71  private:
72  static int g_indent;
73 
74  const wchar_t *m_pMessage;
75 
76  friend class MemDebug;
77  };
78 
79 #ifdef SEPARATISTA_DEBUG_NEW
80 
81  class SEPARATISTADEBUG_EXTERN MemDebug
82  {
83  public:
84  MemDebug()
85  {
86  std::wcscpy(m_TypeName, TEXT("Unknown"));
87  };
88 
89  MemDebug(const wchar_t *pFilename, int nLine) :
90  m_pFilename(pFilename),
91  m_nLine(nLine)
92  {
93  std::wcscpy(m_TypeName, TEXT("Unknown"));
94  };
95 
102  static void trackMemory(void *ptr, const MemDebug &memDebug, const char *pTypeName);
103 
107  static void releaseMemory(void *ptr);
108 
112  static bool init();
113 
117  static bool exit();
118  private:
119  wchar_t m_TypeName[100];
120  const wchar_t *m_pFilename;
121  int m_nLine;
122 
123  static std::unordered_map<const void *, MemDebug>* g_memMap;
124 
125  friend class DebugLogger;
126  };
127 
129  template <class T> inline T* operator *(const MemDebug &memDebug, T *p)
130  {
131  Separatista::Debug::MemDebug::trackMemory(p, memDebug, typeid(T).name());
132  return p;
133  }
134 
135 #endif //defined SEPARATISTA_DEBUG_NEW
136 
137  }
138 }
139 
140 #ifdef SEPARATISTA_DEBUG_NEW
141 
142 #ifdef _MSC_VER
143 #pragma warning (disable: 4595)
144 #endif
145 
146 // Prevent internal use of new from being trapped
147 #ifndef SEPARATISTADEBUG_BUILDINGDLL
148 
149 inline void* operator new(size_t size)
150 {
151  void *p = std::malloc(size);
152  if (p == NULL)
153  throw std::bad_alloc();
154 
155  return p;
156 }
157 
158 inline void* operator new[](size_t size)
159 {
160  void *p = std::malloc(size);
161  if (p == NULL)
162  throw std::bad_alloc();
163 
164  return p;
165 }
166 
167 inline void operator delete (void *ptr)
168 {
169  Separatista::Debug::MemDebug::releaseMemory(ptr);
170  std::free(ptr);
171 }
172 
173 inline void operator delete [](void *ptr)
174 {
175  Separatista::Debug::MemDebug::releaseMemory(ptr);
176  std::free(ptr);
177 }
178 
179 #define new Separatista::Debug::MemDebug(TEXT(__FILE__), __LINE__) * new
180 
181 #endif // !defined SEPARATISTADEBUG_BUILDINGDLL
182 #endif // !defined SEPARATISTA_DEBUG_NEW
183 
184 #else // !defined SEPARATISTA_DEBUG
185 
186 #define LOG(message)
187 #define DEBUG_METHOD
188 
189 #endif // defined SEPARATISTA_DEBUG
190 
191 #endif // !defined SEPARATISTA_DEBUG_H
Definition: mt940s.h:39