]> git.sur5r.net Git - openldap/blob - libraries/libldap/print.c
Add lber_log_print support to libldap.
[openldap] / libraries / libldap / print.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/ctype.h>
11 #include <ac/stdarg.h>
12 #include <ac/string.h>
13 #include <ac/time.h>
14
15 #include "ldap-int.h"
16
17 extern BER_LOG_PRINT_FN lber_log_print;
18
19 /*
20  * ldap log 
21  */
22
23 static int ldap_log_check( LDAP *ld, int loglvl )
24 {
25         int errlvl;
26
27         if(ld == NULL) {
28                 errlvl = ldap_debug;
29         } else {
30                 errlvl = ld->ld_errno;
31         }
32
33         return errlvl & loglvl ? 1 : 0;
34 }
35
36 int ldap_log_printf
37 #ifdef HAVE_STDARG
38         ( LDAP *ld, int loglvl, char *fmt, ... )
39 #else
40         ( va_alist )
41 va_dcl
42 #endif
43 {
44         char buf[ 1024 ];
45         va_list ap;
46
47 #ifdef HAVE_STDARG
48         va_start( ap, fmt );
49 #else
50         LD *ld
51         int loglvl;
52         char *fmt;
53
54         va_start( ap );
55
56         errlvl = va_arg( ap, LD * );
57         loglvl = va_arg( ap, int );
58         fmt = va_arg( ap, char * );
59 #endif
60
61         if ( !ldap_log_check( ld, loglvl )) {
62                 return 0;
63         }
64
65 #ifdef HAVE_VSNPRINTF
66         buf[sizeof(buf) - 1] = '\0';
67         vsnprintf( buf, sizeof(buf)-1, fmt, ap );
68 #elif HAVE_VSPRINTF
69         vsprintf( buf, fmt, ap ); /* hope it's not too long */
70 #else
71         /* use doprnt() */
72         chokeme = "choke me! I don't have a doprnt manual handy!";
73 #endif
74
75         va_end(ap);
76
77         (*lber_log_print)( buf );
78         return 1;
79 }
80
81 static int lber_log_puts(int errlvl, int loglvl, char *buf)
82 {
83         if ( !ldap_log_check( errlvl, loglvl )) {
84                 return 0;
85         }
86
87         (*lber_log_print)( buf );
88         return 1;
89 }