printmsg.c 1.82 KB
Newer Older
1
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3
4
5
6
7
8
9
10
11
12
13
14
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
/*
 * (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.
 */

#include <portable.h>
#include <stdio.h>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
15
16
17
#include <ac/string.h>
#include <ac/stdarg.h>
#include <ac/unistd.h>
18
#include <fcntl.h>
Pierangelo Masarati's avatar
Pierangelo Masarati committed
19
#include <ac/errno.h>
20
21
22
23
24
25
26
27
28

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

#include <ldap_pvt_thread.h>

/* Single threads access to routine */
29
ldap_pvt_thread_mutex_t slapi_printmessage_mutex; 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
30
char			*slapi_log_file = NULL;
31
int			slapi_log_level = SLAPI_LOG_PLUGIN;
32
33
34

int 
vLogError(
35
36
37
38
	int		level, 	
	char		*subsystem, 
	char		*fmt, 
	va_list		arglist ) 
39
{
40
41
	int		rc = 0;
	FILE		*fp = NULL;
42

43
44
45
	char		timeStr[100];
	struct tm	*ltm;
	time_t		currentTime;
46

47
48
	assert( subsystem != NULL );
	assert( fmt != NULL );
49

50
	ldap_pvt_thread_mutex_lock( &slapi_printmessage_mutex ) ;
51
52

	/* for now, we log all severities */
53
54
55
56
57
58
59
60
61
62
63
64
	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 */ ;
65
		}
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

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

83
	} else {
84
		rc = -1;
85
86
	}

87
88
done:
	ldap_pvt_thread_mutex_unlock( &slapi_printmessage_mutex );
89

90
	return rc;
91
}