/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/debug.hpp Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/debug.hpp Source File
Ratpac-two
debug.hpp
1 #ifndef DEBUG_HPP
2 #define DEBUG_HPP
3 /*------------------------------------------------------------------------------
4 
5  Author: Andy Rushton
6  Copyright: (c) Andy Rushton, 2004
7  License: BSD License, see ../docs/license.html
8 
9  Set of simple debug utilities, all of which are switched off by the
10  NDEBUG compiler directive
11 
12  ------------------------------------------------------------------------------*/
13 #include <assert.h>
14 #include <stdio.h>
15 
16 #include <string>
17 
18 #include "exceptions.hpp"
19 #include "os_fixes.hpp"
20 
22 // Exception thrown if an assertion fails
23 
24 class assert_failed : public std::logic_error {
25  public:
26  assert_failed(const char* file, int line, const char* function, const std::string& message) throw();
27  ~assert_failed(void) throw();
28 };
29 
31 // The macros used in debugging
32 
33 #ifndef NDEBUG
34 
35 #define DEBUG_TRACE _debug_trace _debug_trace_(__FILE__, __LINE__, __FUNCTION__)
36 #define IF_DEBUG(stmts) \
37  { \
38  if (_debug_trace_.debug()) { \
39  _debug_trace_.prefix(__LINE__); \
40  stmts; \
41  } \
42  }
43 #define DEBUG_REPORT(str) IF_DEBUG(_debug_trace_.report(__LINE__, str))
44 #define DEBUG_ERROR(str) _debug_trace_.error(__LINE__, str)
45 #define DEBUG_STACKDUMP(str) _debug_trace_.stackdump(__LINE__, str)
46 #define DEBUG_ON _debug_trace_.debug_on(__LINE__, true)
47 #define DEBUG_ON_LOCAL _debug_trace_.debug_on(__LINE__, false)
48 #define DEBUG_ON_GLOBAL _debug_global(__FILE__, __LINE__, __FUNCTION__, true)
49 #define DEBUG_OFF_GLOBAL _debug_global(__FILE__, __LINE__, __FUNCTION__, false)
50 #define DEBUG_OFF _debug_trace_.debug_off(__LINE__)
51 #define DEBUG_ASSERT(test) \
52  if (!(test)) _debug_assert_fail(__FILE__, __LINE__, __FUNCTION__, #test)
53 
54 #else
55 
56 #define DEBUG_TRACE
57 #define IF_DEBUG(stmts)
58 #define DEBUG_REPORT(str)
59 #define DEBUG_ERROR(str)
60 #define DEBUG_STACKDUMP(str)
61 #define DEBUG_ON
62 #define DEBUG_ON_LOCAL
63 #define DEBUG_ON_GLOBAL
64 #define DEBUG_OFF_GLOBAL
65 #define DEBUG_OFF
66 #define DEBUG_ASSERT(test)
67 
68 #endif
69 
71 // infrastructure - don't use directly
72 
73 void _debug_global(const char* file, int line, const char* function, bool state = true);
74 void _debug_assert_fail(const char* file, int line, const char* function, const char* test) throw();
75 
76 class _debug_trace {
77  public:
78  _debug_trace(const char* f, int l, const char* fn);
79  ~_debug_trace(void);
80  const char* file(void) const;
81  int line(void) const;
82  bool debug(void) const;
83  void debug_on(int l, bool recurse);
84  void debug_off(int l);
85  void prefix(int l) const;
86  void report(int l, const std::string& message) const;
87  void report(const std::string& message) const;
88  void error(int l, const std::string& message) const;
89  void error(const std::string& message) const;
90  void stackdump(int l, const std::string& message) const;
91  void stackdump(const std::string& message) const;
92  void stackdump(void) const;
93 
94  private:
95  const char* m_file;
96  int m_line;
97  const char* m_function;
98  unsigned m_depth;
99  const _debug_trace* m_last;
100  bool m_dbg;
101  bool m_old;
102  void do_report(int l, const std::string& message) const;
103  void do_report(const std::string& message) const;
104 
105  // make this class uncopyable
106  _debug_trace(const _debug_trace&);
107  _debug_trace& operator=(const _debug_trace&);
108 };
109 
111 #endif
Definition: debug.hpp:76
Definition: debug.hpp:24