165918Sasmodai/*-
265918Sasmodai * SPDX-License-Identifier: BSD-2-Clause
365918Sasmodai *
465918Sasmodai * Copyright (c) 2012 The FreeBSD Foundation
565918Sasmodai *
665918Sasmodai * This software was developed by Edward Tomasz Napierala under sponsorship
765918Sasmodai * from the FreeBSD Foundation.
865918Sasmodai *
965918Sasmodai * Redistribution and use in source and binary forms, with or without
1065918Sasmodai * modification, are permitted provided that the following conditions
1165918Sasmodai * are met:
1265918Sasmodai * 1. Redistributions of source code must retain the above copyright
1365918Sasmodai *    notice, this list of conditions and the following disclaimer.
1465918Sasmodai * 2. Redistributions in binary form must reproduce the above copyright
1565918Sasmodai *    notice, this list of conditions and the following disclaimer in the
1665918Sasmodai *    documentation and/or other materials provided with the distribution.
1765918Sasmodai *
1865918Sasmodai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1965918Sasmodai * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2065918Sasmodai * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2165918Sasmodai * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2265918Sasmodai * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2365918Sasmodai * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2465918Sasmodai * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2565918Sasmodai * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2665918Sasmodai * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2765918Sasmodai * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2865918Sasmodai * SUCH DAMAGE.
2972112Sasmodai */
3065918Sasmodai
3165918Sasmodai#include <errno.h>
3272112Sasmodai#include <stdarg.h>
3365918Sasmodai#include <stdio.h>
3465918Sasmodai#include <stdlib.h>
3569356Sasmodai#include <string.h>
3669356Sasmodai#include <syslog.h>
3765918Sasmodai#include <vis.h>
3872112Sasmodai
3965918Sasmodai#include "libiscsiutil.h"
4072112Sasmodai
4165918Sasmodaistatic int log_level = 0;
4265918Sasmodaistatic char *peer_name = NULL;
4372112Sasmodaistatic char *peer_addr = NULL;
44
45#define	MSGBUF_LEN	1024
46
47void
48log_init(int level)
49{
50
51	log_level = level;
52	openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
53}
54
55void
56log_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
70void
71log_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
83static void
84log_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
143void
144log_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
155void
156log_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
167void
168log_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
177void
178log_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
187void
188log_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