Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / slapi / printmsg.c
blobc0581fe9540a1d90222bd989dca9a5e91218454c
1 /* $OpenLDAP: pkg/ldap/servers/slapd/slapi/printmsg.c,v 1.15.2.3 2008/02/11 23:26:49 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2008 The OpenLDAP Foundation.
5 * Portions Copyright 1997,2002-2003 IBM Corporation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* ACKNOWLEDGEMENTS:
17 * This work was initially developed by IBM Corporation for use in
18 * IBM products and subsequently ported to OpenLDAP Software by
19 * Steve Omrani.
22 #include <portable.h>
23 #include <stdio.h>
24 #include <ac/string.h>
25 #include <ac/stdarg.h>
26 #include <ac/unistd.h>
27 #include <fcntl.h>
28 #include <ac/errno.h>
30 #include <ldap.h>
31 #include <ldap_config.h>
32 #include <slap.h>
33 #include <slapi.h>
35 #include <ldap_pvt_thread.h>
37 /* Single threads access to routine */
38 ldap_pvt_thread_mutex_t slapi_printmessage_mutex;
39 char *slapi_log_file = NULL;
40 int slapi_log_level = SLAPI_LOG_PLUGIN;
42 int
43 slapi_int_log_error(
44 int level,
45 char *subsystem,
46 char *fmt,
47 va_list arglist )
49 int rc = 0;
50 FILE *fp = NULL;
52 char timeStr[100];
53 struct tm *ltm;
54 time_t currentTime;
56 assert( subsystem != NULL );
57 assert( fmt != NULL );
59 ldap_pvt_thread_mutex_lock( &slapi_printmessage_mutex ) ;
61 /* for now, we log all severities */
62 if ( level <= slapi_log_level ) {
63 fp = fopen( slapi_log_file, "a" );
64 if ( fp == NULL) {
65 rc = -1;
66 goto done;
70 * FIXME: could block
72 while ( lockf( fileno( fp ), F_LOCK, 0 ) != 0 ) {
73 /* DO NOTHING */ ;
76 time( &currentTime );
77 ltm = localtime( &currentTime );
78 strftime( timeStr, sizeof(timeStr), "%x %X", ltm );
79 fputs( timeStr, fp );
81 fprintf( fp, " %s: ", subsystem );
82 vfprintf( fp, fmt, arglist );
83 if ( fmt[ strlen( fmt ) - 1 ] != '\n' ) {
84 fputs( "\n", fp );
86 fflush( fp );
88 lockf( fileno( fp ), F_ULOCK, 0 );
90 fclose( fp );
92 } else {
93 rc = -1;
96 done:
97 ldap_pvt_thread_mutex_unlock( &slapi_printmessage_mutex );
99 return rc;