xref: /freebsd/lib/libiscsiutil/log.c (revision de3deff65c5b407ab29b45780f2585b3bc24bbd6)
163783933SJohn Baldwin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
363783933SJohn Baldwin  *
463783933SJohn Baldwin  * Copyright (c) 2012 The FreeBSD Foundation
563783933SJohn Baldwin  *
663783933SJohn Baldwin  * This software was developed by Edward Tomasz Napierala under sponsorship
763783933SJohn Baldwin  * from the FreeBSD Foundation.
863783933SJohn Baldwin  *
963783933SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
1063783933SJohn Baldwin  * modification, are permitted provided that the following conditions
1163783933SJohn Baldwin  * are met:
1263783933SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
1363783933SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
1463783933SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
1563783933SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
1663783933SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
1763783933SJohn Baldwin  *
1863783933SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1963783933SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2063783933SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2163783933SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2263783933SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2363783933SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2463783933SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2563783933SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2663783933SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2763783933SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2863783933SJohn Baldwin  * SUCH DAMAGE.
2963783933SJohn Baldwin  */
3063783933SJohn Baldwin 
3163783933SJohn Baldwin #include <errno.h>
3263783933SJohn Baldwin #include <stdarg.h>
3363783933SJohn Baldwin #include <stdio.h>
3463783933SJohn Baldwin #include <stdlib.h>
3563783933SJohn Baldwin #include <string.h>
3663783933SJohn Baldwin #include <syslog.h>
3763783933SJohn Baldwin #include <vis.h>
3863783933SJohn Baldwin 
3963783933SJohn Baldwin #include "libiscsiutil.h"
4063783933SJohn Baldwin 
4163783933SJohn Baldwin static int log_level = 0;
4263783933SJohn Baldwin static char *peer_name = NULL;
4363783933SJohn Baldwin static char *peer_addr = NULL;
4463783933SJohn Baldwin 
4563783933SJohn Baldwin #define	MSGBUF_LEN	1024
4663783933SJohn Baldwin 
4763783933SJohn Baldwin void
log_init(int level)4863783933SJohn Baldwin log_init(int level)
4963783933SJohn Baldwin {
5063783933SJohn Baldwin 
5163783933SJohn Baldwin 	log_level = level;
5263783933SJohn Baldwin 	openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
5363783933SJohn Baldwin }
5463783933SJohn Baldwin 
5563783933SJohn Baldwin void
log_set_peer_name(const char * name)5663783933SJohn Baldwin log_set_peer_name(const char *name)
5763783933SJohn Baldwin {
5863783933SJohn Baldwin 
5963783933SJohn Baldwin 	/*
6063783933SJohn Baldwin 	 * XXX: Turn it into assertion?
6163783933SJohn Baldwin 	 */
6263783933SJohn Baldwin 	if (peer_name != NULL)
6363783933SJohn Baldwin 		log_errx(1, "%s called twice", __func__);
6463783933SJohn Baldwin 	if (peer_addr == NULL)
6563783933SJohn Baldwin 		log_errx(1, "%s called before log_set_peer_addr", __func__);
6663783933SJohn Baldwin 
6763783933SJohn Baldwin 	peer_name = checked_strdup(name);
6863783933SJohn Baldwin }
6963783933SJohn Baldwin 
7063783933SJohn Baldwin void
log_set_peer_addr(const char * addr)7163783933SJohn Baldwin log_set_peer_addr(const char *addr)
7263783933SJohn Baldwin {
7363783933SJohn Baldwin 
7463783933SJohn Baldwin 	/*
7563783933SJohn Baldwin 	 * XXX: Turn it into assertion?
7663783933SJohn Baldwin 	 */
7763783933SJohn Baldwin 	if (peer_addr != NULL)
7863783933SJohn Baldwin 		log_errx(1, "%s called twice", __func__);
7963783933SJohn Baldwin 
8063783933SJohn Baldwin 	peer_addr = checked_strdup(addr);
8163783933SJohn Baldwin }
8263783933SJohn Baldwin 
8363783933SJohn Baldwin static void
log_common(int priority,int log_errno,const char * fmt,va_list ap)8463783933SJohn Baldwin log_common(int priority, int log_errno, const char *fmt, va_list ap)
8563783933SJohn Baldwin {
8663783933SJohn Baldwin 	static char msgbuf[MSGBUF_LEN];
8763783933SJohn Baldwin 	static char msgbuf_strvised[MSGBUF_LEN * 4 + 1];
8863783933SJohn Baldwin 	char *errstr;
8963783933SJohn Baldwin 	int ret;
9063783933SJohn Baldwin 
9163783933SJohn Baldwin 	ret = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
9263783933SJohn Baldwin 	if (ret < 0) {
9363783933SJohn Baldwin 		fprintf(stderr, "%s: snprintf failed", getprogname());
9463783933SJohn Baldwin 		syslog(LOG_CRIT, "snprintf failed");
9563783933SJohn Baldwin 		exit(1);
9663783933SJohn Baldwin 	}
9763783933SJohn Baldwin 
9863783933SJohn Baldwin 	ret = strnvis(msgbuf_strvised, sizeof(msgbuf_strvised), msgbuf, VIS_NL);
9963783933SJohn Baldwin 	if (ret < 0) {
10063783933SJohn Baldwin 		fprintf(stderr, "%s: strnvis failed", getprogname());
10163783933SJohn Baldwin 		syslog(LOG_CRIT, "strnvis failed");
10263783933SJohn Baldwin 		exit(1);
10363783933SJohn Baldwin 	}
10463783933SJohn Baldwin 
10563783933SJohn Baldwin 	if (log_errno == -1) {
10663783933SJohn Baldwin 		if (peer_name != NULL) {
10763783933SJohn Baldwin 			fprintf(stderr, "%s: %s (%s): %s\n", getprogname(),
10863783933SJohn Baldwin 			    peer_addr, peer_name, msgbuf_strvised);
10963783933SJohn Baldwin 			syslog(priority, "%s (%s): %s",
11063783933SJohn Baldwin 			    peer_addr, peer_name, msgbuf_strvised);
11163783933SJohn Baldwin 		} else if (peer_addr != NULL) {
11263783933SJohn Baldwin 			fprintf(stderr, "%s: %s: %s\n", getprogname(),
11363783933SJohn Baldwin 			    peer_addr, msgbuf_strvised);
11463783933SJohn Baldwin 			syslog(priority, "%s: %s",
11563783933SJohn Baldwin 			    peer_addr, msgbuf_strvised);
11663783933SJohn Baldwin 		} else {
11763783933SJohn Baldwin 			fprintf(stderr, "%s: %s\n", getprogname(), msgbuf_strvised);
11863783933SJohn Baldwin 			syslog(priority, "%s", msgbuf_strvised);
11963783933SJohn Baldwin 		}
12063783933SJohn Baldwin 
12163783933SJohn Baldwin 	} else {
12263783933SJohn Baldwin 		errstr = strerror(log_errno);
12363783933SJohn Baldwin 
12463783933SJohn Baldwin 		if (peer_name != NULL) {
12563783933SJohn Baldwin 			fprintf(stderr, "%s: %s (%s): %s: %s\n", getprogname(),
12663783933SJohn Baldwin 			    peer_addr, peer_name, msgbuf_strvised, errstr);
12763783933SJohn Baldwin 			syslog(priority, "%s (%s): %s: %s",
12863783933SJohn Baldwin 			    peer_addr, peer_name, msgbuf_strvised, errstr);
12963783933SJohn Baldwin 		} else if (peer_addr != NULL) {
13063783933SJohn Baldwin 			fprintf(stderr, "%s: %s: %s: %s\n", getprogname(),
13163783933SJohn Baldwin 			    peer_addr, msgbuf_strvised, errstr);
13263783933SJohn Baldwin 			syslog(priority, "%s: %s: %s",
13363783933SJohn Baldwin 			    peer_addr, msgbuf_strvised, errstr);
13463783933SJohn Baldwin 		} else {
13563783933SJohn Baldwin 			fprintf(stderr, "%s: %s: %s\n", getprogname(),
13663783933SJohn Baldwin 			    msgbuf_strvised, errstr);
13763783933SJohn Baldwin 			syslog(priority, "%s: %s",
13863783933SJohn Baldwin 			    msgbuf_strvised, errstr);
13963783933SJohn Baldwin 		}
14063783933SJohn Baldwin 	}
14163783933SJohn Baldwin }
14263783933SJohn Baldwin 
14363783933SJohn Baldwin void
log_err(int eval,const char * fmt,...)14463783933SJohn Baldwin log_err(int eval, const char *fmt, ...)
14563783933SJohn Baldwin {
14663783933SJohn Baldwin 	va_list ap;
14763783933SJohn Baldwin 
14863783933SJohn Baldwin 	va_start(ap, fmt);
14963783933SJohn Baldwin 	log_common(LOG_CRIT, errno, fmt, ap);
15063783933SJohn Baldwin 	va_end(ap);
15163783933SJohn Baldwin 
15263783933SJohn Baldwin 	exit(eval);
15363783933SJohn Baldwin }
15463783933SJohn Baldwin 
15563783933SJohn Baldwin void
log_errc(int eval,int code,const char * fmt,...)156*de3deff6SJohn Baldwin log_errc(int eval, int code, const char *fmt, ...)
157*de3deff6SJohn Baldwin {
158*de3deff6SJohn Baldwin 	va_list ap;
159*de3deff6SJohn Baldwin 
160*de3deff6SJohn Baldwin 	va_start(ap, fmt);
161*de3deff6SJohn Baldwin 	log_common(LOG_CRIT, code, fmt, ap);
162*de3deff6SJohn Baldwin 	va_end(ap);
163*de3deff6SJohn Baldwin 
164*de3deff6SJohn Baldwin 	exit(eval);
165*de3deff6SJohn Baldwin }
166*de3deff6SJohn Baldwin 
167*de3deff6SJohn Baldwin void
log_errx(int eval,const char * fmt,...)16863783933SJohn Baldwin log_errx(int eval, const char *fmt, ...)
16963783933SJohn Baldwin {
17063783933SJohn Baldwin 	va_list ap;
17163783933SJohn Baldwin 
17263783933SJohn Baldwin 	va_start(ap, fmt);
17363783933SJohn Baldwin 	log_common(LOG_CRIT, -1, fmt, ap);
17463783933SJohn Baldwin 	va_end(ap);
17563783933SJohn Baldwin 
17663783933SJohn Baldwin 	exit(eval);
17763783933SJohn Baldwin }
17863783933SJohn Baldwin 
17963783933SJohn Baldwin void
log_warn(const char * fmt,...)18063783933SJohn Baldwin log_warn(const char *fmt, ...)
18163783933SJohn Baldwin {
18263783933SJohn Baldwin 	va_list ap;
18363783933SJohn Baldwin 
18463783933SJohn Baldwin 	va_start(ap, fmt);
18563783933SJohn Baldwin 	log_common(LOG_WARNING, errno, fmt, ap);
18663783933SJohn Baldwin 	va_end(ap);
18763783933SJohn Baldwin }
18863783933SJohn Baldwin 
18963783933SJohn Baldwin void
log_warnc(int code,const char * fmt,...)190*de3deff6SJohn Baldwin log_warnc(int code, const char *fmt, ...)
191*de3deff6SJohn Baldwin {
192*de3deff6SJohn Baldwin 	va_list ap;
193*de3deff6SJohn Baldwin 
194*de3deff6SJohn Baldwin 	va_start(ap, fmt);
195*de3deff6SJohn Baldwin 	log_common(LOG_WARNING, code, fmt, ap);
196*de3deff6SJohn Baldwin 	va_end(ap);
197*de3deff6SJohn Baldwin }
198*de3deff6SJohn Baldwin 
199*de3deff6SJohn Baldwin void
log_warnx(const char * fmt,...)20063783933SJohn Baldwin log_warnx(const char *fmt, ...)
20163783933SJohn Baldwin {
20263783933SJohn Baldwin 	va_list ap;
20363783933SJohn Baldwin 
20463783933SJohn Baldwin 	va_start(ap, fmt);
20563783933SJohn Baldwin 	log_common(LOG_WARNING, -1, fmt, ap);
20663783933SJohn Baldwin 	va_end(ap);
20763783933SJohn Baldwin }
20863783933SJohn Baldwin 
20963783933SJohn Baldwin void
log_debugx(const char * fmt,...)21063783933SJohn Baldwin log_debugx(const char *fmt, ...)
21163783933SJohn Baldwin {
21263783933SJohn Baldwin 	va_list ap;
21363783933SJohn Baldwin 
21463783933SJohn Baldwin 	if (log_level == 0)
21563783933SJohn Baldwin 		return;
21663783933SJohn Baldwin 
21763783933SJohn Baldwin 	va_start(ap, fmt);
21863783933SJohn Baldwin 	log_common(LOG_DEBUG, -1, fmt, ap);
21963783933SJohn Baldwin 	va_end(ap);
22063783933SJohn Baldwin }
221