From b527294153be3b79563c82c66102adc0004736c0 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 19:54:12 +0200 Subject: Adding upstream version 2.6.7+dfsg. Signed-off-by: Daniel Baumann --- servers/slapd/slapi/printmsg.c | 120 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 servers/slapd/slapi/printmsg.c (limited to 'servers/slapd/slapi/printmsg.c') diff --git a/servers/slapd/slapi/printmsg.c b/servers/slapd/slapi/printmsg.c new file mode 100644 index 0000000..5251846 --- /dev/null +++ b/servers/slapd/slapi/printmsg.c @@ -0,0 +1,120 @@ +/* $OpenLDAP$ */ +/* This work is part of OpenLDAP Software . + * + * Copyright 2002-2022 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 + * . + */ +/* ACKNOWLEDGEMENTS: + * This work was initially developed by IBM Corporation for use in + * IBM products and subsequently ported to OpenLDAP Software by + * Steve Omrani. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#endif + +#include + +/* Single threads access to routine */ +ldap_pvt_thread_mutex_t slapi_printmessage_mutex; +char *slapi_log_file = NULL; +int slapi_log_level = SLAPI_LOG_PLUGIN; + +int +slapi_int_log_error( + int level, + char *subsystem, + char *fmt, + va_list arglist ) +{ + int rc = 0; + FILE *fp = NULL; + + char timeStr[100]; + struct tm *ltm; + time_t currentTime; + + assert( subsystem != NULL ); + assert( fmt != NULL ); + + ldap_pvt_thread_mutex_lock( &slapi_printmessage_mutex ) ; + + /* for now, we log all severities */ + if ( level <= slapi_log_level ) { +#ifdef _WIN32 + intptr_t fhandle; +#endif + fp = fopen( slapi_log_file, "a" ); + if ( fp == NULL) { + rc = -1; + goto done; + } + +#ifdef _WIN32 + fhandle = _get_osfhandle( fileno( fp )); +#endif + /* + * FIXME: could block + */ +#ifdef _WIN32 + while ( LockFile( fhandle, 0, 0, UINT_MAX, UINT_MAX ) == 0 ) { + /* DO NOTHING */ ; + } +#else + while ( lockf( fileno( fp ), F_LOCK, 0 ) != 0 ) { + /* DO NOTHING */ ; + } +#endif + + time( ¤tTime ); + ltm = localtime( ¤tTime ); + 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 ); + +#ifdef _WIN32 + UnlockFile( fhandle, 0, 0, UINT_MAX, UINT_MAX ); +#else + lockf( fileno( fp ), F_ULOCK, 0 ); +#endif + + fclose( fp ); + + } else { + rc = -1; + } + +done: + ldap_pvt_thread_mutex_unlock( &slapi_printmessage_mutex ); + + return rc; +} -- cgit v1.2.3