xref: /freebsd/lib/libiscsiutil/log.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 The FreeBSD Foundation
5  *
6  * This software was developed by Edward Tomasz Napierala under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <errno.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <syslog.h>
37 #include <vis.h>
38 
39 #include "libiscsiutil.h"
40 
41 static int log_level = 0;
42 static char *peer_name = NULL;
43 static char *peer_addr = NULL;
44 
45 #define	MSGBUF_LEN	1024
46 
47 void
48 log_init(int level)
49 {
50 
51 	log_level = level;
52 	openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
53 }
54 
55 void
56 log_set_peer_name(const char *name)
57 {
58 
59 	/*
60 	 * XXX: Turn it into assertion?
61 	 */
62 	if (peer_name != NULL)
63 		log_errx(1, "%s called twice", __func__);
64 	if (peer_addr == NULL)
65 		log_errx(1, "%s called before log_set_peer_addr", __func__);
66 
67 	peer_name = checked_strdup(name);
68 }
69 
70 void
71 log_set_peer_addr(const char *addr)
72 {
73 
74 	/*
75 	 * XXX: Turn it into assertion?
76 	 */
77 	if (peer_addr != NULL)
78 		log_errx(1, "%s called twice", __func__);
79 
80 	peer_addr = checked_strdup(addr);
81 }
82 
83 static void
84 log_common(int priority, int log_errno, const char *fmt, va_list ap)
85 {
86 	static char msgbuf[MSGBUF_LEN];
87 	static char msgbuf_strvised[MSGBUF_LEN * 4 + 1];
88 	char *errstr;
89 	int ret;
90 
91 	ret = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
92 	if (ret < 0) {
93 		fprintf(stderr, "%s: snprintf failed", getprogname());
94 		syslog(LOG_CRIT, "snprintf failed");
95 		exit(1);
96 	}
97 
98 	ret = strnvis(msgbuf_strvised, sizeof(msgbuf_strvised), msgbuf, VIS_NL);
99 	if (ret < 0) {
100 		fprintf(stderr, "%s: strnvis failed", getprogname());
101 		syslog(LOG_CRIT, "strnvis failed");
102 		exit(1);
103 	}
104 
105 	if (log_errno == -1) {
106 		if (peer_name != NULL) {
107 			fprintf(stderr, "%s: %s (%s): %s\n", getprogname(),
108 			    peer_addr, peer_name, msgbuf_strvised);
109 			syslog(priority, "%s (%s): %s",
110 			    peer_addr, peer_name, msgbuf_strvised);
111 		} else if (peer_addr != NULL) {
112 			fprintf(stderr, "%s: %s: %s\n", getprogname(),
113 			    peer_addr, msgbuf_strvised);
114 			syslog(priority, "%s: %s",
115 			    peer_addr, msgbuf_strvised);
116 		} else {
117 			fprintf(stderr, "%s: %s\n", getprogname(), msgbuf_strvised);
118 			syslog(priority, "%s", msgbuf_strvised);
119 		}
120 
121 	} else {
122 		errstr = strerror(log_errno);
123 
124 		if (peer_name != NULL) {
125 			fprintf(stderr, "%s: %s (%s): %s: %s\n", getprogname(),
126 			    peer_addr, peer_name, msgbuf_strvised, errstr);
127 			syslog(priority, "%s (%s): %s: %s",
128 			    peer_addr, peer_name, msgbuf_strvised, errstr);
129 		} else if (peer_addr != NULL) {
130 			fprintf(stderr, "%s: %s: %s: %s\n", getprogname(),
131 			    peer_addr, msgbuf_strvised, errstr);
132 			syslog(priority, "%s: %s: %s",
133 			    peer_addr, msgbuf_strvised, errstr);
134 		} else {
135 			fprintf(stderr, "%s: %s: %s\n", getprogname(),
136 			    msgbuf_strvised, errstr);
137 			syslog(priority, "%s: %s",
138 			    msgbuf_strvised, errstr);
139 		}
140 	}
141 }
142 
143 void
144 log_err(int eval, const char *fmt, ...)
145 {
146 	va_list ap;
147 
148 	va_start(ap, fmt);
149 	log_common(LOG_CRIT, errno, fmt, ap);
150 	va_end(ap);
151 
152 	exit(eval);
153 }
154 
155 void
156 log_errx(int eval, const char *fmt, ...)
157 {
158 	va_list ap;
159 
160 	va_start(ap, fmt);
161 	log_common(LOG_CRIT, -1, fmt, ap);
162 	va_end(ap);
163 
164 	exit(eval);
165 }
166 
167 void
168 log_warn(const char *fmt, ...)
169 {
170 	va_list ap;
171 
172 	va_start(ap, fmt);
173 	log_common(LOG_WARNING, errno, fmt, ap);
174 	va_end(ap);
175 }
176 
177 void
178 log_warnx(const char *fmt, ...)
179 {
180 	va_list ap;
181 
182 	va_start(ap, fmt);
183 	log_common(LOG_WARNING, -1, fmt, ap);
184 	va_end(ap);
185 }
186 
187 void
188 log_debugx(const char *fmt, ...)
189 {
190 	va_list ap;
191 
192 	if (log_level == 0)
193 		return;
194 
195 	va_start(ap, fmt);
196 	log_common(LOG_DEBUG, -1, fmt, ap);
197 	va_end(ap);
198 }
199