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