On Sun, Jan 04, 2015 at 12:38:37PM +0100, Peter Hessler wrote:
> fatal should completely be variadic, a printf-alike.
> 

How about something like this for making fatal variadic first.  
This factors out the guts of log_warn and adds logerr that they both can
share.

---
 src/usr.sbin/ntpd/log.c  | 54 +++++++++++++++++++++++++++---------------------
 src/usr.sbin/ntpd/ntpd.h |  4 ++--
 2 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/src/usr.sbin/ntpd/log.c b/src/usr.sbin/ntpd/log.c
index 618f4cc..e92924e 100644
--- a/src/usr.sbin/ntpd/log.c
+++ b/src/usr.sbin/ntpd/log.c
@@ -71,29 +71,33 @@ vlog(int pri, const char *fmt, va_list ap)
                vsyslog(pri, fmt, ap);
 }
 
-
 void
-log_warn(const char *emsg, ...)
+vlogerr(int pri, const char *fmt, va_list ap)
 {
        char    *nfmt;
-       va_list  ap;
 
        /* best effort to even work in out of memory situations */
-       if (emsg == NULL)
-               logit(LOG_CRIT, "%s", strerror(errno));
-       else {
-               va_start(ap, emsg);
-
-               if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
+       if (fmt) {
+               if (asprintf(&nfmt, "%s: %s", fmt, strerror(errno)) == -1) {
                        /* we tried it... */
-                       vlog(LOG_CRIT, emsg, ap);
+                       vlog(LOG_CRIT, fmt, ap);
                        logit(LOG_CRIT, "%s", strerror(errno));
                } else {
                        vlog(LOG_CRIT, nfmt, ap);
                        free(nfmt);
                }
-               va_end(ap);
-       }
+       } else
+               logit(LOG_CRIT, "%s", strerror(errno));
+}
+
+void
+log_warn(const char *emsg, ...)
+{
+       va_list  ap;
+
+       va_start(ap, emsg);
+       vlogerr(LOG_CRIT, emsg, ap);
+       va_end(ap);
 }
 
 void
@@ -129,25 +133,27 @@ log_debug(const char *emsg, ...)
 }
 
 void
-fatal(const char *emsg)
+fatal(const char *emsg, ...)
 {
-       if (emsg == NULL)
-               logit(LOG_CRIT, "fatal: %s", strerror(errno));
-       else
-               if (errno)
-                       logit(LOG_CRIT, "fatal: %s: %s",
-                           emsg, strerror(errno));
-               else
-                       logit(LOG_CRIT, "fatal: %s", emsg);
+       va_list  ap;
+
+       va_start(ap, emsg);
+       vlogerr(LOG_CRIT, emsg, ap);
+       va_end(ap);
 
        exit(1);
 }
 
 void
-fatalx(const char *emsg)
+fatalx(const char *emsg, ...)
 {
-       errno = 0;
-       fatal(emsg);
+       va_list  ap;
+
+       va_start(ap, emsg);
+       vlog(LOG_CRIT, emsg, ap);
+       va_end(ap);
+
+       exit(1);
 }
 
 const char *
diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h
index 4a768d8..31bc5a0 100644
--- a/src/usr.sbin/ntpd/ntpd.h
+++ b/src/usr.sbin/ntpd/ntpd.h
@@ -269,8 +269,8 @@ 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 *);
+void            fatal(const char *, ...);
+void            fatalx(const char *, ...);
 const char     *log_sockaddr(struct sockaddr *);
 
 /* ntp.c */
-- 
1.9.1

Reply via email to