[tac_plus] [PATCH tac_plus] Add timestamps to stderr logging when running in foreground mode

Philip Prindeville philipp at redfish-solutions.com
Mon Sep 12 04:47:55 UTC 2016


From: Philip Prindeville <philipp at redfish-solutions.com>

When debugging certain issues (like shutting down idle client
connections, etc) it's useful to have timestamps on the logging
output, even when running in single-threaded mode (option -g).

---
 configure.ac | 24 ++++++++++++++++++++++++
 report.c     |  5 +++++
 tac_plus.h   |  2 ++
 utils.c      | 31 +++++++++++++++++++++++++++++++
 4 files changed, 62 insertions(+)

diff --git a/configure.ac b/configure.ac
index c9ecf95..7ad9083 100644
--- a/configure.ac
+++ b/configure.ac
@@ -720,6 +720,30 @@ AC_ARG_WITH(prof,
 AC_SUBST(PROFLAGS)
 AC_SUBST(PROFLIBS)
 
+dnl
+dnl USE_STDERR_TIMESTAMPS - Turn on timestamping on logging to stderr
+dnl
+AC_MSG_CHECKING(whether to enable foreground logging of timestamps support)
+AH_TEMPLATE(USE_STDERR_TIMESTAMPS, [define this to enable support for including timestamps when foreground logging to stderr ])
+AC_ARG_ENABLE(stderr-timestamps,
+    AS_HELP_STRING([--enable-stderr-timestamps],[Log timestamps in stderr output in foreground mode]),
+[ case "$enable_stderr_timestamps" in
+  no)
+    AC_MSG_RESULT(no)
+    ;;
+  yes)
+    AC_MSG_RESULT(yes)
+    AC_DEFINE(USE_STDERR_TIMESTAMPS)
+    ;;
+  *)
+    AC_MSG_RESULT(no)
+    ;;
+  esac ],
+  # ie: no --{enable,disable}-stderr-timestamps option, withval == ""
+  AC_MSG_RESULT(no)
+)
+dnl AC_SUBST(USE_STDERR_TIMESTAMPS)
+
 # look for PAM
 AH_TEMPLATE(HAVE_PAM, [define if your system has libpam])
 AC_CHECK_LIB([pam], [pam_start],
diff --git a/report.c b/report.c
index a2c86c7..dfa582a 100644
--- a/report.c
+++ b/report.c
@@ -115,6 +115,11 @@ report(priority, fmt, va_alist)
     }
 
     if (single) {
+#ifdef USE_STDERR_TIMESTAMPS
+	const char *ts = tac_timestamp();
+	fwrite(ts, strlen(ts), 1, stderr);
+	fputc(' ', stderr);
+#endif
 	fwrite(msg, nchars, 1, stderr);
 	fputc('\n', stderr);
     }
diff --git a/tac_plus.h b/tac_plus.h
index 205f632..4bcd1f4 100644
--- a/tac_plus.h
+++ b/tac_plus.h
@@ -342,6 +342,8 @@ char *tac_strdup(char *);
 char *tac_make_string(u_char *, int);
 char *tac_find_substring(char *, char *);
 char *tac_realloc(char *, int);
+const char *tac_timestamp(void);
+const char *tac_iso_timestamp(void);
 
 /* do_acct.c */
 int do_acct_file(struct acct_rec *);
diff --git a/utils.c b/utils.c
index 3b1ae59..d2919ff 100644
--- a/utils.c
+++ b/utils.c
@@ -241,3 +241,34 @@ tac_unlockfd(char *filename, int lockfd)
     }
     return(0);
 }
+
+const char *
+tac_timestamp()
+{
+    static char buf[16];
+    time_t now;
+    struct tm *tm;
+    static const char format[] = "%b %e %T";
+
+    time(&now);
+    tm = localtime(&now);
+    strftime(buf, sizeof(buf), format, tm);
+
+    return buf;
+}
+
+const char *
+tac_iso_timestamp()
+{
+    static char buf[17];
+    time_t now;
+    struct tm *tm;
+    static const char format[] = "%Y%m%dT%H%M%SZ";
+
+    time(&now);
+    tm = gmtime(&now);
+    strftime(buf, sizeof(buf), format, tm);
+
+    return buf;
+}
+



More information about the tac_plus mailing list