-/* $OpenBSD: log.c,v 1.9 2014/11/03 20:15:30 bluhm Exp $ */
+/* $OpenBSD: log.c,v 1.10 2015/01/08 00:30:08 bcook Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <sys/socket.h>
+
#include <errno.h>
+#include <netdb.h>
+#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <time.h>
-#include "ntpd.h"
+#include "log.h"
+
+#define TRACE_DEBUG 0x1
+
+static int foreground;
+static int verbose;
+
+void vlog(int, const char *, va_list);
+void logit(int, const char *, ...)
+ __attribute__((format (printf, 2, 3)));
-int debug;
-extern int debugsyslog;
void
-log_init(int n_debug)
+log_init(int n_foreground)
{
extern char *__progname;
- debug = n_debug;
-
- if (!debug)
- openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
+ foreground = n_foreground;
+ if (! foreground)
+ openlog(__progname, LOG_PID | LOG_NDELAY, LOG_MAIL);
tzset();
}
+void
+log_verbose(int v)
+{
+ verbose = v;
+}
+
void
logit(int pri, const char *fmt, ...)
{
{
char *nfmt;
- if (debug) {
+ if (foreground) {
/* best effort in out of mem situations */
if (asprintf(&nfmt, "%s\n", fmt) == -1) {
vfprintf(stderr, fmt, ap);
{
va_list ap;
- if (debug || debugsyslog) {
+ if (verbose & TRACE_DEBUG) {
va_start(ap, emsg);
vlog(LOG_DEBUG, emsg, ap);
va_end(ap);
}
void
-fatal(const char *emsg)
+log_trace(int mask, const char *emsg, ...)
+{
+ va_list ap;
+
+ if (verbose & mask) {
+ va_start(ap, emsg);
+ vlog(LOG_DEBUG, emsg, ap);
+ va_end(ap);
+ }
+}
+
+static void
+fatal_arg(const char *emsg, va_list ap)
{
+#define FATALBUFSIZE 1024
+ static char ebuffer[FATALBUFSIZE];
+
if (emsg == NULL)
- logit(LOG_CRIT, "fatal: %s", strerror(errno));
- else
- if (errno)
- logit(LOG_CRIT, "fatal: %s: %s",
- emsg, strerror(errno));
+ (void)strlcpy(ebuffer, strerror(errno), sizeof ebuffer);
+ else {
+ if (errno) {
+ (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap);
+ (void)strlcat(ebuffer, ": ", sizeof ebuffer);
+ (void)strlcat(ebuffer, strerror(errno), sizeof ebuffer);
+ }
else
- logit(LOG_CRIT, "fatal: %s", emsg);
+ (void)vsnprintf(ebuffer, sizeof ebuffer, emsg, ap);
+ }
+ logit(LOG_CRIT, "fatal: %s", ebuffer);
+}
+void
+fatal(const char *emsg, ...)
+{
+ va_list ap;
+
+ va_start(ap, emsg);
+ fatal_arg(emsg, ap);
+ va_end(ap);
exit(1);
}
void
-fatalx(const char *emsg)
+fatalx(const char *emsg, ...)
{
+ va_list ap;
+
errno = 0;
- fatal(emsg);
+ va_start(ap, emsg);
+ fatal_arg(emsg, ap);
+ va_end(ap);
+ exit(1);
}
const char *
--- /dev/null
+/* $OpenBSD: log.h,v 1.1 2015/01/08 00:30:08 bcook Exp $ */
+
+/*
+ * Copyright (c) 2010 Gilles Chehade <gilles@poolp.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);
+void log_verbose(int);
+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 log_trace(int, const char *, ...)
+ __attribute__((format (printf, 2, 3)));
+__dead void fatal(const char *, ...)
+ __attribute__((format (printf, 1, 2)));
+__dead void fatalx(const char *, ...)
+ __attribute__((format (printf, 1, 2)));
+const char * log_sockaddr(struct sockaddr *sa);
-/* $OpenBSD: ntpd.c,v 1.81 2015/01/04 01:24:43 bcook Exp $ */
+/* $OpenBSD: ntpd.c,v 1.82 2015/01/08 00:30:08 bcook Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
volatile sig_atomic_t reconfig = 0;
volatile sig_atomic_t sigchld = 0;
struct imsgbuf *ibuf;
-int debugsyslog = 0;
int timeout = INFTIM;
const char *showopt;
switch (ch) {
case 'd':
lconf.debug = 1;
+ log_verbose(1);
break;
case 'f':
conffile = optarg;
lconf.settime = 0;
break;
case 'v':
- debugsyslog = 1;
+ log_verbose(1);
break;
default:
usage();
-/* $OpenBSD: ntpd.h,v 1.113 2015/01/04 01:48:49 bcook Exp $ */
+/* $OpenBSD: ntpd.h,v 1.114 2015/01/08 00:30:08 bcook Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
#include <stdarg.h>
#include <poll.h>
+#include "log.h"
#include "ntp.h"
#include <imsg.h>
};
/* prototypes */
-/* log.c */
-void log_init(int);
-void logit(int, const char *, ...);
-void vlog(int, const char *, va_list);
-void log_warn(const char *, ...);
-void log_warnx(const char *, ...);
-void log_info(const char *, ...);
-void log_debug(const char *, ...);
-void fatal(const char *);
-void fatalx(const char *);
-const char *log_sockaddr(struct sockaddr *);
/* ntp.c */
pid_t ntp_main(int[2], int, struct ntpd_conf *, struct passwd *);
-/* $OpenBSD: parse.y,v 1.55 2014/11/20 05:51:20 jsg Exp $ */
+/* $OpenBSD: parse.y,v 1.56 2015/01/08 00:30:08 bcook Exp $ */
/*
* Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
if (vasprintf(&msg, fmt, ap) == -1)
fatalx("yyerror vasprintf");
va_end(ap);
- logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
+ log_warnx("%s:%d: %s", file->name, yylval.lineno, msg);
free(msg);
return (0);
}