summaryrefslogtreecommitdiff
path: root/usr/src/lib/libldap4/util/log.c
blob: 29ea85a64463f56f53eb83b4b04ad7925a59b816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (c) 1998-1999 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <nl_types.h>
#include <limits.h>
#include <stdarg.h>
#include <string.h>

#include <syslog.h>
#include <portable.h>
/* #include <lthread.h> */
#include <pthread.h>
#include <thread.h>

#include "log.h"

#define	LDAP_DEBUG_ANY	0xffff

static pthread_mutex_t	log_mutex;
static char		logfile[PATH_MAX] =
					"/var/opt/SUNWconn/ldap/log/slapd.log";
static int		logsize = 512000;
static int		logtime = 1;
static FILE		*logfd = NULL;
static int		syslogopen = 0;
pthread_mutex_t		systime_mutex;
nl_catd			sundscat;
static int		log_debug = LDAP_DEBUG_STATS;

typedef struct _logctx {
	char		*logfile;
	int		syslogopen;
	int		logsize;
	pthread_mutex_t	log_mutex;
	int		log_debug;
	int		log_syslog;

} LogCtx;

void
ldaplogconfig(char *logf, int size)
{
	strcpy(logfile, logf);
	logsize = size * 1024;
}

void
ldaplogconfigf(FILE *fd)
{
	logfd = fd;
	logsize = 0;
}

void
ldaploginit(char *name, int facility)
{
	openlog(name, OPENLOG_OPTIONS, facility);
	syslogopen = 1;
	pthread_mutex_init(&log_mutex, NULL);
}

void
ldaploginitlevel(char *name, int facility, int level)
{
	ldaploginit(name, facility);
	log_debug = level;
}

LogCtx *
sundsloginit(char *name, int facility, int debug_level, int syslog_level)
{
	LogCtx *returnCtx = NULL;

	if ((returnCtx = (LogCtx *)malloc(sizeof (LogCtx))) == NULL)
		return (NULL);
	if ((returnCtx->logfile = strdup(name)) == NULL) {
		free(returnCtx);
		return (NULL);
	}
	openlog(returnCtx->logfile, OPENLOG_OPTIONS, facility);
	returnCtx->syslogopen = 1;
	pthread_mutex_init(&(returnCtx->log_mutex), NULL);
	returnCtx->log_debug = debug_level;
	returnCtx->log_syslog = syslog_level;
	return (returnCtx);
}

static char timestr[128];
static time_t timelast = 0;

/*VARARGS*/
void
ldaplog(int level, char *fmt, ...)
{
	va_list ap;
	struct stat statbuf = {0};
	char newlog1[PATH_MAX];
	char newlog2[PATH_MAX];
	time_t now;
	int i;

	if (!(log_debug & level))
		return;

	va_start(ap, fmt);

	if (level == LDAP_DEBUG_ANY) {
		/*
		 * this message is probably an error message, send it to syslog
		 */
		if (syslogopen) {
			vsyslog(LOG_ERR, fmt, ap);
		} /* end if */
		/* and sent it also on stderr */
		vfprintf(stderr, fmt, ap);
	} /* end if */

	/*
	 * check that the log file is not already too big
	 */
	pthread_mutex_lock(&log_mutex);
	if ((logsize > 0) && (stat(logfile, &statbuf) == 0 &&
					statbuf.st_size > logsize)) {
		for (i = 9; i > 1; i--) {
			(void) sprintf(newlog1, "%s.%d", logfile, i-1);
			(void) sprintf(newlog2, "%s.%d", logfile, i);
			(void) rename(newlog1, newlog2);
		} /* end for */
		if (logfd) {
			fclose(logfd);
			logfd = NULL;
		} /* end if */
		(void) rename(logfile, newlog1);
	} /* end if */
	/*
	 * send the message into a regular log file
	 */
	if (!logfd) {
		logfd = fopen(logfile, "a");
	} /* end if */
	/*
	 * finally write the message into the log file
	 */
	if (logfd) {
		if (logtime) {
			time(&now);
			if (now-timelast > 60) {
				pthread_mutex_lock(&systime_mutex);
				timelast = now;
				ctime_r(&now, timestr, 128);
				pthread_mutex_unlock(&systime_mutex);
			} /* end if */
			fprintf(logfd, "%.16s : ", timestr);
		} /* end if */
		vfprintf(logfd, fmt, ap);
		fflush(logfd);
	} /* end if */
	pthread_mutex_unlock(&log_mutex);
	va_end(ap);
}