]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/printmsg.c
reworking of slapi headers
[openldap] / servers / slapd / slapi / printmsg.c
1 /*
2  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * (C) Copyright IBM Corp. 1997,2002
7  * Redistribution and use in source and binary forms are permitted
8  * provided that this notice is preserved and that due credit is 
9  * given to IBM Corporation. This software is provided ``as is'' 
10  * without express or implied warranty.
11  */
12
13 #include <portable.h>
14 #include <stdio.h>
15 #include <ac/string.h>
16 #include <ac/stdarg.h>
17 #include <ac/unistd.h>
18 #include <fcntl.h>
19 #include <ac/errno.h>
20
21 #include <ldap.h>
22 #include <ldap_config.h>
23 #include <slap.h>
24 #include <slapi.h>
25
26 #include <ldap_pvt_thread.h>
27
28 /* Single threads access to routine */
29 ldap_pvt_thread_mutex_t slapi_printmessage_mutex; 
30 char                    *slapi_log_file = NULL;
31 int                     slapi_log_level = SLAPI_LOG_PLUGIN;
32
33 int 
34 vLogError(
35         int             level,  
36         char            *subsystem, 
37         char            *fmt, 
38         va_list         arglist ) 
39 {
40         int             rc = 0;
41         FILE            *fp = NULL;
42
43         char            timeStr[100];
44         struct tm       *ltm;
45         time_t          currentTime;
46
47         assert( subsystem != NULL );
48         assert( fmt != NULL );
49
50         ldap_pvt_thread_mutex_lock( &slapi_printmessage_mutex ) ;
51
52         /* for now, we log all severities */
53         if ( level <= slapi_log_level ) {
54                 fp = fopen( slapi_log_file, "a" );
55                 if ( fp == NULL) {
56                         rc = -1;
57                         goto done;
58                 }
59
60                 /*
61                  * FIXME: could block
62                  */
63                 while ( lockf( fileno( fp ), F_LOCK, 0 ) != 0 ) {
64                         /* DO NOTHING */ ;
65                 }
66
67                 time( &currentTime );
68                 ltm = localtime( &currentTime );
69                 strftime( timeStr, sizeof(timeStr), "%x %X", ltm );
70                 fputs( timeStr, fp );
71
72                 fprintf( fp, " %s: ", subsystem );
73                 vfprintf( fp, fmt, arglist );
74                 if ( fmt[ strlen( fmt ) - 1 ] != '\n' ) {
75                         fputs( "\n", fp );
76                 }
77                 fflush( fp );
78
79                 lockf( fileno( fp ), F_ULOCK, 0 );
80
81                 fclose( fp );
82
83         } else {
84                 rc = -1;
85         }
86
87 done:
88         ldap_pvt_thread_mutex_unlock( &slapi_printmessage_mutex );
89
90         return rc;
91 }