Back to index

openldap  2.4.31
printmsg.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 2002-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 1997,2002-2003 IBM Corporation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by IBM Corporation for use in
00018  * IBM products and subsequently ported to OpenLDAP Software by
00019  * Steve Omrani.
00020  */
00021 
00022 #include <portable.h>
00023 #include <stdio.h>
00024 #include <ac/string.h>
00025 #include <ac/stdarg.h>
00026 #include <ac/unistd.h>
00027 #include <fcntl.h>
00028 #include <ac/errno.h>
00029 
00030 #include <ldap.h>
00031 #include <ldap_config.h>
00032 #include <slap.h>
00033 #include <slapi.h>
00034 
00035 #include <ldap_pvt_thread.h>
00036 
00037 /* Single threads access to routine */
00038 ldap_pvt_thread_mutex_t slapi_printmessage_mutex; 
00039 char                 *slapi_log_file = NULL;
00040 int                  slapi_log_level = SLAPI_LOG_PLUGIN;
00041 
00042 int 
00043 slapi_int_log_error(
00044        int           level,        
00045        char          *subsystem, 
00046        char          *fmt, 
00047        va_list              arglist ) 
00048 {
00049        int           rc = 0;
00050        FILE          *fp = NULL;
00051 
00052        char          timeStr[100];
00053        struct tm     *ltm;
00054        time_t        currentTime;
00055 
00056        assert( subsystem != NULL );
00057        assert( fmt != NULL );
00058 
00059        ldap_pvt_thread_mutex_lock( &slapi_printmessage_mutex ) ;
00060 
00061        /* for now, we log all severities */
00062        if ( level <= slapi_log_level ) {
00063               fp = fopen( slapi_log_file, "a" );
00064               if ( fp == NULL) {
00065                      rc = -1;
00066                      goto done;
00067               }
00068 
00069               /*
00070                * FIXME: could block
00071                */
00072               while ( lockf( fileno( fp ), F_LOCK, 0 ) != 0 ) {
00073                      /* DO NOTHING */ ;
00074               }
00075 
00076               time( &currentTime );
00077               ltm = localtime( &currentTime );
00078               strftime( timeStr, sizeof(timeStr), "%x %X", ltm );
00079               fputs( timeStr, fp );
00080 
00081               fprintf( fp, " %s: ", subsystem );
00082               vfprintf( fp, fmt, arglist );
00083               if ( fmt[ strlen( fmt ) - 1 ] != '\n' ) {
00084                      fputs( "\n", fp );
00085               }
00086               fflush( fp );
00087 
00088               lockf( fileno( fp ), F_ULOCK, 0 );
00089 
00090               fclose( fp );
00091 
00092        } else {
00093               rc = -1;
00094        }
00095 
00096 done:
00097        ldap_pvt_thread_mutex_unlock( &slapi_printmessage_mutex );
00098 
00099        return rc;
00100 }