"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
gotd/gotwebd: unify log.c
To:
gameoftrees@openbsd.org
Date:
Mon, 20 May 2024 16:03:34 +0200

Download raw body.

Thread
gotd and gotwebd are using one duplicate each of log.c, so move it in
lib/ and use it in both projects.

This is just a mechanical diff, in a follow-up I'd like to tweak log.c a
bit so that, for e.g., log_debug is logged only with -vv and log_info
with -v, and finally review gotwebd and gotd log_* usage.

(for e.g. fcgi tracing with log_debug visible under -v is a bit annoying.)

ok?

diff /home/op/w/got
commit - c6458e88f5a9085ec9206a60b93a713138b9b2fa
path + /home/op/w/got
blob - 14609f4960c9b8d6580769df8494ba5c3d482139
file + /dev/null
--- gotd/log.c
+++ /dev/null
@@ -1,194 +0,0 @@
-/*
- * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#include <syslog.h>
-#include <errno.h>
-#include <time.h>
-
-#include "log.h"
-
-static int	 debug;
-static int	 verbose;
-const char	*log_procname;
-
-void
-log_init(int n_debug, int facility)
-{
-	debug = n_debug;
-	verbose = n_debug;
-	log_procinit(getprogname());
-
-	if (!debug)
-		openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
-
-	tzset();
-}
-
-void
-log_procinit(const char *procname)
-{
-	if (procname != NULL)
-		log_procname = procname;
-}
-
-void
-log_setverbose(int v)
-{
-	verbose = v;
-}
-
-int
-log_getverbose(void)
-{
-	return (verbose);
-}
-
-void
-logit(int pri, const char *fmt, ...)
-{
-	va_list ap;
-
-	va_start(ap, fmt);
-	vlog(pri, fmt, ap);
-	va_end(ap);
-}
-
-void
-vlog(int pri, const char *fmt, va_list ap)
-{
-	char *nfmt;
-	int saved_errno = errno;
-
-	if (debug) {
-		/* best effort in out of mem situations */
-		if (asprintf(&nfmt, "%s: %s\n", log_procname, fmt) == -1) {
-			vfprintf(stderr, fmt, ap);
-			fprintf(stderr, "\n");
-		} else {
-			vfprintf(stderr, nfmt, ap);
-			free(nfmt);
-		}
-		fflush(stderr);
-	} else
-		vsyslog(pri, fmt, ap);
-
-	errno = saved_errno;
-}
-
-void
-log_warn(const char *emsg, ...)
-{
-	char *nfmt;
-	va_list ap;
-	int saved_errno = errno;
-
-	/* best effort to even work in out of memory situations */
-	if (emsg == NULL)
-		logit(LOG_CRIT, "%s", strerror(saved_errno));
-	else {
-		va_start(ap, emsg);
-
-		if (asprintf(&nfmt, "%s: %s", emsg,
-		    strerror(saved_errno)) == -1) {
-			/* we tried it... */
-			vlog(LOG_CRIT, emsg, ap);
-			logit(LOG_CRIT, "%s", strerror(saved_errno));
-		} else {
-			vlog(LOG_CRIT, nfmt, ap);
-			free(nfmt);
-		}
-		va_end(ap);
-	}
-
-	errno = saved_errno;
-}
-
-void
-log_warnx(const char *emsg, ...)
-{
-	va_list ap;
-
-	va_start(ap, emsg);
-	vlog(LOG_CRIT, emsg, ap);
-	va_end(ap);
-}
-
-void
-log_info(const char *emsg, ...)
-{
-	va_list ap;
-
-	va_start(ap, emsg);
-	vlog(LOG_INFO, emsg, ap);
-	va_end(ap);
-}
-
-void
-log_debug(const char *emsg, ...)
-{
-	va_list ap;
-
-	if (verbose) {
-		va_start(ap, emsg);
-		vlog(LOG_DEBUG, emsg, ap);
-		va_end(ap);
-	}
-}
-
-static void
-vfatalc(int code, const char *emsg, va_list ap)
-{
-	static char s[BUFSIZ];
-	const char *sep;
-
-	if (emsg != NULL) {
-		(void)vsnprintf(s, sizeof(s), emsg, ap);
-		sep = ": ";
-	} else {
-		s[0] = '\0';
-		sep = "";
-	}
-	if (code)
-		logit(LOG_CRIT, "%s%s%s", s, sep, strerror(code));
-	else
-		logit(LOG_CRIT, "%s", s);
-}
-
-void
-fatal(const char *emsg, ...)
-{
-	va_list ap;
-
-	va_start(ap, emsg);
-	vfatalc(errno, emsg, ap);
-	va_end(ap);
-	exit(1);
-}
-
-void
-fatalx(const char *emsg, ...)
-{
-	va_list ap;
-
-	va_start(ap, emsg);
-	vfatalc(0, emsg, ap);
-	va_end(ap);
-	exit(1);
-}
blob - fff6f87ffd0edd1a69a1db346fad051aadc39c21
file + /dev/null
--- gotd/log.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-void	log_init(int, int);
-void	log_procinit(const char *);
-void	log_setverbose(int);
-int	log_getverbose(void);
-void	log_warn(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	log_warnx(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	log_info(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	log_debug(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	logit(int, const char *, ...)
-	    __attribute__((__format__ (printf, 2, 3)));
-void	vlog(int, const char *, va_list)
-	    __attribute__((__format__ (printf, 2, 0)));
-__dead void fatal(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-__dead void fatalx(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
blob - 258caeda8f63d90382ebfba64f426fcbc541a389
file + gotwebd/Makefile
--- gotwebd/Makefile
+++ gotwebd/Makefile
@@ -7,7 +7,7 @@ SUBDIR = libexec
 .include "Makefile.inc"
 
 PROG =		gotwebd
-SRCS =		config.c sockets.c log.c gotwebd.c parse.y \
+SRCS =		config.c sockets.c gotwebd.c parse.y \
 		fcgi.c gotweb.c got_operations.c tmpl.c pages.c
 SRCS +=		blame.c commit_graph.c delta.c diff.c \
 		diffreg.c error.c object.c object_cache.c \
@@ -21,7 +21,7 @@ SRCS +=		blame.c commit_graph.c delta.c diff.c \
 		sigs.c date.c \
 		object_open_privsep.c read_gitconfig_privsep.c \
 		read_gotconfig_privsep.c pollfd.c reference_parse.c \
-		object_qid.c
+		object_qid.c log.c
 
 .if exists(${.CURDIR}/../template/obj/template)
 TEMPLATE = ${.CURDIR}/../template/obj/template
blob - 39545f5ca4d24bed17282d23968aa2a919a327fc
file + gotwebd/config.c
--- gotwebd/config.c
+++ gotwebd/config.c
@@ -40,6 +40,7 @@
 #include "got_reference.h"
 
 #include "gotwebd.h"
+#include "log.h"
 
 int
 config_init(struct gotwebd *env)
blob - 202f1af662fe84b6759cc1a1c53efca43470da98
file + gotwebd/fcgi.c
--- gotwebd/fcgi.c
+++ gotwebd/fcgi.c
@@ -36,6 +36,7 @@
 #include "got_reference.h"
 
 #include "gotwebd.h"
+#include "log.h"
 #include "tmpl.h"
 
 size_t	 fcgi_parse_record(uint8_t *, size_t, struct request *);
blob - 5bcdf61f67c447df889e0dce4ed81ce7e39d19e0
file + gotwebd/got_operations.c
--- gotwebd/got_operations.c
+++ gotwebd/got_operations.c
@@ -40,6 +40,7 @@
 #include "got_privsep.h"
 
 #include "gotwebd.h"
+#include "log.h"
 
 static const struct got_error *got_init_repo_commit(struct repo_commit **);
 static const struct got_error *got_init_repo_tag(struct repo_tag **);
blob - 629ddbf12941ad82fb3f24aba94fc8ddb599969f
file + gotwebd/gotweb.c
--- gotwebd/gotweb.c
+++ gotwebd/gotweb.c
@@ -50,6 +50,7 @@
 #include "got_privsep.h"
 
 #include "gotwebd.h"
+#include "log.h"
 #include "tmpl.h"
 
 static const struct querystring_keys querystring_keys[] = {
blob - 449626bea5142dd28150d6b7d4f526712840e040
file + gotwebd/gotwebd.c
--- gotwebd/gotwebd.c
+++ gotwebd/gotwebd.c
@@ -43,6 +43,7 @@
 #include "got_reference.h"
 
 #include "gotwebd.h"
+#include "log.h"
 
 __dead void usage(void);
 
blob - 79be23fd4a96aa9933e08e7578e6a97b51d844b8
file + gotwebd/gotwebd.h
--- gotwebd/gotwebd.h
+++ gotwebd/gotwebd.h
@@ -24,12 +24,6 @@
 #include <limits.h>
 #include <stdio.h>
 
-#ifdef DEBUG
-#define dprintf(x...)   do { log_debug(x); } while(0)
-#else
-#define dprintf(x...)
-#endif /* DEBUG */
-
 #ifndef nitems
 #define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
 #endif
@@ -521,25 +515,3 @@ int config_setfd(struct gotwebd *, struct socket *);
 int config_getfd(struct gotwebd *, struct imsg *);
 int config_getcfg(struct gotwebd *, struct imsg *);
 int config_init(struct gotwebd *);
-
-/* log.c */
-void	log_init(int, int);
-void	log_procinit(const char *);
-void	log_setverbose(int);
-int	log_getverbose(void);
-void	log_warn(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	log_warnx(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	log_info(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	log_debug(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	logit(int, const char *, ...)
-	    __attribute__((__format__ (printf, 2, 3)));
-void	vlog(int, const char *, va_list)
-	    __attribute__((__format__ (printf, 2, 0)));
-__dead void fatal(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-__dead void fatalx(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
blob - 73cbf47a766ba764769018c3e5833cb0414ed1f8
file + /dev/null
--- gotwebd/log.c
+++ /dev/null
@@ -1,214 +0,0 @@
-/*
- * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#include <syslog.h>
-#include <errno.h>
-#include <time.h>
-
-static int	 debug;
-static int	 verbose;
-const char	*log_procname;
-
-void	log_init(int, int);
-void	log_procinit(const char *);
-void	log_setverbose(int);
-int	log_getverbose(void);
-void	log_warn(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	log_warnx(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	log_info(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	log_debug(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-void	logit(int, const char *, ...)
-	    __attribute__((__format__ (printf, 2, 3)));
-void	vlog(int, const char *, va_list)
-	    __attribute__((__format__ (printf, 2, 0)));
-__dead void fatal(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-__dead void fatalx(const char *, ...)
-	    __attribute__((__format__ (printf, 1, 2)));
-
-void
-log_init(int n_debug, int facility)
-{
-	debug = n_debug;
-	verbose = n_debug;
-	log_procinit(getprogname());
-
-	if (!debug)
-		openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
-
-	tzset();
-}
-
-void
-log_procinit(const char *procname)
-{
-	if (procname != NULL)
-		log_procname = procname;
-}
-
-void
-log_setverbose(int v)
-{
-	verbose = v;
-}
-
-int
-log_getverbose(void)
-{
-	return (verbose);
-}
-
-void
-logit(int pri, const char *fmt, ...)
-{
-	va_list ap;
-
-	va_start(ap, fmt);
-	vlog(pri, fmt, ap);
-	va_end(ap);
-}
-
-void
-vlog(int pri, const char *fmt, va_list ap)
-{
-	char *nfmt;
-	int saved_errno = errno;
-
-	if (debug) {
-		/* best effort in out of mem situations */
-		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
-			vfprintf(stderr, fmt, ap);
-			fprintf(stderr, "\n");
-		} else {
-			vfprintf(stderr, nfmt, ap);
-			free(nfmt);
-		}
-		fflush(stderr);
-	} else
-		vsyslog(pri, fmt, ap);
-
-	errno = saved_errno;
-}
-
-void
-log_warn(const char *emsg, ...)
-{
-	char *nfmt;
-	va_list ap;
-	int saved_errno = errno;
-
-	/* best effort to even work in out of memory situations */
-	if (emsg == NULL)
-		logit(LOG_CRIT, "%s", strerror(saved_errno));
-	else {
-		va_start(ap, emsg);
-
-		if (asprintf(&nfmt, "%s: %s", emsg,
-		    strerror(saved_errno)) == -1) {
-			/* we tried it... */
-			vlog(LOG_CRIT, emsg, ap);
-			logit(LOG_CRIT, "%s", strerror(saved_errno));
-		} else {
-			vlog(LOG_CRIT, nfmt, ap);
-			free(nfmt);
-		}
-		va_end(ap);
-	}
-
-	errno = saved_errno;
-}
-
-void
-log_warnx(const char *emsg, ...)
-{
-	va_list ap;
-
-	va_start(ap, emsg);
-	vlog(LOG_CRIT, emsg, ap);
-	va_end(ap);
-}
-
-void
-log_info(const char *emsg, ...)
-{
-	va_list ap;
-
-	va_start(ap, emsg);
-	vlog(LOG_INFO, emsg, ap);
-	va_end(ap);
-}
-
-void
-log_debug(const char *emsg, ...)
-{
-	va_list ap;
-
-	if (verbose) {
-		va_start(ap, emsg);
-		vlog(LOG_DEBUG, emsg, ap);
-		va_end(ap);
-	}
-}
-
-static void
-vfatalc(int code, const char *emsg, va_list ap)
-{
-	static char s[BUFSIZ];
-	const char *sep;
-
-	if (emsg != NULL) {
-		(void)vsnprintf(s, sizeof(s), emsg, ap);
-		sep = ": ";
-	} else {
-		s[0] = '\0';
-		sep = "";
-	}
-	if (code)
-		logit(LOG_CRIT, "%s: %s%s%s",
-		    log_procname, s, sep, strerror(code));
-	else
-		logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
-}
-
-void
-fatal(const char *emsg, ...)
-{
-	va_list ap;
-
-	va_start(ap, emsg);
-	vfatalc(errno, emsg, ap);
-	va_end(ap);
-	exit(1);
-}
-
-void
-fatalx(const char *emsg, ...)
-{
-	va_list ap;
-
-	va_start(ap, emsg);
-	vfatalc(0, emsg, ap);
-	va_end(ap);
-	exit(1);
-}
blob - d6f39ddd87489a5be6dceeaae758836ca25385f3
file + gotwebd/pages.tmpl
--- gotwebd/pages.tmpl
+++ gotwebd/pages.tmpl
@@ -35,6 +35,7 @@
 #include "got_reference.h"
 
 #include "gotwebd.h"
+#include "log.h"
 #include "tmpl.h"
 
 enum gotweb_ref_tm {
blob - 90f1dfb2d5d488e436389ece78e9cebf0e84a4d5
file + gotwebd/parse.y
--- gotwebd/parse.y
+++ gotwebd/parse.y
@@ -51,6 +51,7 @@
 #include "got_reference.h"
 
 #include "gotwebd.h"
+#include "log.h"
 
 TAILQ_HEAD(files, file)		 files = TAILQ_HEAD_INITIALIZER(files);
 static struct file {
blob - fd3c5d9c75c9ebd4b6883e0fefaa27c558b4d263
file + gotwebd/sockets.c
--- gotwebd/sockets.c
+++ gotwebd/sockets.c
@@ -56,6 +56,7 @@
 #include "got_privsep.h"
 
 #include "gotwebd.h"
+#include "log.h"
 #include "tmpl.h"
 
 #define SOCKS_BACKLOG 5
blob - /dev/null
file + lib/log.c (mode 644)
--- /dev/null
+++ lib/log.c
@@ -0,0 +1,194 @@
+/*
+ * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <syslog.h>
+#include <errno.h>
+#include <time.h>
+
+#include "log.h"
+
+static int	 debug;
+static int	 verbose;
+const char	*log_procname;
+
+void
+log_init(int n_debug, int facility)
+{
+	debug = n_debug;
+	verbose = n_debug;
+	log_procinit(getprogname());
+
+	if (!debug)
+		openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
+
+	tzset();
+}
+
+void
+log_procinit(const char *procname)
+{
+	if (procname != NULL)
+		log_procname = procname;
+}
+
+void
+log_setverbose(int v)
+{
+	verbose = v;
+}
+
+int
+log_getverbose(void)
+{
+	return (verbose);
+}
+
+void
+logit(int pri, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	vlog(pri, fmt, ap);
+	va_end(ap);
+}
+
+void
+vlog(int pri, const char *fmt, va_list ap)
+{
+	char *nfmt;
+	int saved_errno = errno;
+
+	if (debug) {
+		/* best effort in out of mem situations */
+		if (asprintf(&nfmt, "%s: %s\n", log_procname, fmt) == -1) {
+			vfprintf(stderr, fmt, ap);
+			fprintf(stderr, "\n");
+		} else {
+			vfprintf(stderr, nfmt, ap);
+			free(nfmt);
+		}
+		fflush(stderr);
+	} else
+		vsyslog(pri, fmt, ap);
+
+	errno = saved_errno;
+}
+
+void
+log_warn(const char *emsg, ...)
+{
+	char *nfmt;
+	va_list ap;
+	int saved_errno = errno;
+
+	/* best effort to even work in out of memory situations */
+	if (emsg == NULL)
+		logit(LOG_CRIT, "%s", strerror(saved_errno));
+	else {
+		va_start(ap, emsg);
+
+		if (asprintf(&nfmt, "%s: %s", emsg,
+		    strerror(saved_errno)) == -1) {
+			/* we tried it... */
+			vlog(LOG_CRIT, emsg, ap);
+			logit(LOG_CRIT, "%s", strerror(saved_errno));
+		} else {
+			vlog(LOG_CRIT, nfmt, ap);
+			free(nfmt);
+		}
+		va_end(ap);
+	}
+
+	errno = saved_errno;
+}
+
+void
+log_warnx(const char *emsg, ...)
+{
+	va_list ap;
+
+	va_start(ap, emsg);
+	vlog(LOG_CRIT, emsg, ap);
+	va_end(ap);
+}
+
+void
+log_info(const char *emsg, ...)
+{
+	va_list ap;
+
+	va_start(ap, emsg);
+	vlog(LOG_INFO, emsg, ap);
+	va_end(ap);
+}
+
+void
+log_debug(const char *emsg, ...)
+{
+	va_list ap;
+
+	if (verbose) {
+		va_start(ap, emsg);
+		vlog(LOG_DEBUG, emsg, ap);
+		va_end(ap);
+	}
+}
+
+static void
+vfatalc(int code, const char *emsg, va_list ap)
+{
+	static char s[BUFSIZ];
+	const char *sep;
+
+	if (emsg != NULL) {
+		(void)vsnprintf(s, sizeof(s), emsg, ap);
+		sep = ": ";
+	} else {
+		s[0] = '\0';
+		sep = "";
+	}
+	if (code)
+		logit(LOG_CRIT, "%s%s%s", s, sep, strerror(code));
+	else
+		logit(LOG_CRIT, "%s", s);
+}
+
+void
+fatal(const char *emsg, ...)
+{
+	va_list ap;
+
+	va_start(ap, emsg);
+	vfatalc(errno, emsg, ap);
+	va_end(ap);
+	exit(1);
+}
+
+void
+fatalx(const char *emsg, ...)
+{
+	va_list ap;
+
+	va_start(ap, emsg);
+	vfatalc(0, emsg, ap);
+	va_end(ap);
+	exit(1);
+}
blob - /dev/null
file + lib/log.h (mode 644)
--- /dev/null
+++ lib/log.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+void	log_init(int, int);
+void	log_procinit(const char *);
+void	log_setverbose(int);
+int	log_getverbose(void);
+void	log_warn(const char *, ...)
+	    __attribute__((__format__ (printf, 1, 2)));
+void	log_warnx(const char *, ...)
+	    __attribute__((__format__ (printf, 1, 2)));
+void	log_info(const char *, ...)
+	    __attribute__((__format__ (printf, 1, 2)));
+void	log_debug(const char *, ...)
+	    __attribute__((__format__ (printf, 1, 2)));
+void	logit(int, const char *, ...)
+	    __attribute__((__format__ (printf, 2, 3)));
+void	vlog(int, const char *, va_list)
+	    __attribute__((__format__ (printf, 2, 0)));
+__dead void fatal(const char *, ...)
+	    __attribute__((__format__ (printf, 1, 2)));
+__dead void fatalx(const char *, ...)
+	    __attribute__((__format__ (printf, 1, 2)));