[logging] Add new "log timestamp precision" command for subsecond timestamps

2007-04-28 Andrew J. Schorr <ajschorr@alumni.princeton.edu>

	* command.c: (config_write_host) Save "log timestamp precision"
	  if not default value.
	  (show_logging) Show configured timestamp precision.
	  (config_log_timestamp_precision) Enable configuration of timestamp
	  precision.
	  (no_config_log_timestamp_precision) Restore default timestamp
	  precision.
	  (cmd_init) Install new timestamp precision commands.
	* log.h: (struct zlog) New timestamp_precision field.
	  (quagga_timestamp) New function to generate a timestamp with the
	  desired precision.
	  (struct timestamp_control) Declare a structure for use in avoiding
	  repeated duplicate calls to quagga_timestamp.
	* log.c: (quagga_timestamp) New function to generate a timestamp
	  of the desired precision.
	  (time_print) Call quagga_timestamp if the time hasn't already been
	  calculated.
	  (vzlog) Initialize a timestamp_control structure and pass it to
	  time_print and vty_log.
	  (zlog_backtrace) Fix 64-bit problem: cannot print size_t with %u.
	* vty.h: Must now include "log.h".
	  (vty_log) Takes an additional struct timestamp_control argument.
	* vty.c: (vty_log_out) Use new struct timestamp_control and new
	  quagga_timestamp function to print timestamps of the desired
	  precision.
	  (vty_time_print) Use new quagga_timestamp function.
	  (vty_log) Accept new struct timestamp_control argument and pass it
	  down to vty_log_out.
diff --git a/lib/log.h b/lib/log.h
index 0f058a8..da8fbea 100644
--- a/lib/log.h
+++ b/lib/log.h
@@ -1,5 +1,5 @@
 /*
- * $Id: log.h,v 1.18 2005/05/06 21:25:49 paul Exp $
+ * $Id$
  *
  * Zebra logging funcions.
  * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
@@ -83,6 +83,7 @@
   int record_priority;	/* should messages logged through stdio include the
   			   priority of the message? */
   int syslog_options;	/* 2nd arg to openlog */
+  int timestamp_precision;	/* # of digits of subsecond precision */
 };
 
 /* Message structure. */
@@ -168,6 +169,23 @@
    that is logged in addition to the current backtrace. */
 extern void zlog_backtrace_sigsafe(int priority, void *program_counter);
 
+/* Puts a current timestamp in buf and returns the number of characters
+   written (not including the terminating NUL).  The purpose of
+   this function is to avoid calls to localtime appearing all over the code.
+   It caches the most recent localtime result and can therefore
+   avoid multiple calls within the same second.  If buflen is too small,
+   *buf will be set to '\0', and 0 will be returned. */
+extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */,
+			       char *buf, size_t buflen);
+
+/* structure useful for avoiding repeated rendering of the same timestamp */
+struct timestamp_control {
+   size_t len;		/* length of rendered timestamp */
+   int precision;	/* configuration parameter */
+   int already_rendered; /* should be initialized to 0 */
+   char buf[40];	/* will contain the rendered timestamp */
+};
+
 /* Defines for use in command construction: */
 
 #define LOG_LEVELS "(emergencies|alerts|critical|errors|warnings|notifications|informational|debugging)"