xref: /netbsd-src/usr.sbin/mopd/common/log.c (revision 42adbd22c5da7aae434ed6560447a460aba3e2b5)
1*42adbd22Skalvisd /*	$NetBSD: log.c,v 1.5 2024/12/03 05:57:02 kalvisd Exp $	*/
226ae5011Schristos 
326ae5011Schristos /*-
426ae5011Schristos  * Copyright (c) 2003 The NetBSD Foundation, Inc.
526ae5011Schristos  * All rights reserved.
626ae5011Schristos  *
726ae5011Schristos  * This code is derived from software contributed to The NetBSD Foundation
826ae5011Schristos  * by Christos Zoulas.
926ae5011Schristos  *
1026ae5011Schristos  * Redistribution and use in source and binary forms, with or without
1126ae5011Schristos  * modification, are permitted provided that the following conditions
1226ae5011Schristos  * are met:
1326ae5011Schristos  * 1. Redistributions of source code must retain the above copyright
1426ae5011Schristos  *    notice, this list of conditions and the following disclaimer.
1526ae5011Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1626ae5011Schristos  *    notice, this list of conditions and the following disclaimer in the
1726ae5011Schristos  *    documentation and/or other materials provided with the distribution.
1826ae5011Schristos  *
1926ae5011Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2026ae5011Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2126ae5011Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2226ae5011Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2326ae5011Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2426ae5011Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2526ae5011Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2626ae5011Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2726ae5011Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2826ae5011Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2926ae5011Schristos  * POSSIBILITY OF SUCH DAMAGE.
3026ae5011Schristos  */
3126ae5011Schristos 
32*42adbd22Skalvisd #if defined (HAVE_NBTOOL_CONFIG_H)
33*42adbd22Skalvisd # include "nbtool_config.h"
34*42adbd22Skalvisd #else
35cdd21bd3Schristos # include "port.h"
36*42adbd22Skalvisd #endif /* defined (HAVE_NBTOOL_CONFIG_H) */
3726ae5011Schristos #ifndef lint
38*42adbd22Skalvisd __RCSID("$NetBSD: log.c,v 1.5 2024/12/03 05:57:02 kalvisd Exp $");
3926ae5011Schristos #endif
4026ae5011Schristos 
4126ae5011Schristos #include <err.h>
4226ae5011Schristos #include <stdarg.h>
4326ae5011Schristos #include "os.h"
4426ae5011Schristos #include "mopdef.h"
4526ae5011Schristos #include "log.h"
4626ae5011Schristos 
4726ae5011Schristos int mopInteractive = 0;
4826ae5011Schristos 
4926ae5011Schristos void
5026ae5011Schristos mopLogErr(const char *fmt, ...)
5126ae5011Schristos {
5226ae5011Schristos 	va_list ap;
5326ae5011Schristos 	char buf[1024];
54d7888147Sdholland 	int error;
5526ae5011Schristos 
5626ae5011Schristos 	va_start(ap, fmt);
5726ae5011Schristos 	if (mopInteractive)
5826ae5011Schristos 		verr(1, fmt, ap);
5926ae5011Schristos 	else {
60d7888147Sdholland 		error = errno;
61d7888147Sdholland 		vsnprintf(buf, sizeof(buf), fmt, ap);
62d7888147Sdholland 		syslog(LOG_ERR, "%s: %s", buf, strerror(error));
6326ae5011Schristos 	}
6426ae5011Schristos 	va_end(ap);
6526ae5011Schristos 	exit(1);
6626ae5011Schristos }
6726ae5011Schristos 
6826ae5011Schristos void
6926ae5011Schristos mopLogWarn(const char *fmt, ...)
7026ae5011Schristos {
7126ae5011Schristos 	va_list ap;
7226ae5011Schristos 	char buf[1024];
73d7888147Sdholland 	int error;
7426ae5011Schristos 
7526ae5011Schristos 	va_start(ap, fmt);
7626ae5011Schristos 	if (mopInteractive)
7726ae5011Schristos 		vwarn(fmt, ap);
7826ae5011Schristos 	else {
79d7888147Sdholland 		error = errno;
80d7888147Sdholland 		vsnprintf(buf, sizeof(buf), fmt, ap);
81d7888147Sdholland 		syslog(LOG_WARNING, "%s: %s", buf, strerror(error));
8226ae5011Schristos 	}
8326ae5011Schristos 	va_end(ap);
8426ae5011Schristos }
8526ae5011Schristos 
8626ae5011Schristos void
8726ae5011Schristos mopLogErrX(const char *fmt, ...)
8826ae5011Schristos {
8926ae5011Schristos 	va_list ap;
9026ae5011Schristos 
9126ae5011Schristos 	va_start(ap, fmt);
9226ae5011Schristos 	if (mopInteractive)
9326ae5011Schristos 		verrx(1, fmt, ap);
9426ae5011Schristos 	else
9526ae5011Schristos 		vsyslog(LOG_ERR, fmt, ap);
9626ae5011Schristos 	va_end(ap);
9726ae5011Schristos 	exit(1);
9826ae5011Schristos }
9926ae5011Schristos 
10026ae5011Schristos void
10126ae5011Schristos mopLogWarnX(const char *fmt, ...)
10226ae5011Schristos {
10326ae5011Schristos 	va_list ap;
10426ae5011Schristos 
10526ae5011Schristos 	va_start(ap, fmt);
10626ae5011Schristos 	if (mopInteractive)
10726ae5011Schristos 		vwarnx(fmt, ap);
10826ae5011Schristos 	else
10926ae5011Schristos 		vsyslog(LOG_WARNING, fmt, ap);
11026ae5011Schristos 	va_end(ap);
11126ae5011Schristos }
112