]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - support/nfs/xlog.c
Cleaned up warnings in rmtab.c and xlog.c
[nfs-utils.git] / support / nfs / xlog.c
index 90c7e635df9403ca6c89c75dba1ac59fa78fc182..5ac9ba0e6a4d0657d2d9f9440319522921363cf6 100644 (file)
@@ -15,7 +15,9 @@
  *             as is, with no warranty expressed or implied.
  */
 
-#include "config.h"
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <unistd.h>
 #include <signal.h>
 
 #undef VERBOSE_PRINTF
 
-static int  foreground = 1;            /* not a daemon initially       */
+static int  log_stderr = 1;
+static int  log_syslog = 1;
 static int  logging = 0;               /* enable/disable DEBUG logs    */
 static int  logmask = 0;               /* What will be logged          */
 static char log_name[256];             /* name of this program         */
 static int  log_pid = -1;              /* PID of this program          */
-static FILE *log_fp = (FILE *)NULL;    /* fp for the log file          */
 
 static void    xlog_toggle(int sig);
 static struct xlog_debugfac    debugnames[] = {
@@ -50,11 +52,6 @@ void
 xlog_open(char *progname)
 {
        openlog(progname, LOG_PID, LOG_DAEMON);
-       if (foreground) {
-               log_fp = stderr;
-               if (log_fp != NULL)
-                       setbuf(log_fp, NULL);
-       }
 
        strncpy(log_name, progname, sizeof (log_name) - 1);
        log_name [sizeof (log_name) - 1] = '\0';
@@ -65,9 +62,15 @@ xlog_open(char *progname)
 }
 
 void
-xlog_background(void)
+xlog_stderr(int on)
+{
+       log_stderr = on;
+}
+
+void
+xlog_syslog(int on)
 {
-       foreground = 0;
+       log_syslog = on;
 }
 
 static void
@@ -126,64 +129,89 @@ xlog_enabled(int fac)
 }
 
 
-/* Write something to the system logfile. */
+/* Write something to the system logfile and/or stderr */
 void
-xlog(int kind, const char *fmt, ...)
+xlog_backend(int kind, const char *fmt, va_list args)
 {
-       char            buff[1024];
-       va_list         args;
-       int             logged = 1, n;
-#ifdef VERBOSE_PRINTF
-       time_t          now;
-       struct tm       *tm;
-#endif
+       va_list args2 = NULL;
 
        if (!(kind & (L_ALL)) && !(logging && (kind & logmask)))
                return;
 
-       va_start(args, fmt);
-       vsnprintf(buff, sizeof (buff), fmt, args);
-       va_end(args);
-       buff[sizeof (buff) - 1] = 0;
-
-       if ((n = strlen(buff)) > 0 && buff[n-1] != '\n') {
-               buff[n++] = '\n'; buff[n++] = '\0';
+       if (log_stderr)
+               va_copy(args2, args);
+
+       if (log_syslog) {
+               switch (kind) {
+               case L_FATAL:
+                       vsyslog(LOG_ERR, fmt, args);
+                       break;
+               case L_ERROR:
+                       vsyslog(LOG_ERR, fmt, args);
+                       break;
+               case L_WARNING:
+                       vsyslog(LOG_WARNING, fmt, args);
+                       break;
+               case L_NOTICE:
+                       vsyslog(LOG_NOTICE, fmt, args);
+                       break;
+               default:
+                       if (!log_stderr)
+                               vsyslog(LOG_INFO, fmt, args);
+                       break;
+               }
        }
 
-       switch (kind) {
-       case L_FATAL:
-               syslog(LOG_ERR, "%s", buff);
-               break;
-       case L_ERROR:
-               syslog(LOG_ERR, "%s", buff);
-               break;
-       case L_WARNING:
-               syslog(LOG_WARNING, "%s", buff);
-               break;
-       case L_NOTICE:
-               syslog(LOG_NOTICE, "%s", buff);
-               break;
-       default:
-               logged = 0;
-               break;
-       }
-       if (!logged || foreground) {
-               if (!logged && log_fp == NULL) {
-                       syslog(LOG_DEBUG, "%s", buff);
-               } else if (log_fp != NULL) {
+       if (log_stderr) {
 #ifdef VERBOSE_PRINTF
-                       time(&now);
-                       tm = localtime(&now);
-                       fprintf(log_fp, "%s[%d] %02d/%02d/%02d %02d:%02d %s\n",
-                                       log_name, log_pid,
-                                       tm->tm_mon + 1, tm->tm_mday,
-                                       tm->tm_year, tm->tm_hour, tm->tm_min,
-                                       buff);
+               time_t          now;
+               struct tm       *tm;
+
+               time(&now);
+               tm = localtime(&now);
+               fprintf(stderr, "%s[%d] %04d-%02d-%02d %02d:%02d:%02d ",
+                               log_name, log_pid,
+                               tm->tm_year+1900, tm->tm_mon + 1, tm->tm_mday,
+                               tm->tm_hour, tm->tm_min, tm->tm_sec);
 #else
-                       fprintf(log_fp, "%s: %s", log_name, buff);
+               fprintf(stderr, "%s: ", log_name);
 #endif
-               }
+               vfprintf(stderr, fmt, args2);
+               fprintf(stderr, "\n");
+               va_end(args2);
        }
+
        if (kind == L_FATAL)
                exit(1);
 }
+
+void
+xlog(int kind, const char* fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       xlog_backend(kind, fmt, args);
+       va_end(args);
+}
+
+void
+xlog_warn(const char* fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       xlog_backend(L_WARNING, fmt, args);
+       va_end(args);
+}
+
+
+void
+xlog_err(const char* fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       xlog_backend(L_FATAL, fmt, args);
+       va_end(args);
+}