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

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