1/* $OpenLDAP$ */
2/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1998-2011 The OpenLDAP Foundation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
9 * Public License.
10 *
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
14 */
15/* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
24 */
25
26#ifndef LDAP_LOG_H
27#define LDAP_LOG_H
28
29#include <stdio.h>
30#include <ldap_cdefs.h>
31
32LDAP_BEGIN_DECL
33
34/*
35 * debug reporting levels.
36 *
37 * They start with the syslog levels, and
38 * go down in importance.  The normal
39 * debugging levels begin with LDAP_LEVEL_ENTRY
40 *
41 */
42
43/*
44 * The "OLD_DEBUG" means that all logging occurs at LOG_DEBUG
45 */
46
47#ifdef OLD_DEBUG
48/* original behavior: all logging occurs at the same severity level */
49#if defined(LDAP_DEBUG) && defined(LDAP_SYSLOG)
50#define LDAP_LEVEL_EMERG	ldap_syslog_level
51#define LDAP_LEVEL_ALERT	ldap_syslog_level
52#define LDAP_LEVEL_CRIT		ldap_syslog_level
53#define LDAP_LEVEL_ERR		ldap_syslog_level
54#define LDAP_LEVEL_WARNING	ldap_syslog_level
55#define LDAP_LEVEL_NOTICE	ldap_syslog_level
56#define LDAP_LEVEL_INFO		ldap_syslog_level
57#define LDAP_LEVEL_DEBUG	ldap_syslog_level
58#else /* !LDAP_DEBUG || !LDAP_SYSLOG */
59#define LDAP_LEVEL_EMERG	(7)
60#define LDAP_LEVEL_ALERT	(7)
61#define LDAP_LEVEL_CRIT		(7)
62#define LDAP_LEVEL_ERR		(7)
63#define LDAP_LEVEL_WARNING	(7)
64#define LDAP_LEVEL_NOTICE	(7)
65#define LDAP_LEVEL_INFO		(7)
66#define LDAP_LEVEL_DEBUG	(7)
67#endif /* !LDAP_DEBUG || !LDAP_SYSLOG */
68
69#else /* ! OLD_DEBUG */
70/* map syslog onto LDAP severity levels */
71#ifdef LOG_DEBUG
72#define LDAP_LEVEL_EMERG	LOG_EMERG
73#define LDAP_LEVEL_ALERT	LOG_ALERT
74#define LDAP_LEVEL_CRIT		LOG_CRIT
75#define LDAP_LEVEL_ERR		LOG_ERR
76#define LDAP_LEVEL_WARNING	LOG_WARNING
77#define LDAP_LEVEL_NOTICE	LOG_NOTICE
78#define LDAP_LEVEL_INFO		LOG_INFO
79#define LDAP_LEVEL_DEBUG	LOG_DEBUG
80#else /* ! LOG_DEBUG */
81#define LDAP_LEVEL_EMERG	(0)
82#define LDAP_LEVEL_ALERT	(1)
83#define LDAP_LEVEL_CRIT		(2)
84#define LDAP_LEVEL_ERR		(3)
85#define LDAP_LEVEL_WARNING	(4)
86#define LDAP_LEVEL_NOTICE	(5)
87#define LDAP_LEVEL_INFO		(6)
88#define LDAP_LEVEL_DEBUG	(7)
89#endif /* ! LOG_DEBUG */
90#endif /* ! OLD_DEBUG */
91#if 0
92/* in case we need to reuse the unused bits of severity */
93#define	LDAP_LEVEL_MASK(s)	((s) & 0x7)
94#else
95#define	LDAP_LEVEL_MASK(s)	(s)
96#endif
97
98/* (yet) unused */
99#define LDAP_LEVEL_ENTRY	(0x08)	/* log function entry points */
100#define LDAP_LEVEL_ARGS		(0x10)	/* log function call parameters */
101#define LDAP_LEVEL_RESULTS	(0x20)	/* Log function results */
102#define LDAP_LEVEL_DETAIL1	(0x40)	/* log level 1 function operational details */
103#define LDAP_LEVEL_DETAIL2	(0x80)	/* Log level 2 function operational details */
104/* end of (yet) unused */
105
106/* original subsystem selection mechanism */
107#define LDAP_DEBUG_TRACE	0x0001
108#define LDAP_DEBUG_PACKETS	0x0002
109#define LDAP_DEBUG_ARGS		0x0004
110#define LDAP_DEBUG_CONNS	0x0008
111#define LDAP_DEBUG_BER		0x0010
112#define LDAP_DEBUG_FILTER	0x0020
113#define LDAP_DEBUG_CONFIG	0x0040
114#define LDAP_DEBUG_ACL		0x0080
115#define LDAP_DEBUG_STATS	0x0100
116#define LDAP_DEBUG_STATS2	0x0200
117#define LDAP_DEBUG_SHELL	0x0400
118#define LDAP_DEBUG_PARSE	0x0800
119#if 0 /* no longer used (nor supported) */
120#define LDAP_DEBUG_CACHE	0x1000
121#define LDAP_DEBUG_INDEX	0x2000
122#endif
123#ifdef __APPLE__
124#define LDAP_DEBUG_ASYNC        0x1000
125#endif
126#define LDAP_DEBUG_SYNC		0x4000
127
128#define LDAP_DEBUG_NONE		0x8000
129#define LDAP_DEBUG_ANY		(-1)
130
131/* debugging stuff */
132#ifdef LDAP_DEBUG
133    /*
134     * This is a bogus extern declaration for the compiler. No need to ensure
135     * a 'proper' dllimport.
136     */
137#ifndef ldap_debug
138extern int	ldap_debug;
139#endif /* !ldap_debug */
140
141#ifdef LDAP_SYSLOG
142extern int	ldap_syslog;
143extern int	ldap_syslog_level;
144
145#ifdef HAVE_EBCDIC
146#define syslog	eb_syslog
147extern void eb_syslog(int pri, const char *fmt, ...);
148#endif /* HAVE_EBCDIC */
149
150#endif /* LDAP_SYSLOG */
151
152/* this doesn't below as part of ldap.h */
153#ifdef LDAP_SYSLOG
154#define Log0( level, severity, fmt )	\
155	do { \
156		if ( ldap_debug & (level) ) \
157			lutil_debug( ldap_debug, (level), (fmt) ); \
158		if ( ldap_syslog & (level) ) \
159			syslog( LDAP_LEVEL_MASK((severity)), (fmt) ); \
160	} while ( 0 )
161#define Log1( level, severity, fmt, arg1 )	\
162	do { \
163		if ( ldap_debug & (level) ) \
164			lutil_debug( ldap_debug, (level), (fmt), (arg1) ); \
165		if ( ldap_syslog & (level) ) \
166			syslog( LDAP_LEVEL_MASK((severity)), (fmt), (arg1) ); \
167	} while ( 0 )
168#define Log2( level, severity, fmt, arg1, arg2 ) \
169	do { \
170		if ( ldap_debug & (level) ) \
171			lutil_debug( ldap_debug, (level), (fmt), (arg1), (arg2) ); \
172		if ( ldap_syslog & (level) ) \
173			syslog( LDAP_LEVEL_MASK((severity)), (fmt), (arg1), (arg2) ); \
174	} while ( 0 )
175#define Log3( level, severity, fmt, arg1, arg2, arg3 ) \
176	do { \
177		if ( ldap_debug & (level) ) \
178			lutil_debug( ldap_debug, (level), (fmt), (arg1), (arg2), (arg3) ); \
179		if ( ldap_syslog & (level) ) \
180			syslog( LDAP_LEVEL_MASK((severity)), (fmt), (arg1), (arg2), (arg3) ); \
181	} while ( 0 )
182#define Log4( level, severity, fmt, arg1, arg2, arg3, arg4 ) \
183	do { \
184		if ( ldap_debug & (level) ) \
185			lutil_debug( ldap_debug, (level), (fmt), (arg1), (arg2), (arg3), (arg4) ); \
186		if ( ldap_syslog & (level) ) \
187			syslog( LDAP_LEVEL_MASK((severity)), (fmt), (arg1), (arg2), (arg3), (arg4) ); \
188	} while ( 0 )
189#define Log5( level, severity, fmt, arg1, arg2, arg3, arg4, arg5 ) \
190	do { \
191		if ( ldap_debug & (level) ) \
192			lutil_debug( ldap_debug, (level), (fmt), (arg1), (arg2), (arg3), (arg4), (arg5) ); \
193		if ( ldap_syslog & (level) ) \
194			syslog( LDAP_LEVEL_MASK((severity)), (fmt), (arg1), (arg2), (arg3), (arg4), (arg5) ); \
195	} while ( 0 )
196#define Debug( level, fmt, arg1, arg2, arg3 )	\
197	Log3( (level), ldap_syslog_level, (fmt), (arg1), (arg2), (arg3) )
198#define LogTest(level) ( ( ldap_debug | ldap_syslog ) & (level) )
199
200#else /* ! LDAP_SYSLOG */
201#define Log0( level, severity, fmt ) \
202	do { \
203		if ( ldap_debug & (level) ) \
204	    		lutil_debug( ldap_debug, (level), (fmt) ); \
205	} while ( 0 )
206#define Log1( level, severity, fmt, arg1 ) \
207	do { \
208		if ( ldap_debug & (level) ) \
209	    		lutil_debug( ldap_debug, (level), (fmt), (arg1) ); \
210	} while ( 0 )
211#define Log2( level, severity, fmt, arg1, arg2 ) \
212	do { \
213		if ( ldap_debug & (level) ) \
214	    		lutil_debug( ldap_debug, (level), (fmt), (arg1), (arg2) ); \
215	} while ( 0 )
216#define Log3( level, severity, fmt, arg1, arg2, arg3 ) \
217	do { \
218		if ( ldap_debug & (level) ) \
219	    		lutil_debug( ldap_debug, (level), (fmt), (arg1), (arg2), (arg3) ); \
220	} while ( 0 )
221#define Log4( level, severity, fmt, arg1, arg2, arg3, arg4 ) \
222	do { \
223		if ( ldap_debug & (level) ) \
224	    		lutil_debug( ldap_debug, (level), (fmt), (arg1), (arg2), (arg3), (arg4) ); \
225	} while ( 0 )
226#define Log5( level, severity, fmt, arg1, arg2, arg3, arg4, arg5 ) \
227	do { \
228		if ( ldap_debug & (level) ) \
229	    		lutil_debug( ldap_debug, (level), (fmt), (arg1), (arg2), (arg3), (arg4), (arg5) ); \
230	} while ( 0 )
231#define Debug( level, fmt, arg1, arg2, arg3 ) \
232		Log3( (level), 0, (fmt), (arg1), (arg2), (arg3) )
233#define LogTest(level) ( ldap_debug & (level) )
234#endif /* ! LDAP_SYSLOG */
235#else /* ! LDAP_DEBUG */
236/* TODO: in case LDAP_DEBUG is undefined, make sure logs with appropriate
237 * severity gets thru anyway */
238#define Log0( level, severity, fmt ) ((void)0)
239#define Log1( level, severity, fmt, arg1 ) ((void)0)
240#define Log2( level, severity, fmt, arg1, arg2 ) ((void)0)
241#define Log3( level, severity, fmt, arg1, arg2, arg3 ) ((void)0)
242#define Log4( level, severity, fmt, arg1, arg2, arg3, arg4 ) ((void)0)
243#define Log5( level, severity, fmt, arg1, arg2, arg3, arg4, arg5 ) ((void)0)
244#define Debug( level, fmt, arg1, arg2, arg3 ) ((void)0)
245#define LogTest(level) ( 0 )
246#endif /* ! LDAP_DEBUG */
247
248/* Actually now in liblber/debug.c */
249LDAP_LUTIL_F(int) lutil_debug_file LDAP_P(( FILE *file ));
250
251LDAP_LUTIL_F(void) lutil_debug LDAP_P((
252	int debug, int level,
253	const char* fmt, ... )) LDAP_GCCATTR((format(printf, 3, 4)));
254
255#ifdef LDAP_DEFINE_LDAP_DEBUG
256/* This struct matches the head of ldapoptions in <ldap-int.h> */
257struct ldapoptions_prefix {
258	short	ldo_valid;
259	int		ldo_debug;
260};
261#define ldap_debug \
262	(*(int *) ((char *)&ldap_int_global_options \
263		 + offsetof(struct ldapoptions_prefix, ldo_debug)))
264
265struct ldapoptions;
266LDAP_V ( struct ldapoptions ) ldap_int_global_options;
267#endif /* LDAP_DEFINE_LDAP_DEBUG */
268
269LDAP_END_DECL
270
271#endif /* LDAP_LOG_H */
272