Commit Diff


commit - f74e6bb744285abe3049d43d7fd7a11a2faaf48f
commit + 0db0ef5902441ddb290701caa77944b93fbb582a
blob - 179235943ad862e0426e472d7b0922bf3fef0371
blob + b7c41e52ddb39fcf8c001ab114ba496ecef31ecf
--- log.c
+++ log.c
@@ -1,197 +1,169 @@
-/*	$OpenBSD: log.c,v 1.1 2018/07/10 16:39:54 florian Exp $	*/
-
 /*
- * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
+ * This is free and unencumbered software released into the public domain.
  *
- * 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.
+ * Anyone is free to copy, modify, publish, use, compile, sell, or
+ * distribute this software, either in source code form or as a compiled
+ * binary, for any purpose, commercial or non-commercial, and by any
+ * means.
  */
 
+#include <err.h>
+#include <errno.h>
+#include <stdarg.h>
 #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;
-static const char	*log_procname;
+__dead void	log_syslog_fatal(int, const char *, ...);
+__dead void	log_syslog_fatalx(int, const char *, ...);
+void		log_syslog_warn(const char *, ...);
+void		log_syslog_warnx(const char *, ...);
+void		log_syslog_info(const char *, ...);
+void		log_syslog_debug(const char *, ...);
 
+const struct logger syslogger = {
+	.fatal =	&log_syslog_fatal,
+	.fatalx =	&log_syslog_fatalx,
+	.warn =		&log_syslog_warn,
+	.warnx =	&log_syslog_warnx,
+	.info =		&log_syslog_info,
+	.debug =	&log_syslog_debug,
+};
+
+const struct logger dbglogger = {
+	.fatal =	&err,
+	.fatalx =	&errx,
+	.warn =		&warn,
+	.warnx =	&warnx,
+	.info =		&warnx,
+	.debug =	&warnx,
+};
+
+const struct logger *logger = &dbglogger;
+
+static char logbuf[4096];
+static int debug;
+static int verbose;
+
 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();
+	if (debug)
+		setvbuf(stderr, logbuf, _IOLBF, sizeof(logbuf));
+	else {
+		openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
+		logger = &syslogger;
+	}
 }
 
 void
-log_procinit(const char *procname)
-{
-	if (procname != NULL)
-		log_procname = procname;
-}
-
-void
 log_setverbose(int v)
 {
 	verbose = v;
 }
 
-int
-log_getverbose(void)
+__dead void
+log_syslog_fatal(int eval, const char *fmt, ...)
 {
-	return (verbose);
-}
+	static char	 s[BUFSIZ];
+	va_list		 ap;
+	int		 r, save_errno;
 
-void
-logit(int pri, const char *fmt, ...)
-{
-	va_list	ap;
+	save_errno = errno;
 
 	va_start(ap, fmt);
-	vlog(pri, fmt, ap);
+	r = vsnprintf(s, sizeof(s), fmt, ap);
 	va_end(ap);
-}
 
-void
-vlog(int pri, const char *fmt, va_list ap)
-{
-	char	*nfmt;
-	int	 saved_errno = errno;
+	errno = save_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);
+	if (r > 0 && (size_t)r <= sizeof(s))
+		syslog(LOG_DAEMON|LOG_CRIT, "%s: %s", s, strerror(errno));
 
-	errno = saved_errno;
+	exit(eval);
 }
 
-void
-log_warn(const char *emsg, ...)
+__dead void
+log_syslog_fatalx(int eval, const char *fmt, ...)
 {
-	char		*nfmt;
 	va_list		 ap;
-	int		 saved_errno = errno;
 
-	/* best effort to even work in out of memory situations */
-	if (emsg == NULL)
-		logit(LOG_ERR, "%s", strerror(saved_errno));
-	else {
-		va_start(ap, emsg);
+	va_start(ap, fmt);
+	vsyslog(LOG_DAEMON|LOG_CRIT, fmt, ap);
+	va_end(ap);
 
-		if (asprintf(&nfmt, "%s: %s", emsg,
-		    strerror(saved_errno)) == -1) {
-			/* we tried it... */
-			vlog(LOG_ERR, emsg, ap);
-			logit(LOG_ERR, "%s", strerror(saved_errno));
-		} else {
-			vlog(LOG_ERR, nfmt, ap);
-			free(nfmt);
-		}
-		va_end(ap);
-	}
-
-	errno = saved_errno;
+	exit(eval);
 }
 
 void
