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/>. 3 * 4 * Copyright 2002-2008 The OpenLDAP Foundation. 5 * Portions Copyright 1997,2002-2003 IBM Corporation. 6 * All rights reserved. 7 * 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. 11 * 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>. 15 */ 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. 20 */ 21 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> 29 30 #include <ldap.h> 31 #include <ldap_config.h> 32 #include <slap.h> 33 #include <slapi.h> 34 35 #include <ldap_pvt_thread.h> 36 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; 41 42 int 43 slapi_int_log_error( 44 int level, 45 char *subsystem, 46 char *fmt, 47 va_list arglist ) 48 { 49 int rc = 0; 50 FILE *fp = NULL; 51 52 char timeStr[100]; 53 struct tm *ltm; 54 time_t currentTime; 55 56 assert( subsystem != NULL ); 57 assert( fmt != NULL ); 58 59 ldap_pvt_thread_mutex_lock( &slapi_printmessage_mutex ) ; 60 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; 67 } 68 69 /* 70 * FIXME: could block 71 */ 72 while ( lockf( fileno( fp ), F_LOCK, 0 ) != 0 ) { 73 /* DO NOTHING */ ; 74 } 75 76 time( ¤tTime ); 77 ltm = localtime( ¤tTime ); 78 strftime( timeStr, sizeof(timeStr), "%x %X", ltm ); 79 fputs( timeStr, fp ); 80 81 fprintf( fp, " %s: ", subsystem ); 82 vfprintf( fp, fmt, arglist ); 83 if ( fmt[ strlen( fmt ) - 1 ] != '\n' ) { 84 fputs( "\n", fp ); 85 } 86 fflush( fp ); 87 88 lockf( fileno( fp ), F_ULOCK, 0 ); 89 90 fclose( fp ); 91 92 } else { 93 rc = -1; 94 } 95 96 done: 97 ldap_pvt_thread_mutex_unlock( &slapi_printmessage_mutex ); 98 99 return rc; 100 } 101