printmsg.c 2.14 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* $OpenLDAP$ */
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 2002-2019 The OpenLDAP Foundation.
5
6
7
 * Portions Copyright 1997,2002-2003 IBM Corporation.
 * All rights reserved.
 *
8
9
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
10
11
12
13
14
15
16
17
18
19
 * 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>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by IBM Corporation for use in
 * IBM products and subsequently ported to OpenLDAP Software by
 * Steve Omrani.
20
21
22
23
 */

#include <portable.h>
#include <stdio.h>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
24
25
26
#include <ac/string.h>
#include <ac/stdarg.h>
#include <ac/unistd.h>
27
#include <fcntl.h>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
28
#include <ac/errno.h>
29
30
31
32
33
34
35
36
37

#include <ldap.h>
#include <ldap_config.h>
#include <slap.h>
#include <slapi.h>

#include <ldap_pvt_thread.h>

/* Single threads access to routine */
38
ldap_pvt_thread_mutex_t slapi_printmessage_mutex; 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
39
char			*slapi_log_file = NULL;
40
int			slapi_log_level = SLAPI_LOG_PLUGIN;
41
42

int 
Luke Howard's avatar
Luke Howard committed
43
slapi_int_log_error(
44
45
46
47
	int		level, 	
	char		*subsystem, 
	char		*fmt, 
	va_list		arglist ) 
48
{
49
50
	int		rc = 0;
	FILE		*fp = NULL;
51

52
53
54
	char		timeStr[100];
	struct tm	*ltm;
	time_t		currentTime;
55

56
57
	assert( subsystem != NULL );
	assert( fmt != NULL );
58

59
	ldap_pvt_thread_mutex_lock( &slapi_printmessage_mutex ) ;
60
61

	/* for now, we log all severities */
62
63
64
65
66
67
68
69
70
71
72
73
	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 */ ;
74
		}
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

		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 );

92
	} else {
93
		rc = -1;
94
95
	}

96
97
done:
	ldap_pvt_thread_mutex_unlock( &slapi_printmessage_mutex );
98

99
	return rc;
100
}