-log_warnx(const char *emsg, ...)
+log_syslog_warn(const char *fmt, ...)
 {
-	va_list	 ap;
+	static char	 s[BUFSIZ];
+	va_list		 ap;
+	int		 r, save_errno;
 
-	va_start(ap, emsg);
-	vlog(LOG_ERR, emsg, ap);
+	save_errno = errno;
+
+	va_start(ap, fmt);
+	r = vsnprintf(s, sizeof(s), fmt, ap);
 	va_end(ap);
-}
 
-void
-log_info(const char *emsg, ...)
-{
-	va_list	 ap;
+	errno = save_errno;
 
-	va_start(ap, emsg);
-	vlog(LOG_INFO, emsg, ap);
-	va_end(ap);
+	if (r > 0 && (size_t)r < sizeof(s))
+		syslog(LOG_DAEMON|LOG_ERR, "%s: %s", s, strerror(errno));
+
+	errno = save_errno;
 }
 
 void
-log_debug(const char *emsg, ...)
+log_syslog_warnx(const char *fmt, ...)
 {
-	va_list	 ap;
+	va_list		 ap;
+	int		 save_errno;
 
-	if (verbose) {
-		va_start(ap, emsg);
-		vlog(LOG_DEBUG, emsg, ap);
-		va_end(ap);
-	}
+	save_errno = errno;
+	va_start(ap, fmt);
+	vsyslog(LOG_DAEMON|LOG_ERR, fmt, ap);
+	va_end(ap);
+	errno = save_errno;
 }
 
-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, "fatal in %s: %s%s%s",
-		    log_procname, s, sep, strerror(code));
-	else
-		logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
-}
-
 void
-fatal(const char *emsg, ...)
+log_syslog_info(const char *fmt, ...)
 {
-	va_list	ap;
+	va_list		 ap;
+	int		 save_errno;
 
-	va_start(ap, emsg);
-	vfatalc(errno, emsg, ap);
+	if (verbose < 1)
+		return;
+
+	save_errno = errno;
+	va_start(ap, fmt);
+	vsyslog(LOG_DAEMON|LOG_INFO, fmt, ap);
 	va_end(ap);
-	exit(1);
+	errno = save_errno;
 }
 
 void
-fatalx(const char *emsg, ...)
+log_syslog_debug(const char *fmt, ...)
 {
-	va_list	ap;
+	va_list		 ap;
+	int		 save_errno;
 
-	va_start(ap, emsg);
-	vfatalc(0, emsg, ap);
+	if (verbose < 2)
+		return;
+
+	save_errno = errno;
+	va_start(ap, fmt);
+	vsyslog(LOG_DAEMON|LOG_DEBUG, fmt, ap);
 	va_end(ap);
-	exit(1);
+	errno = save_errno;
 }
blob - 0fa046fc3afbe8c893d40f492481a4d3055dd2bc
blob + 4e26c5293dfc7e06848e76380e90711193e81e52
--- log.h
+++ log.h
@@ -1,45 +1,31 @@
-/*	$OpenBSD: log.h,v 1.2 2021/12/13 18:28:40 deraadt Exp $ */
-
 /*
- * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
+ * This is free and unencumbered software released into the public domain.
  *
- * 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.
+ * Anyone is free to copy, modify, publish, use, compile, sell, or
+ * distribute this software, either in source code form or as a compiled
+ * binary, for any purpose, commercial or non-commercial, and by any
+ * means.
  */
 
-#ifndef LOG_H
-#define LOG_H
+#define LOG_ATTR_PRINTF(A, B) __attribute__((__format__ (printf, A, B)))
+struct logger {
+	__dead void (*fatal)(int, const char *, ...)	LOG_ATTR_PRINTF(2, 3);
+	__dead void (*fatalx)(int, const char *, ...)	LOG_ATTR_PRINTF(2, 3);
+	void (*warn)(const char *, ...)			LOG_ATTR_PRINTF(1, 2);
+	void (*warnx)(const char *, ...)		LOG_ATTR_PRINTF(1, 2);
+	void (*info)(const char *, ...)			LOG_ATTR_PRINTF(1, 2);
+	void (*debug)(const char *, ...)		LOG_ATTR_PRINTF(1, 2);
+};
+#undef LOG_ATTR_PRINTF
 
-#include <stdarg.h>
+extern const struct logger *logger, syslogger, dbglogger;
 
+#define fatal(...)	logger->fatal(1, __VA_ARGS__)
+#define fatalx(...)	logger->fatalx(1, __VA_ARGS__)
+#define log_warn(...)	logger->warn(__VA_ARGS__)
+#define log_warnx(...)	logger->warnx(__VA_ARGS__)
+#define log_info(...)	logger->info(__VA_ARGS__)
+#define log_debug(...)	logger->debug(__VA_ARGS__)
+
 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)));
-
-#endif /* LOG_H */