public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Add new timestamped_file class
@ 2022-03-28 20:19 Tom Tromey
  0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2022-03-28 20:19 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3c6c449e304413f513db5635abd2181776f7db92

commit 3c6c449e304413f513db5635abd2181776f7db92
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Dec 31 11:44:19 2021 -0700

    Add new timestamped_file class
    
    This adds a "timestamped_file" subclass of ui_file.  This class adds a
    timestamp to its output when appropriate.  That is, it follows the
    rule already used in vfprintf_unfiltered of adding a timestamp at most
    once per write.
    
    The new class is not yet used.

Diff:
---
 gdb/ui-file.c | 29 +++++++++++++++++++++++++++++
 gdb/ui-file.h | 24 ++++++++++++++++++++++++
 gdb/utils.c   |  2 +-
 gdb/utils.h   |  3 +++
 4 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/gdb/ui-file.c b/gdb/ui-file.c
index d30ec04a68f..354a7c3e3b6 100644
--- a/gdb/ui-file.c
+++ b/gdb/ui-file.c
@@ -25,6 +25,7 @@
 #include "gdbsupport/gdb_select.h"
 #include "gdbsupport/filestuff.h"
 #include "cli/cli-style.h"
+#include <chrono>
 
 null_file null_stream;
 
@@ -449,3 +450,31 @@ no_terminal_escape_file::puts (const char *buf)
   if (*buf != '\0')
     this->stdio_file::write (buf, strlen (buf));
 }
+
+void
+timestamped_file::write (const char *buf, long len)
+{
+  if (debug_timestamp)
+    {
+      /* Print timestamp if previous print ended with a \n.  */
+      if (m_needs_timestamp)
+	{
+	  using namespace std::chrono;
+
+	  steady_clock::time_point now = steady_clock::now ();
+	  seconds s = duration_cast<seconds> (now.time_since_epoch ());
+	  microseconds us = duration_cast<microseconds> (now.time_since_epoch () - s);
+	  std::string timestamp = string_printf ("%ld.%06ld ",
+						 (long) s.count (),
+						 (long) us.count ());
+	  m_stream->puts (timestamp.c_str ());
+	}
+
+      /* Print the message.  */
+      m_stream->write (buf, len);
+
+      m_needs_timestamp = (len > 0 && buf[len - 1] == '\n');
+    }
+  else
+    m_stream->write (buf, len);
+}
diff --git a/gdb/ui-file.h b/gdb/ui-file.h
index d8bc3fb3e24..8d41f6f8300 100644
--- a/gdb/ui-file.h
+++ b/gdb/ui-file.h
@@ -364,4 +364,28 @@ public:
   void puts (const char *linebuffer) override;
 };
 
+/* A ui_file that optionally puts a timestamp at the start of each
+   line of output.  */
+
+class timestamped_file : public ui_file
+{
+public:
+  explicit timestamped_file (ui_file *stream)
+    : m_stream (stream)
+  {
+  }
+
+  DISABLE_COPY_AND_ASSIGN (timestamped_file);
+
+  void write (const char *buf, long len) override;
+
+private:
+
+  /* Output is sent here.  */
+  ui_file *m_stream;
+
+  /* True if the next output should be timestamped.  */
+  bool m_needs_timestamp = true;
+};
+
 #endif
diff --git a/gdb/utils.c b/gdb/utils.c
index a8d6c96386d..8dd70087d0c 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -103,7 +103,7 @@ static std::chrono::steady_clock::duration prompt_for_continue_wait_time;
 
 /* A flag indicating whether to timestamp debugging messages.  */
 
-static bool debug_timestamp = false;
+bool debug_timestamp = false;
 
 /* True means that strings with character values >0x7F should be printed
    as octal escapes.  False means just print the value (e.g. it's an
diff --git a/gdb/utils.h b/gdb/utils.h
index d6080012aa6..2cd0cff0766 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -185,6 +185,9 @@ extern int get_chars_per_line ();
 
 extern bool pagination_enabled;
 
+/* A flag indicating whether to timestamp debugging messages.  */
+extern bool debug_timestamp;
+
 extern struct ui_file **current_ui_gdb_stdout_ptr (void);
 extern struct ui_file **current_ui_gdb_stdin_ptr (void);
 extern struct ui_file **current_ui_gdb_stderr_ptr (void);


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-28 20:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-28 20:19 [binutils-gdb] Add new timestamped_file class Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).