]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/printmsg.c
SLAPI - Netscape plugin API for slapd - based on patch contributed by Steve Omrani...
[openldap] / servers / slapd / slapi / printmsg.c
1 /*
2  * Copyright 1998-2002 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 <fcntl.h>
19 #include <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 static ldap_pvt_thread_mutex_t PrintMessage_mutex; 
30 static int PrintMessage_mutex_inited = 0;
31
32 static void
33 InitMutex () 
34 {
35         if (PrintMessage_mutex_inited == 0) {
36                 PrintMessage_mutex_inited = 1;
37                 ldap_pvt_thread_mutex_init(&PrintMessage_mutex);
38         }
39 }
40
41 int 
42 vLogError(
43         int level,      
44         char *subsystem, 
45         char *fmt, 
46         va_list arglist ) 
47 {
48         int rc = 0;
49         char *tmpFmt;
50         FILE * fp = NULL;
51         char *p, *sval;
52         int ival;
53
54         char timeStr[100];
55         struct tm *ltm;
56         time_t currentTime;
57
58         tmpFmt = fmt;
59         fmt = (char*)ch_calloc(strlen(subsystem) + strlen(tmpFmt) + 3, 1);
60         sprintf(fmt, "%s: %s", subsystem, tmpFmt);
61
62         InitMutex() ;
63         ldap_pvt_thread_mutex_lock( &PrintMessage_mutex ) ;
64
65         /* for now, we log all severities */
66         if ( 1 ) {
67                 fp = fopen( LDAP_RUNDIR LDAP_DIRSEP "errors", "a" );
68                 if (fp == NULL) 
69                         fp = fopen( "errors", "a" );
70                         
71                 if ( fp != NULL) {
72                         while ( lockf(fileno(fp), F_LOCK, 0 ) != 0 ) {}
73
74                         time (&currentTime);
75                         ltm = localtime( &currentTime );
76                         strftime( timeStr, sizeof(timeStr), "%x %X ", ltm );
77                         fprintf(fp, timeStr);
78                         for (p = fmt; *p; p++) {
79                                 if (*p != '%') {
80                                         fprintf(fp, "%c", *p);
81                                         continue;
82                                 }
83                                 switch(*++p) {
84                                 case 'd':
85                                         ival = va_arg( arglist, int);
86                                         fprintf(fp, "%d", ival);
87                                         break;
88                                 case 's':
89                                         for (sval = va_arg(arglist, char *); *sval; sval++)
90                                                 fprintf(fp, "%c", *sval);
91                                         break;
92                                 default:
93                                         fprintf(fp, "%c", *p);
94                                         break;
95                                 
96                                 }
97                         }
98         
99                         fflush(fp);
100
101                         lockf( fileno(fp), F_ULOCK, 0 );
102
103                         fclose(fp);
104                 } else {
105 #if 0 /* unused */
106                         int save_errno = (int)errno;
107 #endif /* unused */
108                         rc = ( -1);
109                 }
110         } else {
111                 rc = ( -1);
112         }
113
114         ldap_pvt_thread_mutex_unlock( &PrintMessage_mutex );
115         ch_free(fmt);
116
117         return (rc);
118 }