2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 // debugsupport - support interface for making and managing debugger objects.
22 // This header is not needed for logging debug messages.
24 #ifndef _H_DEBUGSUPPORT
25 #define _H_DEBUGSUPPORT
28 // Generate stub-code support if NDEBUG (but not CLEAN_NDEBUG) is set, to support
29 // client code that may have been generated with debug enabled. You don't actually
30 // get *real* debug logging, of course, just cheap dummy stubs to keep the linker happy.
32 #include <Security/debugging.h>
33 #include <Security/threading.h>
42 // Debug scope names - short strings with value semantics.
43 // We don't use STL strings because of overhead.
47 static const int maxLength
= 12;
50 { strncpy(mName
, s
, maxLength
-1); mName
[maxLength
-1] = '\0'; }
52 Name(const char *start
, const char *end
)
54 int length
= end
- start
; if (length
>= maxLength
) length
= maxLength
- 1;
55 memcpy(mName
, start
, length
); memset(mName
+ length
, 0, maxLength
- length
);
58 operator const char *() const { return mName
; }
60 bool operator < (const Name
&other
) const
61 { return memcmp(mName
, other
.mName
, maxLength
) < 0; }
63 bool operator == (const Name
&other
) const
64 { return memcmp(mName
, other
.mName
, maxLength
) == 0; }
67 char mName
[maxLength
]; // null terminated for easy printing
72 // A debugging Target. This is an object that receives debugging requests.
73 // You can have many, but one default one is always provided.
80 // get default (singleton) Target
83 void setFromEnvironment();
89 virtual void put(const char *buffer
, unsigned int length
) = 0;
90 virtual void dump(const char *buffer
);
91 virtual void configure(const char *argument
);
95 void to(const char *filename
);
96 void to(int syslogPriority
);
97 void to(FILE *openFile
);
99 void configure(); // from DEBUGOPTIONS
100 void configure(const char *options
); // from explicit string
103 void message(const char *scope
, const char *format
, va_list args
);
104 bool debugging(const char *scope
);
105 void dump(const char *format
, va_list args
);
106 bool dump(const char *scope
);
112 void operator = (const char *config
);
114 bool operator () (const char *name
) const;
117 bool useSet
; // use contents of enableSet
118 bool negate
; // negate meaning of enableSet
119 set
<Name
> enableSet
; // set of names
123 static const size_t messageConstructionSize
= 512; // size of construction buffer
125 Selector logSelector
; // selector for logging
126 Selector dumpSelector
; // selector for dumping
128 // output option state (from last configure call)
129 bool showScope
; // include scope in output lines
130 bool showThread
; // include #Threadid in output lines
131 bool showPid
; // include [Pid] in output lines
132 size_t dumpLimit
; // max. # of bytes dumped by dumpData & friends
134 // current output support
137 static terminate_handler previousTerminator
; // for chaining
138 static void terminator();
140 // the default Target
141 static Target
*singleton
;
146 // Standard Target::Sinks
148 class FileSink
: public Target::Sink
{
150 FileSink(FILE *f
) : file(f
), addDate(false), lockIO(true), lock(false) { }
151 void put(const char *, unsigned int);
152 void dump(const char *text
);
153 void configure(const char *);
162 class SyslogSink
: public Target::Sink
{
164 SyslogSink(int pri
) : priority(pri
), dumpBase(dumpBuffer
), dumpPtr(dumpBuffer
) { }
165 void put(const char *, unsigned int);
166 void dump(const char *text
);
167 void configure(const char *);
172 // a sliding buffer to hold partial line output
173 static const size_t dumpBufferSize
= 1024; // make this about 2 * maximum line length of dumps
174 char dumpBuffer
[dumpBufferSize
];
175 char *dumpBase
, *dumpPtr
;
179 } // end namespace Debug
180 } // end namespace Security
183 #endif //_H_DEBUGSUPPORT