1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2021 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 
32 LDAP_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 #define LDAP_DEBUG_SYNC		0x4000
124 
125 #define LDAP_DEBUG_NONE		0x8000
126 #define LDAP_DEBUG_ANY		(-1)
127 
128 /* debugging stuff */
129 #ifdef LDAP_DEBUG
130     /*
131      * This is a bogus extern declaration for the compiler. No need to ensure
132      * a 'proper' dllimport.
133      */
134 #ifndef ldap_debug
135 extern int	ldap_debug;
136 #endif /* !ldap_debug */
137 
138 #ifdef LDAP_SYSLOG
139 extern int	ldap_syslog;
140 extern int	ldap_syslog_level;
141 
142 #ifdef HAVE_EBCDIC
143 #define syslog	eb_syslog
144 extern void eb_syslog(int pri, const char *fmt, ...);
145 #endif /* HAVE_EBCDIC */
146 
147 #endif /* LDAP_SYSLOG */
148 #endif /* LDAP_DEBUG */
149 
150 /* we keep libldap working with preprocessors that can't do variadic macros */
151 #ifndef LDAP_INT_DEBUG
152 /* this doesn't below as part of ldap.h */
153 #ifdef LDAP_DEBUG
154 #ifdef LDAP_SYSLOG
155 
156 #define LogTest(level) ( ( ldap_debug | ldap_syslog ) & (level) )
157 #define Log(level, severity, ...) \
158 	do { \
159 		if ( ldap_debug & (level) ) \
160 			lutil_debug( ldap_debug, (level), __VA_ARGS__ ); \
161 		if ( ldap_syslog & (level) ) \
162 			syslog( LDAP_LEVEL_MASK((severity)), __VA_ARGS__ ); \
163 	} while ( 0 )
164 
165 #else /* ! LDAP_SYSLOG */
166 
167 #define LogTest(level) ( ldap_debug & (level) )
168 #define Log(level, severity, ...) \
169 	do { \
170 		if ( ldap_debug & (level) ) \
171 			lutil_debug( ldap_debug, (level), __VA_ARGS__ ); \
172 	} while ( 0 )
173 
174 #endif /* ! LDAP_SYSLOG */
175 #else /* ! LDAP_DEBUG */
176 
177 /* TODO: in case LDAP_DEBUG is undefined, make sure logs with appropriate
178  * severity gets thru anyway */
179 #define LogTest(level) ( 0 )
180 #define Log(level, severity, ...) ((void) 0)
181 
182 #endif /* ! LDAP_DEBUG */
183 
184 #define Debug(level, ...) \
185 	Log((level), ldap_syslog_level, __VA_ARGS__ )
186 #endif /* ! LDAP_INT_DEBUG */
187 
188 /* Actually now in liblber/debug.c */
189 LDAP_LUTIL_F(int) lutil_debug_file LDAP_P(( FILE *file ));
190 
191 LDAP_LUTIL_F(void) lutil_debug LDAP_P((
192 	int debug, int level,
193 	const char* fmt, ... )) LDAP_GCCATTR((format(printf, 3, 4)));
194 
195 #ifdef LDAP_DEFINE_LDAP_DEBUG
196 /* This struct matches the head of ldapoptions in <ldap-int.h> */
197 struct ldapoptions_prefix {
198 	short	ldo_valid;
199 	int		ldo_debug;
200 };
201 #define ldap_debug \
202 	(*(int *) ((char *)&ldap_int_global_options \
203 		 + offsetof(struct ldapoptions_prefix, ldo_debug)))
204 
205 struct ldapoptions;
206 LDAP_V ( struct ldapoptions ) ldap_int_global_options;
207 #endif /* LDAP_DEFINE_LDAP_DEBUG */
208 
209 LDAP_END_DECL
210 
211 #endif /* LDAP_LOG_H */
212