CACAO
Debug.hpp
Go to the documentation of this file.
1 /* src/toolbox/Debug.hpp - core debugging facilities
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 #ifndef DEBUG_HPP_
26 #define DEBUG_HPP_ 1
27 
28 #include "config.h"
29 #include <cstring>
30 #include <cassert>
31 #include "toolbox/Option.hpp"
32 
33 #ifdef ENABLE_LOGGING
34 
35 namespace cacao {
36 
37 struct Debug {
38  /** True if we should print a prefix
39  *
40  * Can be set using the -XX:+DebugPrefix command line flag
41  * @default false
42  */
43  static Option<bool> prefix_enabled;
44 
45  /** Verbosity level
46  *
47  * Higher number means more details.
48  * Can be set using the -XX:+DebugVerbose command line flag
49  * @default 0
50  */
51  static Option<unsigned int> verbose;
52 
53  /** True if we should print a the thread id
54  *
55  * Can be set using the -XX:+DebugPrintThread command line flag
56  * @default false
57  */
58  static Option<bool> thread_enabled;
59 
60  /** The name of system you are interested in debugging
61  *
62  * can be conviently be set via the command line flag -XX:DebugName
63  */
64  static Option<const char*> debugname;
65 
66  /**
67  * Debugging of a sub system is enabled if it's name is a valid prefix
68  * of the currently set system's name (as set via set_current_system)
69  */
70  static bool is_debugging_enabled(const char *system, size_t sz);
71 
72  inline static bool is_debugging_enabled(const char *system) {
73  return is_debugging_enabled(system, ::std::strlen(system));
74  }
75 };
76 
77 /**
78  * The debug condition.
79  *
80  * It is used by all DEBUG* and LOG* macros but it can also be used in code
81  * for marking a longer block which should be only executed in the debug context.
82  *
83  * @code
84  * if (DEBUG_COND_WITH_NAME_N) {
85  * ...
86  * }
87  * @endcode
88  */
89 #define DEBUG_COND_WITH_NAME_N(DBG_NAME,VERBOSE) \
90  ( (cacao::Debug::verbose >= (VERBOSE) ) && \
91  (cacao::Debug::is_debugging_enabled( (DBG_NAME) )) )
92 
93 /// This macro executes STMT iff debugging of sub system DBG_NAME is enabled
94 #define DEBUG_WITH_NAME_N(DBG_NAME, VERBOSE, STMT) \
95  do { \
96  if (DEBUG_COND_WITH_NAME_N( (DBG_NAME) , (VERBOSE) )) { \
97  STMT; \
98  } \
99  } while (0)
100 
101 } // end namespace cacao
102 
103 #else
104 
105 #define DEBUG_COND_WITH_NAME_N(DBG_NAME,VERBOSE) false
106 #define DEBUG_WITH_NAME_N(DBG_NAME, VERBOSE, STMT) do { } while(0)
107 
108 #endif // end ENABLE_LOGGING
109 
110 
111 #define DEBUG_COND_WITH_NAME(DBG_NAME) DEBUG_COND_WITH_NAME_N(DBG_NAME,0)
112 #define DEBUG_COND_N(VERBOSE) DEBUG_COND_WITH_NAME_N(DEBUG_NAME,VERBOSE)
113 #define DEBUG_COND DEBUG_COND_N(0)
114 
115 #define DEBUG_WITH_NAME(DBG_NAME, STMT) DEBUG_WITH_NAME_N(DBG_NAME,0, STMT)
116 #define DEBUG_N(VERBOSE,STMT) DEBUG_WITH_NAME_N(DEBUG_NAME,VERBOSE, STMT)
117 
118 /** Execute debug statements in your current module.
119  *
120  * To use this macro you must define the macro DEBUG_NAME to the
121  * name of your current module (should be a string literal.
122  * Never do this in a header (but if you do this, undef DEBUG_NAME
123  * at the end of the header)!
124  */
125 #define DEBUG(STMT) DEBUG_N(0, STMT)
126 #define DEBUG1(STMT) DEBUG_N(1, STMT)
127 #define DEBUG2(STMT) DEBUG_N(2, STMT)
128 #define DEBUG3(STMT) DEBUG_N(3, STMT)
129 
130 #endif // DEBUG_HPP_
131 
132 /*
133  * These are local overrides for various environment variables in Emacs.
134  * Please do not remove this and leave it at the end of the file, where
135  * Emacs will automagically detect them.
136  * ---------------------------------------------------------------------
137  * Local variables:
138  * mode: c++
139  * indent-tabs-mode: t
140  * c-basic-offset: 4
141  * tab-width: 4
142  * End:
143  * vim:noexpandtab:sw=4:ts=4:
144  */
This file contains the command line option parsing library.
bool verbose