ug4
debug_id.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__COMMON__DEBUG_ID_H
34 #define __H__UG__COMMON__DEBUG_ID_H
35 #include <vector>
36 #include <map>
37 #include <string>
38 #include "common/util/crc32.h"
39 #include <sstream>
40 
41 namespace ug{
42 
45 
46 class DebugIDManager;
47 
48 
49 
50 // DebugID
93 class DebugID
94 {
95 public:
97  DebugID(const char *str);
98 
99 
100  DebugID(uint32 hash) { m_hash = hash; }
102  inline int get_debug_level() const;
103 
105  inline bool set_debug_level(int level);
106 
107  friend class DebugIDManager;
108 private:
110 };
111 
112 // the list of ug4's standard DebugIDs.
113 extern DebugID MAIN,
133 
134 
140 {
141 private:
143  inline int get_debug_level(uint32 debugIDhash) const
144  {
145  std::map<uint32, int>::const_iterator it = m_dbgLevels.find(debugIDhash);
146  if(it == m_dbgLevels.end()) return -1;
147  else return (*it).second;
148  }
149 
151  inline bool set_debug_level(uint32 debugIDhash, int level)
152  {
153  std::map<uint32, int>::iterator it = m_dbgLevels.find(debugIDhash);
154  if(it == m_dbgLevels.end()) return false;
155  else
156  {
157  (*it).second = level;
158  return true;
159  }
160  }
161 public:
163  static DebugIDManager& instance();
164 
166  inline bool debug_id_registered(const char *debugIDname) const
167  {
168  return m_dbgLevels.find(crc32(debugIDname)) != m_dbgLevels.end();
169  }
171  inline int get_debug_level(const DebugID &debugID) const
172  {
173  return get_debug_level(debugID.m_hash);
174  }
176  inline int get_debug_level(const char *debugID) const
177  {
178  return get_debug_level(crc32(debugID));
179  }
180 
182  inline DebugID get_debug_id(const char *debugID) const
183  {
184  return DebugID(crc32(debugID));
185  }
186 
187  inline DebugID get_or_create_debug_id(const char *debugID)
188  {
189  if(debug_id_registered(debugID)==false)
190  register_debug_id(debugID);
191  return DebugID(crc32(debugID));
192  }
193 
194 
196  inline bool set_debug_level(DebugID &debugID, int level)
197  {
198  return set_debug_level(debugID.m_hash, level);
199  }
201  bool set_debug_level(const char *debugID, int level);
202 
203 
205  bool set_debug_levels(int lev);
206 
208  std::string get_registered_debug_IDs() const
209  {
210  std::stringstream str;
211  str << "DebugIDs:\n";
212  for(size_t i=0; i<m_dbgLevelIdentifiers.size(); i++)
213  str << m_dbgLevelIdentifiers[i] << " : Level " << get_debug_level(m_dbgLevelIdentifiers[i].c_str()) << "\n";
214  return str.str();
215  }
216 
217  const std::vector<std::string> &get_registered_debug_IDs_arr() const
218  {
219  return m_dbgLevelIdentifiers;
220  }
221 
222 
223 
224  bool register_debug_id(const char *debugID);
225 
226 private:
227  std::vector<std::string> m_dbgLevelIdentifiers;
228  std::map<uint32, int> m_dbgLevels;
229 };
230 
232 {
233  return DebugIDManager::instance();
234 }
235 
236 inline int DebugID::get_debug_level() const
237 {
238  return GetDebugIDManager().get_debug_level(*this);
239 }
240 inline bool DebugID::set_debug_level(int level)
241 {
242  return GetDebugIDManager().set_debug_level(*this, level);
243 }
244 
245 // TemporaryDebugLevel
247 
272 #ifdef UG_ENABLE_DEBUG_LOGS
273 class TemporaryDebugLevel
274 {
275  DebugID &did;
276 
277  int m_prevDebugLevel;
278 public:
279  TemporaryDebugLevel(DebugID &debugID) : did(debugID)
280  {
281  m_prevDebugLevel = did.get_debug_level();
282  }
283 
285  TemporaryDebugLevel(DebugID &debugID, int temporaryDebugLevel) : did(debugID)
286  {
287  m_prevDebugLevel = did.get_debug_level();
288  did.set_debug_level(temporaryDebugLevel);
289  }
290 
292  TemporaryDebugLevel(DebugID &debugID, DebugID &debugSrc, int diff=0) : did(debugID)
293  {
294  m_prevDebugLevel = did.get_debug_level();
295  did.set_debug_level(debugSrc.get_debug_level()+diff);
296  }
297 
299  ~TemporaryDebugLevel()
300  {
301  did.set_debug_level(m_prevDebugLevel);
302  }
303 
304  void set(int temporaryDebugLevel)
305  {
306  did.set_debug_level(temporaryDebugLevel);
307  }
308 };
309 #else
310 class TemporaryDebugLevel
311 {
312 
313 public:
314  TemporaryDebugLevel(DebugID &debugID){}
316  TemporaryDebugLevel(DebugID &debugID, int temporaryDebugLevel){}
318  TemporaryDebugLevel(DebugID &debugID, DebugID &debugSrc, int diff=0){}
320  ~TemporaryDebugLevel(){}
321  void set(int temporaryDebugLevel){}
322 };
323 #endif
324 
325 // end group ugbase_common
327 
328 } // end namespace ug
329 #endif /* __H__UG__COMMON__DEBUG_ID_H */
330 
Definition: debug_id.h:94
Definition: debug_id.h:140
uint32 crc32(const char *str)
Calculates the crc32 for a null-terminated string.
Definition: crc32.cpp:39
DebugID LIB_DISC
Definition: debug_id.h:117
int get_debug_level() const
returns the debug level via GetDebugIDManager.
Definition: debug_id.h:236
DebugID LIB_GRID_REFINER
Definition: debug_id.h:116
DebugID LIB_DISC_NEWTON
Definition: debug_id.h:121
DebugID LIB_DISC_TRANSFER
Definition: debug_id.h:123
DebugID get_debug_id(const char *debugID) const
Definition: debug_id.h:182
bool set_debug_level(uint32 debugIDhash, int level)
sets the debug level of debugIDhash if registered to 'level'
Definition: debug_id.h:151
int get_debug_level(const char *debugID) const
returns the debug level of debugID, -1 if not found
Definition: debug_id.h:176
DebugID LIB_DISC_LINKER
Definition: debug_id.h:122
ugtypes::uint32_t uint32
Definition: types.h:116
bool set_debug_level(DebugID &debugID, int level)
sets the debug level of debugID if registered to 'level'
Definition: debug_id.h:196
DebugID LIB_ALG_LINEAR_SOLVER
Definition: debug_id.h:128
DebugID(const char *str)
registers the DebugID at DebugIDManager
Definition: debug_id.cpp:50
DebugID get_or_create_debug_id(const char *debugID)
Definition: debug_id.h:187
DebugID APP
Definition: debug_id.h:114
bool register_debug_id(const char *debugID)
Definition: debug_id.cpp:103
static DebugIDManager & instance()
returns a reference to the single instance of LogAssistant
Definition: debug_id.cpp:56
DebugID LIB_DISC_OPERATOR_INVERSE
Definition: debug_id.h:126
int get_debug_level(const DebugID &debugID) const
returns the debug level of debugID, -1 if not found
Definition: debug_id.h:171
DebugID LIB_ALG_VECTOR
Definition: debug_id.h:129
bool debug_id_registered(const char *debugIDname) const
returns true if there is a DebugID with name debugIDname
Definition: debug_id.h:166
int get_debug_level(uint32 debugIDhash) const
returns the debug level of debugIDhash, -1 if not found
Definition: debug_id.h:143
bool set_debug_level(int level)
sets the debug level via GetDebugIDManager.
Definition: debug_id.h:240
bool set_debug_levels(int lev)
sets the debug level of all registered debugIDs to 'lev'
Definition: debug_id.cpp:64
std::vector< std::string > m_dbgLevelIdentifiers
Definition: debug_id.h:227
std::map< uint32, int > m_dbgLevels
Definition: debug_id.h:228
DebugID LIB_DISC_ASSEMBLE
Definition: debug_id.h:118
DebugID LIB_ALG_LINEAR_OPERATOR
Definition: debug_id.h:127
DebugID LIB_ALG_MATRIX
Definition: debug_id.h:130
uint32 m_hash
Definition: debug_id.h:109
std::string get_registered_debug_IDs() const
returns a string describing all registered debugIDs.
Definition: debug_id.h:208
DebugID LIB_DISC_OUTPUT
Definition: debug_id.h:125
DebugID MAIN
DebugID LIB_ALG_AMG
Definition: debug_id.h:131
DebugID LIB_DISC_D3F
Definition: debug_id.h:119
DebugID(uint32 hash)
Definition: debug_id.h:100
DebugID LIB_DISC_DISCRETE_FUNCTION
Definition: debug_id.h:124
DebugID LIB_GRID
Definition: debug_id.h:115
DebugIDManager & GetDebugIDManager()
Definition: debug_id.h:231
DebugID LIB_PCL
Definition: debug_id.h:132
DebugID LIB_DISC_MULTIGRID
Definition: debug_id.h:120
const std::vector< std::string > & get_registered_debug_IDs_arr() const
Definition: debug_id.h:217
the ug namespace