]> Dogcows Code - chaz/yoink/blob - src/stlplus/portability/debug.hpp
cleanup stlplus files
[chaz/yoink] / src / stlplus / portability / debug.hpp
1 #ifndef STLPLUS_DEBUG
2 #define STLPLUS_DEBUG
3 ////////////////////////////////////////////////////////////////////////////////
4
5 // Author: Andy Rushton
6 // Copyright: (c) Southampton University 1999-2004
7 // (c) Andy Rushton 2004-2009
8 // License: BSD License, see ../docs/license.html
9
10 // Set of simple debug utilities, all of which are switched off by the
11 // NDEBUG compiler directive
12
13 ////////////////////////////////////////////////////////////////////////////////
14
15 #include "portability_fixes.hpp"
16 #include <stdexcept>
17 #include <string>
18
19 ////////////////////////////////////////////////////////////////////////////////
20 // Problem with missing __FUNCTION__ macro
21 ////////////////////////////////////////////////////////////////////////////////
22 // this macro is used in debugging but was missing in Visual Studio prior to version 7
23 // it also has a different name in Borland
24
25 #if defined(_MSC_VER) && (_MSC_VER < 1300)
26 #define __FUNCTION__ 0
27 #endif
28
29 #ifdef __BORLANDC__
30 #define __FUNCTION__ __FUNC__
31 #endif
32
33 ////////////////////////////////////////////////////////////////////////////////
34 // Exception thrown if an assertion fails
35
36 namespace stlplus
37 {
38
39 class assert_failed : public std::logic_error
40 {
41 public:
42 assert_failed(const char* file, int line, const char* function, const char* message) throw();
43 ~assert_failed(void) throw();
44 };
45
46 } // end namespace stlplus
47
48 ////////////////////////////////////////////////////////////////////////////////
49 // The macros used in debugging
50
51 #ifndef NDEBUG
52
53 #define DEBUG_TRACE stlplus::debug_trace stlplus_debug_trace(__FILE__,__LINE__,__FUNCTION__)
54 #define IF_DEBUG(stmts) {if (stlplus_debug_trace.debug()){stlplus_debug_trace.prefix(__LINE__);stmts;}}
55 #define DEBUG_REPORT(str) IF_DEBUG(stlplus_debug_trace.report(__LINE__,str))
56 #define DEBUG_ERROR(str) stlplus_debug_trace.error(__LINE__,str)
57 #define DEBUG_STACKDUMP(str) stlplus_debug_trace.stackdump(__LINE__,str)
58 #define DEBUG_ON stlplus_debug_trace.debug_on(__LINE__,true)
59 #define DEBUG_ON_LOCAL stlplus_debug_trace.debug_on(__LINE__,false)
60 #define DEBUG_ON_GLOBAL stlplus::debug_global(__FILE__,__LINE__,__FUNCTION__,true)
61 #define DEBUG_OFF_GLOBAL stlplus::debug_global(__FILE__,__LINE__,__FUNCTION__,false)
62 #define DEBUG_OFF stlplus_debug_trace.debug_off(__LINE__)
63 #define DEBUG_ASSERT(test) if (!(test))stlplus::debug_assert_fail(__FILE__,__LINE__,__FUNCTION__,#test)
64
65 #else
66
67 #define DEBUG_TRACE
68 #define IF_DEBUG(stmts)
69 #define DEBUG_REPORT(str)
70 #define DEBUG_ERROR(str)
71 #define DEBUG_STACKDUMP(str)
72 #define DEBUG_ON
73 #define DEBUG_ON_LOCAL
74 #define DEBUG_ON_GLOBAL
75 #define DEBUG_OFF_GLOBAL
76 #define DEBUG_OFF
77 #define DEBUG_ASSERT(test)
78
79 #endif
80
81 ////////////////////////////////////////////////////////////////////////////////
82 // infrastructure - don't use directly
83
84 namespace stlplus
85 {
86
87 void debug_global(const char* file, int line, const char* function, bool state = true);
88 void debug_assert_fail(const char* file, int line, const char* function, const char* test) throw(assert_failed);
89
90 class debug_trace
91 {
92 public:
93 debug_trace(const char* f, int l, const char* fn);
94 ~debug_trace(void);
95 const char* file(void) const;
96 int line(void) const;
97 bool debug(void) const;
98 void debug_on(int l, bool recurse);
99 void debug_off(int l);
100 void prefix(int l) const;
101 void report(int l, const std::string& message) const;
102 void report(const std::string& message) const;
103 void error(int l, const std::string& message) const;
104 void error(const std::string& message) const;
105 void stackdump(int l, const std::string& message) const;
106 void stackdump(const std::string& message) const;
107 void stackdump(void) const;
108
109 private:
110 const char* m_file;
111 int m_line;
112 const char* m_function;
113 unsigned m_depth;
114 const debug_trace* m_last;
115 bool m_dbg;
116 bool m_old;
117 void do_report(int l, const std::string& message) const;
118 void do_report(const std::string& message) const;
119
120 // make this class uncopyable
121 debug_trace(const debug_trace&);
122 debug_trace& operator = (const debug_trace&);
123 };
124
125 } // end namespace stlplus
126
127 ////////////////////////////////////////////////////////////////////////////////
128 #endif
This page took 0.03642 seconds and 4 git commands to generate.