|
|
|
|
|
|
|
In large, complex projects, you may want more control than simply turning DEBUG on and off. You can define debug levels, and test for these levels when deciding which macros to use and which to strip out. |
|
|
|
|
|
|
|
|
To define a level, simply follow the #define DEBUG statement with a number. While you can have any number of levels, a common system is to have four levels: HIGH, MEDIUM, LOW, and NONE. Listing 21.6 illustrates how this might be done, using the String and Animal classes from Listing 21.4. The definitions of the class methods other than Invariants() have been left out to save space, because they are unchanged from Listing 21.4. |
|
|
|
|
|
|
|
|
To compile this code, copy lines 40132 of Listing 21.4 between lines 64 and 65 of this listing. |
|
|
|
| | Also, please insert lines 169 to 174 (Animal::Animal(int age, const String& name) definition) of listing 21.4 before line 108 in this listing. |
|
|
|
|
|
LISTING 21.6 LEVELS OF DEBUGGING |
|
|
|
 |
|
|
|
|
1: enum DEBUGLEVEL {NONE, LOW, MEDIUM, HIGH };
2:
3: #define DEBUGLEVEL HIGH
4:
5: #include <iostream.h>
6: #include <string.h>
7:
8: #if DEBUGLEVEL <= LOW // must be medium or high
9: #define ASSERT(x)
10: #else
11: #define ASSERT(x) \
12: if (! (x)) \
13: { \
14: cout << ERROR!! Assert << #x << failed\n; \
15: cout << on line << __LINE__ << \n; \
16: cout << in file << __FILE__ << \n; \
17: }
18: #endif
19:
20: #if DEBUGLEVEL < MEDIUM
21: #define EVAL(x)
22: #else
23: #define EVAL(x)
24: cout << #x << :\t << x << end1;
25: #endif |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|