-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.h
71 lines (61 loc) · 1.77 KB
/
debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Tim Mertogul
// tmertogu@ucsc.edu
// Michael Lowe
// mimlowe@ucsc.edu
// CMPS109
// Spring 2015
// ASG 2
// $Id: debug.h,v 1.1 2014-04-08 19:04:03-07 - - $
#ifndef __DEBUG_H__
#define __DEBUG_H__
#include <string>
#include <vector>
using namespace std;
//
// debug -
// static class for maintaining global debug flags, each indicated
// by a single character.
// setflags -
// Takes a string argument, and sets a flag for each char in the
// string. As a special case, '@', sets all flags.
// getflag -
// Used by the DEBUGF macro to check to see if a flag has been set.
// Not to be called by user code.
//
class debugflags {
private:
static vector<bool> flags;
public:
static void setflags (const string& optflags);
static bool getflag (char flag);
static void where (char flag, const char* file, int line,
const char* func);
};
//
// DEBUGF -
// Macro which expands into trace code. First argument is a
// trace flag char, second argument is output code that can
// be sandwiched between <<. Beware of operator precedence.
// Example:
// DEBUGF ('u', "foo = " << foo);
// will print two words and a newline if flag 'u' is on.
// Traces are preceded by filename, line number, and function.
//
#ifdef NDEBUG
#define DEBUGF(FLAG,CODE) ;
#define DEBUGS(FLAG,STMT) ;
#else
#define DEBUGF(FLAG,CODE) { \
if (debugflags::getflag (FLAG)) { \
debugflags::where (FLAG, __FILE__, __LINE__, __func__); \
cerr << CODE << endl; \
} \
}
#define DEBUGS(FLAG,STMT) { \
if (debugflags::getflag (FLAG)) { \
debugflags::where (FLAG, __FILE__, __LINE__, __func__); \
STMT; \
} \
}
#endif
#endif