aboutsummaryrefslogtreecommitdiff
path: root/gdb/ui-file.c
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2021-12-31 11:44:19 -0700
committerTom Tromey <tom@tromey.com>2022-03-28 14:13:28 -0600
commit3c6c449e304413f513db5635abd2181776f7db92 (patch)
tree5893f61deb6f79f3c3107c06c961ccc4cad8bb49 /gdb/ui-file.c
parent8b1931b39443acab9d1f8272a8a81b261f7ef29b (diff)
downloadfsf-binutils-gdb-3c6c449e304413f513db5635abd2181776f7db92.zip
fsf-binutils-gdb-3c6c449e304413f513db5635abd2181776f7db92.tar.gz
fsf-binutils-gdb-3c6c449e304413f513db5635abd2181776f7db92.tar.bz2
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.
Diffstat (limited to 'gdb/ui-file.c')
-rw-r--r--gdb/ui-file.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/gdb/ui-file.c b/gdb/ui-file.c
index d30ec04..354a7c3 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);
+}