]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/printmsg.c
include unistd.h for F_LOCK
[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 <slapi_common.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdarg.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <errno.h>
21
22 #include <ldap.h>
23 #include <ldap_config.h>
24 #include <slap.h>
25 #include <slapi.h>
26
27 #include <ldap_pvt_thread.h>
28
29 /* Single threads access to routine */
30 ldap_pvt_thread_mutex_t slapi_printmessage_mutex; 
31 char                    *slapi_log_file = NULL;
32 int                     slapi_log_level = SLAPI_LOG_PLUGIN;
33
34 int 
35 vLogError(
36         int             level,  
37         char            *subsystem, 
38         char            *fmt, 
39         va_list         arglist ) 
40 {
41         int             rc = 0;
42         FILE            *fp = NULL;
43
44         char            timeStr[100];
45         struct tm       *ltm;
46         time_t          currentTime;
47
48         assert( subsystem != NULL );
49         assert( fmt != NULL );
50
51         ldap_pvt_thread_mutex_lock( &slapi_printmessage_mutex ) ;
52
53         /* for now, we log all severities */
54         if ( level <= slapi_log_level ) {
55                 fp = fopen( slapi_log_file, "a" );
56                 if ( fp == NULL) {
57                         rc = -1;
58                         goto done;
59                 }
60
61                 /*
62                  * FIXME: could block
63                  */
64                 while ( lockf( fileno( fp ), F_LOCK, 0 ) != 0 ) {
65                         /* DO NOTHING */ ;
66                 }
67
68                 time( &currentTime );
69                 ltm = localtime( &currentTime );
70                 strftime( timeStr, sizeof(timeStr), "%x %X", ltm );
71                 fputs( timeStr, fp );
72
73                 fprintf( fp, " %s: ", subsystem );
74                 vfprintf( fp, fmt, arglist );
75                 if ( fmt[ strlen( fmt ) - 1 ] != '\n' ) {
76                         fputs( "\n", fp );
77                 }
78                 fflush( fp );
79
80                 lockf( fileno( fp ), F_ULOCK, 0 );
81
82                 fclose( fp );
83
84         } else {
85                 rc = -1;
86         }
87
88 done:
89         ldap_pvt_thread_mutex_unlock( &slapi_printmessage_mutex );
90
91         return rc;
92 }