Add support for a --time-stamp option that causes each message output
by valgrind to include a time stamp. This fixes bug #70587.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@2594 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/docs/coregrind_core.html b/coregrind/docs/coregrind_core.html
index 1a3ea84..384791e 100644
--- a/coregrind/docs/coregrind_core.html
+++ b/coregrind/docs/coregrind_core.html
@@ -557,6 +557,12 @@
       <code>valgrind-listener</code> program.  For further details,
       see section <a href="#core-comment">2.3</a>.
       </li><br><p>
+
+  <li><code>--time-stamp=no</code> [default]<br>
+      <code>--time-stamp=yes</code>
+      <p>Specifies that valgrind should output a timestamp before
+      each message that it outputs.
+      </li><br><p>
 </ul>
 
 <h4>Error-related options</h4>
diff --git a/coregrind/vg_include.h b/coregrind/vg_include.h
index 7bd817f..2fad874 100644
--- a/coregrind/vg_include.h
+++ b/coregrind/vg_include.h
@@ -204,6 +204,9 @@
 extern Int     VG_(clo_log_fd);
 extern Char*   VG_(clo_log_name);
 
+/* Add timestamps to log messages?  default: NO */
+extern Bool  VG_(clo_time_stamp);
+
 /* The file descriptor to read for input.  default: 0 == stdin */
 extern Int   VG_(clo_input_fd);
 /* The number of suppression files specified. */
diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c
index a0f9693..ab374da 100644
--- a/coregrind/vg_main.c
+++ b/coregrind/vg_main.c
@@ -1433,6 +1433,8 @@
 Int     VG_(clo_log_fd)        = 1;
 Char*   VG_(clo_log_name)      = NULL;
 
+Bool   VG_(clo_time_stamp)     = False;
+
 Int    VG_(clo_input_fd)       = 0; /* stdin */
 Int    VG_(clo_n_suppressions) = 0;
 Char*  VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
@@ -1481,6 +1483,7 @@
 "    -v --verbose              be more verbose, incl counts of errors\n"
 "    --trace-children=no|yes   Valgrind-ise child processes? [no]\n"
 "    --track-fds=no|yes        track open file descriptors? [no]\n"
+"    --time-stamp=no|yes       add timestamps to log messages? [no]\n"
 "\n"
 "  uncommon user options for all Valgrind tools:\n"
 "    --run-libc-freeres=no|yes free up glibc memory at exit? [yes]\n"
@@ -1685,6 +1688,7 @@
       else VG_BOOL_CLO("--run-libc-freeres", VG_(clo_run_libc_freeres))
       else VG_BOOL_CLO("--show-below-main",  VG_(clo_show_below_main))
       else VG_BOOL_CLO("--single-step",      VG_(clo_single_step))
+      else VG_BOOL_CLO("--time-stamp",       VG_(clo_time_stamp))
       else VG_BOOL_CLO("--track-fds",        VG_(clo_track_fds))
       else VG_BOOL_CLO("--trace-children",   VG_(clo_trace_children))
       else VG_BOOL_CLO("--trace-sched",      VG_(clo_trace_sched))
diff --git a/coregrind/vg_messages.c b/coregrind/vg_messages.c
index 5a19b07..c7b35d1 100644
--- a/coregrind/vg_messages.c
+++ b/coregrind/vg_messages.c
@@ -32,6 +32,9 @@
 
 #include "vg_include.h"
 
+#include <time.h>
+#include <sys/time.h>
+
 
 static char vg_mbuf[M_VG_MSGBUF];
 static int vg_n_mbuf;
@@ -43,6 +46,24 @@
   vg_mbuf[vg_n_mbuf]   = 0;
 }
 
+static void add_timestamp ( Char *buf )
+{
+   struct timeval tv;
+   struct tm tm;
+  
+   if ( gettimeofday( &tv, NULL ) == 0 &&
+        localtime_r( &tv.tv_sec, &tm ) == &tm ) {
+      VG_(sprintf)( buf, "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
+                    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
+                    tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec / 1000 );
+   }
+   else {
+      VG_(strcpy)( buf, "" );
+   }
+   
+   return;
+}
+
 
 /* Publically visible from here onwards. */
 
@@ -79,9 +100,14 @@
 
 int VG_(start_msg) ( VgMsgKind kind )
 {
+   Char ts[32];
    Char c;
    vg_n_mbuf = 0;
    vg_mbuf[vg_n_mbuf] = 0;
+   if (VG_(clo_time_stamp))
+     add_timestamp(ts);
+   else
+     VG_(strcpy)(ts, "");
    switch (kind) {
       case Vg_UserMsg:       c = '='; break;
       case Vg_DebugMsg:      c = '-'; break;
@@ -89,8 +115,8 @@
       case Vg_ClientMsg:     c = '*'; break;
       default:               c = '?'; break;
    }
-   return VG_(add_to_msg)( "%c%c%d%c%c ", 
-                           c,c, VG_(getpid)(), c,c );
+   return VG_(add_to_msg)( "%c%c%s%d%c%c ", 
+                           c,c, ts, VG_(getpid)(), c,c );
 }
 
 
diff --git a/none/tests/cmdline1.stdout.exp b/none/tests/cmdline1.stdout.exp
index 1c36bca..ac28bbb 100644
--- a/none/tests/cmdline1.stdout.exp
+++ b/none/tests/cmdline1.stdout.exp
@@ -9,6 +9,7 @@
     -v --verbose              be more verbose, incl counts of errors
     --trace-children=no|yes   Valgrind-ise child processes? [no]
     --track-fds=no|yes        track open file descriptors? [no]
+    --time-stamp=no|yes       add timestamps to log messages? [no]
 
   uncommon user options for all Valgrind tools:
     --run-libc-freeres=no|yes free up glibc memory at exit? [yes]
diff --git a/none/tests/cmdline2.stdout.exp b/none/tests/cmdline2.stdout.exp
index c331c8d..33e8607 100644
--- a/none/tests/cmdline2.stdout.exp
+++ b/none/tests/cmdline2.stdout.exp
@@ -9,6 +9,7 @@
     -v --verbose              be more verbose, incl counts of errors
     --trace-children=no|yes   Valgrind-ise child processes? [no]
     --track-fds=no|yes        track open file descriptors? [no]
+    --time-stamp=no|yes       add timestamps to log messages? [no]
 
   uncommon user options for all Valgrind tools:
     --run-libc-freeres=no|yes free up glibc memory at exit? [yes]