]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapi/printmsg.c
improve usability of global ovrlays for write operations; may need to anticipate...
[openldap] / servers / slapd / slapi / printmsg.c
index 6ecdadc3a8572f503394316d102fa8a4275ff8cc..658eca5c270c325621f7533c6a1312ca33bca186 100644 (file)
@@ -1,22 +1,31 @@
-/*
- * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2002-2004 The OpenLDAP Foundation.
+ * Portions Copyright 1997,2002-2003 IBM Corporation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
-/*
- * (C) Copyright IBM Corp. 1997,2002
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is 
- * given to IBM Corporation. This software is provided ``as is'' 
- * without express or implied warranty.
+/* ACKNOWLEDGEMENTS:
+ * This work was initially developed by IBM Corporation for use in
+ * IBM products and subsequently ported to OpenLDAP Software by
+ * Steve Omrani.
  */
 
 #include <portable.h>
-#include <slapi_common.h>
 #include <stdio.h>
-#include <string.h>
-#include <stdarg.h>
+#include <ac/string.h>
+#include <ac/stdarg.h>
+#include <ac/unistd.h>
 #include <fcntl.h>
-#include <errno.h>
+#include <ac/errno.h>
 
 #include <ldap.h>
 #include <ldap_config.h>
 #include <ldap_pvt_thread.h>
 
 /* Single threads access to routine */
-static ldap_pvt_thread_mutex_t PrintMessage_mutex; 
-static int PrintMessage_mutex_inited = 0;
-
-static void
-InitMutex () 
-{
-       if (PrintMessage_mutex_inited == 0) {
-               PrintMessage_mutex_inited = 1;
-               ldap_pvt_thread_mutex_init(&PrintMessage_mutex);
-       }
-}
+ldap_pvt_thread_mutex_t slapi_printmessage_mutex; 
+char                   *slapi_log_file = NULL;
+int                    slapi_log_level = SLAPI_LOG_PLUGIN;
 
 int 
-vLogError(
-       int level,      
-       char *subsystem, 
-       char *fmt, 
-       va_list arglist ) 
+slapi_int_log_error(
+       int             level,  
+       char            *subsystem, 
+       char            *fmt, 
+       va_list         arglist ) 
 {
-       int rc = 0;
-       char *tmpFmt;
-       FILE * fp = NULL;
-       char *p, *sval;
-       int ival;
+       int             rc = 0;
+       FILE            *fp = NULL;
 
-       char timeStr[100];
-       struct tm *ltm;
-       time_t currentTime;
+       char            timeStr[100];
+       struct tm       *ltm;
+       time_t          currentTime;
 
-       tmpFmt = fmt;
-       fmt = (char*)ch_calloc(strlen(subsystem) + strlen(tmpFmt) + 3, 1);
-       sprintf(fmt, "%s: %s", subsystem, tmpFmt);
+       assert( subsystem != NULL );
+       assert( fmt != NULL );
 
-       InitMutex() ;
-       ldap_pvt_thread_mutex_lock( &PrintMessage_mutex ) ;
+       ldap_pvt_thread_mutex_lock( &slapi_printmessage_mutex ) ;
 
        /* for now, we log all severities */
-       if ( 1 ) {
-               fp = fopen( LDAP_RUNDIR LDAP_DIRSEP "errors", "a" );
-               if (fp == NULL) 
-                       fp = fopen( "errors", "a" );
-                       
-               if ( fp != NULL) {
-                       while ( lockf(fileno(fp), F_LOCK, 0 ) != 0 ) {}
-
-                       time (&currentTime);
-                       ltm = localtime( &currentTime );
-                       strftime( timeStr, sizeof(timeStr), "%x %X ", ltm );
-                       fprintf(fp, timeStr);
-                       for (p = fmt; *p; p++) {
-                               if (*p != '%') {
-                                       fprintf(fp, "%c", *p);
-                                       continue;
-                               }
-                               switch(*++p) {
-                               case 'd':
-                                       ival = va_arg( arglist, int);
-                                       fprintf(fp, "%d", ival);
-                                       break;
-                               case 's':
-                                       for (sval = va_arg(arglist, char *); *sval; sval++)
-                                               fprintf(fp, "%c", *sval);
-                                       break;
-                               default:
-                                       fprintf(fp, "%c", *p);
-                                       break;
-                               
-                               }
-                       }
-       
-                       fflush(fp);
-
-                       lockf( fileno(fp), F_ULOCK, 0 );
-
-                       fclose(fp);
-               } else {
-#if 0 /* unused */
-                       int save_errno = (int)errno;
-#endif /* unused */
-                       rc = ( -1);
+       if ( level <= slapi_log_level ) {
+               fp = fopen( slapi_log_file, "a" );
+               if ( fp == NULL) {
+                       rc = -1;
+                       goto done;
+               }
+
+               /*
+                * FIXME: could block
+                */
+               while ( lockf( fileno( fp ), F_LOCK, 0 ) != 0 ) {
+                       /* DO NOTHING */ ;
                }
+
+               time( &currentTime );
+               ltm = localtime( &currentTime );
+               strftime( timeStr, sizeof(timeStr), "%x %X", ltm );
+               fputs( timeStr, fp );
+
+               fprintf( fp, " %s: ", subsystem );
+               vfprintf( fp, fmt, arglist );
+               if ( fmt[ strlen( fmt ) - 1 ] != '\n' ) {
+                       fputs( "\n", fp );
+               }
+               fflush( fp );
+
+               lockf( fileno( fp ), F_ULOCK, 0 );
+
+               fclose( fp );
+
        } else {
-               rc = ( -1);
+               rc = -1;
        }
 
-       ldap_pvt_thread_mutex_unlock( &PrintMessage_mutex );
-       ch_free(fmt);
+done:
+       ldap_pvt_thread_mutex_unlock( &slapi_printmessage_mutex );
 
-       return (rc);
+       return rc;
 }