GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/rtadvd/log.c Lines: 0 63 0.0 %
Date: 2017-11-13 Branches: 0 16 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: log.c,v 1.4 2017/07/12 06:09:59 florian 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 <errno.h>
20
#include <stdarg.h>
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <syslog.h>
25
#include <time.h>
26
#include <unistd.h>
27
28
#include "log.h"
29
30
int		 debug;
31
int		 verbose;
32
const char	*log_procname;
33
34
void
35
log_init(int n_debug)
36
{
37
	extern char	*__progname;
38
39
	debug = n_debug;
40
41
	if (!debug)
42
		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
43
44
	tzset();
45
}
46
47
void
48
log_verbose(int v)
49
{
50
	verbose = v;
51
}
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
	char	*nfmt;
67
68
	if (debug) {
69
		/* best effort in out of mem situations */
70
		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
71
			vfprintf(stderr, fmt, ap);
72
			fprintf(stderr, "\n");
73
		} else {
74
			vfprintf(stderr, nfmt, ap);
75
			free(nfmt);
76
		}
77
		fflush(stderr);
78
	} else
79
		vsyslog(pri, fmt, ap);
80
}
81
82
void
83
log_warn(const char *emsg, ...)
84
{
85
	char	*nfmt;
86
	va_list	 ap;
87
88
	/* best effort to even work in out of memory situations */
89
	if (emsg == NULL)
90
		logit(LOG_ERR, "%s", strerror(errno));
91
	else {
92
		va_start(ap, emsg);
93
94
		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
95
			/* we tried it... */
96
			vlog(LOG_ERR, emsg, ap);
97
			logit(LOG_ERR, "%s", strerror(errno));
98
		} else {
99
			vlog(LOG_ERR, nfmt, ap);
100
			free(nfmt);
101
		}
102
		va_end(ap);
103
	}
104
}
105
106
void
107
log_warnx(const char *emsg, ...)
108
{
109
	va_list	 ap;
110
111
	va_start(ap, emsg);
112
	vlog(LOG_ERR, emsg, ap);
113
	va_end(ap);
114
}
115
116
void
117
log_info(const char *emsg, ...)
118
{
119
	va_list	 ap;
120
121
	va_start(ap, emsg);
122
	vlog(LOG_INFO, emsg, ap);
123
	va_end(ap);
124
}
125
126
void
127
log_debug(const char *emsg, ...)
128
{
129
	va_list	 ap;
130
131
	if (verbose) {
132
		va_start(ap, emsg);
133
		vlog(LOG_DEBUG, emsg, ap);
134
		va_end(ap);
135
	}
136
}
137
138
static void
139
vfatalc(int code, const char *emsg, va_list ap)
140
{
141
	static char	s[BUFSIZ];
142
	const char	*sep;
143
144
	if (emsg != NULL) {
145
		(void)vsnprintf(s, sizeof(s), emsg, ap);
146
		sep = ": ";
147
	} else {
148
		s[0] = '\0';
149
		sep = "";
150
	}
151
	if (code)
152
		logit(LOG_CRIT, "fatal in %s: %s%s%s",
153
		    log_procname, s, sep, strerror(code));
154
	else
155
		logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
156
}
157
158
void
159
fatal(const char *emsg, ...)
160
{
161
	va_list	ap;
162
163
	va_start(ap, emsg);
164
	vfatalc(errno, emsg, ap);
165
	va_end(ap);
166
	exit(1);
167
}
168
169
void
170
fatalx(const char *emsg, ...)
171
{
172
	va_list	ap;
173
174
	va_start(ap, emsg);
175
	vfatalc(0, emsg, ap);
176
	va_end(ap);
177
	exit(1);
178
}