GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ldapd/log.c Lines: 36 71 50.7 %
Date: 2017-11-13 Branches: 6 18 33.3 %

Line Branch Exec Source
1
/*	$OpenBSD: log.c,v 1.8 2017/03/21 12:06:55 bluhm Exp $	*/
2
3
/*
4
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/time.h>
20
#include <errno.h>
21
#include <stdarg.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <syslog.h>
26
#include <time.h>
27
#include <unistd.h>
28
29
#include "log.h"
30
31
int	 	 debug;
32
int	 	 verbose;
33
34
void
35
log_init(int n_debug)
36
{
37
	extern char	*__progname;
38
39
96
	debug = n_debug;
40
41
48
	if (!debug)
42
		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
43
44
48
	tzset();
45
48
}
46
47
void
48
log_verbose(int v)
49
{
50
64
	verbose = v;
51
32
}
52
53
void
54
logit(int pri, const char *fmt, ...)
55
{
56
	va_list	ap;
57
58
	va_start(ap, fmt);
59
	vlog(pri, fmt, ap);
60
	va_end(ap);
61
}
62
63
void
64
vlog(int pri, const char *fmt, va_list ap)
65
{
66
320
	char		 datebuf[24];
67
160
	struct timeval	 tv;
68
	struct tm	*tm;
69
160
	char		*nfmt;
70
	size_t		 rc;
71
160
	time_t		 now;
72
73
160
	if (debug) {
74
160
		gettimeofday(&tv, NULL);
75
160
		now = tv.tv_sec;
76
160
		tm = localtime(&now);
77
160
		rc = strftime(datebuf, sizeof(datebuf), "%b %e %H:%M:%S", tm);
78
160
		if (rc == 0)
79
			datebuf[0] = 0;
80
160
		fprintf(stderr, "%s.%03ld [%d] ", datebuf,
81
160
		    tv.tv_usec / 1000, getpid());
82
83
		/* best effort in out of mem situations */
84
160
		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
85
			vfprintf(stderr, fmt, ap);
86
			fprintf(stderr, "\n");
87
		} else {
88
160
			vfprintf(stderr, nfmt, ap);
89
160
			free(nfmt);
90
		}
91
160
		fflush(stderr);
92
160
	} else
93
		vsyslog(pri, fmt, ap);
94
160
}
95
96
void
97
log_warn(const char *emsg, ...)
98
{
99
	char	*nfmt;
100
	va_list	 ap;
101
102
	/* best effort to even work in out of memory situations */
103
	if (emsg == NULL)
104
		logit(LOG_ERR, "%s", strerror(errno));
105
	else {
106
		va_start(ap, emsg);
107
108
		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
109
			/* we tried it... */
110
			vlog(LOG_ERR, emsg, ap);
111
			logit(LOG_ERR, "%s", strerror(errno));
112
		} else {
113
			vlog(LOG_ERR, nfmt, ap);
114
			free(nfmt);
115
		}
116
		va_end(ap);
117
	}
118
}
119
120
void
121
log_warnx(const char *emsg, ...)
122
{
123
	va_list	 ap;
124
125
	va_start(ap, emsg);
126
	vlog(LOG_ERR, emsg, ap);
127
	va_end(ap);
128
}
129
130
void
131
log_info(const char *emsg, ...)
132
{
133
96
	va_list	 ap;
134
135
48
	va_start(ap, emsg);
136
48
	vlog(LOG_INFO, emsg, ap);
137
48
	va_end(ap);
138
48
}
139
140
void
141
log_debug(const char *emsg, ...)
142
{
143
416
	va_list	 ap;
144
145
208
	if (verbose) {
146
112
		va_start(ap, emsg);
147
112
		vlog(LOG_DEBUG, emsg, ap);
148
112
		va_end(ap);
149
112
	}
150
208
}
151
152
void
153
fatal(const char *emsg)
154
{
155
	if (emsg == NULL)
156
		logit(LOG_CRIT, "fatal: %s", strerror(errno));
157
	else
158
		if (errno)
159
			logit(LOG_CRIT, "fatal: %s: %s",
160
			    emsg, strerror(errno));
161
		else
162
			logit(LOG_CRIT, "fatal: %s", emsg);
163
164
	exit(1);
165
}
166
167
void
168
fatalx(const char *emsg)
169
{
170
	errno = 0;
171
	fatal(emsg);
172
}