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