]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/debugging.h
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / debugging.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // debugging - non-trivial debug support
21 //
22 #ifndef _H_DEBUGGING
23 #define _H_DEBUGGING
24
25 #include <Security/utilities.h>
26 #include <cstdarg>
27 #include <typeinfo>
28
29 namespace Security {
30 namespace Debug {
31
32
33 #if !defined(NDEBUG)
34
35
36 // Debug to standard target
37 void debug(const char *scope, const char *format, ...) __attribute__((format(printf,2,3)));
38 void vdebug(const char *scope, const char *format, va_list args);
39 bool debugging(const char *scope);
40
41 // Stream dumping to standard target
42 bool dumping(const char *scope);
43 void dump(const char *format, ...) __attribute((format(printf,1,2)));
44 void dumpData(const void *data, size_t length);
45 void dumpData(const char *title, const void *data, size_t length);
46 template <class Data> inline void dumpData(const Data &obj)
47 { dumpData(obj.data(), obj.length()); }
48 template <class Data> inline void dumpData(const char *title, const Data &obj)
49 { dumpData(title, obj.data(), obj.length()); }
50
51 #if defined(DEBUGDUMP)
52 # define IFDUMP(code) code
53 # define IFDUMPING(scope,code) if (Debug::dumping(scope)) code; else /* no */
54 #else
55 # define IFDUMP(code) /* no-op */
56 # define IFDUMPING(scope,code) /* no-op */
57 #endif
58
59
60 //
61 // A (prepared) debug scope object.
62 //
63 class Scope {
64 public:
65 Scope(const char *string) { mScope = string; }
66
67 void operator () (const char *format, ...);
68
69 private:
70 const char *mScope;
71 };
72
73
74 //
75 // Given an object of any type, produce the proper name of its type.
76 //
77 string makeTypeName(const type_info &info);
78
79 template <class Object>
80 string typeName(const Object &obj)
81 {
82 return makeTypeName(typeid(obj));
83 }
84
85
86 #else // NDEBUG
87
88
89 //
90 // If NDEBUG is defined, we try to make all debugging functions weightless
91 //
92
93 #if __GNUC__ > 2
94 inline void debug(const char *, const char *, ...) { }
95 #else
96 // @@@ Hack to work around the fact that gcc2 can't inline empty varargs functions.
97 extern "C" inline void debug() { }
98 #endif
99
100 inline void vdebug(const char *, const char *, va_list) { }
101 inline bool debugging(const char *) { return false; }
102
103 class Scope {
104 public:
105 Scope(const char *) { }
106
107 // @@@ Hack to work around the fact that gcc can't inline empty varargs functions.
108 //void operator () (const char *, ...) { }
109 void operator () (const char *, ...);
110 };
111
112 inline bool dumping(const char *) { return false; }
113
114 // @@@ Hack to work around the fact that gcc can't inline empty varargs functions.
115 //inline void dump(const char *, ...) { }
116 extern "C" inline void dump() { }
117
118 inline void dumpData(const void *, size_t) { }
119 void dumpData(const char *, const void *, size_t);
120 template <class Data> inline void dumpData(const Data &) { }
121 template <class Data> inline void dumpData(const char *, const Data &) { }
122
123 // debugdumping is forced off
124 #if defined(DEBUGDUMP)
125 # undef DEBUGDUMP
126 #endif
127 # define IFDUMP(code) /* no-op */
128 # define IFDUMPING(scope,code) /* no-op */
129
130 // no debug typeName; don't call this if NDEBUG
131
132 #endif // NDEBUG
133
134
135 } // end namespace Debug
136 } // end namespace Security
137
138 // We intentionally leak a few functions into the global namespace
139 using Security::Debug::debug;
140
141
142 #endif //_H_DEBUGGING