public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 4/4] Switch gdb_stdlog to use timestamped_file
Date: Fri, 31 Dec 2021 13:19:01 -0700	[thread overview]
Message-ID: <20211231201901.1567610-5-tom@tromey.com> (raw)
In-Reply-To: <20211231201901.1567610-1-tom@tromey.com>

Currently, timestamps for logging are done by looking for the use of
gdb_stdlog in vfprintf_unfiltered.  This seems potentially buggy, in
that during logging or other redirects (like execute_fn_to_ui_file) we
might have gdb_stdout==gdb_stdlog and so, conceivably, wind up with
timestamps in a log when they were not desired.

It seems better, instead, for timestamps to be a property of the
ui_file itself.

This patch changes gdb to use the new timestamped_file for gdb_stdlog
where appropriate, and removes the special case from
vfprintf_unfiltered.

Note that this may somewhat change the output in some cases -- in
particular, when going through execute_fn_to_ui_file (or the _string
variant), timestamps won't be emitted.  This could be fixed in those
functions, but it wasn't clear to me whether this is really desirable.

Note also that this changes the TUI to send gdb_stdlog to gdb_stderr.
I imagine that the previous use of gdb_stdout here was inadvertent.
(And in any case it probably doesn't matter.)
---
 gdb/cli/cli-interp.c |  6 +++++-
 gdb/event-top.c      |  2 +-
 gdb/tui/tui-io.c     |  8 ++++++--
 gdb/utils.c          | 30 +-----------------------------
 4 files changed, 13 insertions(+), 33 deletions(-)

diff --git a/gdb/cli/cli-interp.c b/gdb/cli/cli-interp.c
index 031ab10aea6..c52a5f10a18 100644
--- a/gdb/cli/cli-interp.c
+++ b/gdb/cli/cli-interp.c
@@ -396,6 +396,7 @@ struct saved_output_files
   ui_file *targ;
   ui_file *targerr;
   ui_file_up file_to_delete;
+  ui_file_up log_to_delete;
 };
 static std::unique_ptr<saved_output_files> saved_output;
 
@@ -426,8 +427,11 @@ cli_interp_base::set_logging (ui_file_up logfile, bool logging_redirect,
       else
 	saved_output->file_to_delete = std::move (logfile);
 
+      saved_output->log_to_delete.reset
+	(new timestamped_file (debug_redirect ? logfile_p : tee));
+
       gdb_stdout = logging_redirect ? logfile_p : tee;
-      gdb_stdlog = debug_redirect ? logfile_p : tee;
+      gdb_stdlog = saved_output->log_to_delete.get ();
       gdb_stderr = logging_redirect ? logfile_p : tee;
       gdb_stdtarg = logging_redirect ? logfile_p : tee;
       gdb_stdtargerr = logging_redirect ? logfile_p : tee;
diff --git a/gdb/event-top.c b/gdb/event-top.c
index 530ea298247..80ac1e4a61e 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -1285,7 +1285,7 @@ gdb_setup_readline (int editing)
   if (!batch_silent)
     gdb_stdout = new stdio_file (ui->outstream);
   gdb_stderr = new stderr_file (ui->errstream);
-  gdb_stdlog = gdb_stderr;  /* for moment */
+  gdb_stdlog = new timestamped_file (gdb_stderr);
   gdb_stdtarg = gdb_stderr; /* for moment */
   gdb_stdtargerr = gdb_stderr; /* for moment */
 
diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c
index bd443dc0c0c..58996df7924 100644
--- a/gdb/tui/tui-io.c
+++ b/gdb/tui/tui-io.c
@@ -108,11 +108,13 @@ key_is_start_sequence (int ch)
 /* TUI output files.  */
 static struct ui_file *tui_stdout;
 static struct ui_file *tui_stderr;
+static struct ui_file *tui_stdlog;
 struct ui_out *tui_out;
 
 /* GDB output files in non-curses mode.  */
 static struct ui_file *tui_old_stdout;
 static struct ui_file *tui_old_stderr;
+static struct ui_file *tui_old_stdlog;
 cli_ui_out *tui_old_uiout;
 
 /* Readline previous hooks.  */
@@ -828,13 +830,14 @@ tui_setup_io (int mode)
       /* Keep track of previous gdb output.  */
       tui_old_stdout = gdb_stdout;
       tui_old_stderr = gdb_stderr;
+      tui_old_stdlog = gdb_stdlog;
       tui_old_uiout = dynamic_cast<cli_ui_out *> (current_uiout);
       gdb_assert (tui_old_uiout != nullptr);
 
       /* Reconfigure gdb output.  */
       gdb_stdout = tui_stdout;
       gdb_stderr = tui_stderr;
-      gdb_stdlog = gdb_stdout;	/* for moment */
+      gdb_stdlog = tui_stdlog;
       gdb_stdtarg = gdb_stderr;	/* for moment */
       gdb_stdtargerr = gdb_stderr;	/* for moment */
       current_uiout = tui_out;
@@ -847,7 +850,7 @@ tui_setup_io (int mode)
       /* Restore gdb output.  */
       gdb_stdout = tui_old_stdout;
       gdb_stderr = tui_old_stderr;
-      gdb_stdlog = gdb_stdout;	/* for moment */
+      gdb_stdlog = tui_old_stdlog;
       gdb_stdtarg = gdb_stderr;	/* for moment */
       gdb_stdtargerr = gdb_stderr;	/* for moment */
       current_uiout = tui_old_uiout;
@@ -902,6 +905,7 @@ tui_initialize_io (void)
   /* Create tui output streams.  */
   tui_stdout = new tui_file (stdout);
   tui_stderr = new tui_file (stderr);
+  tui_stdlog = new timestamped_file (tui_stderr);
   tui_out = tui_out_new (tui_stdout);
 
   /* Create the default UI.  */
diff --git a/gdb/utils.c b/gdb/utils.c
index fc460d20321..13d431c5301 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -2060,35 +2060,7 @@ vfprintf_filtered (struct ui_file *stream, const char *format, va_list args)
 void
 vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args)
 {
-  if (debug_timestamp && stream == gdb_stdlog)
-    {
-      static bool needs_timestamp = true;
-
-      /* Print timestamp if previous print ended with a \n.  */
-      if (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 ());
-	  fputs_unfiltered (timestamp.c_str (), stream);
-	}
-
-      /* Print the message.  */
-      string_file sfile;
-      cli_ui_out (&sfile, 0).vmessage (ui_file_style (), format, args);
-      std::string linebuffer = std::move (sfile.string ());
-      fputs_unfiltered (linebuffer.c_str (), stream);
-
-      size_t len = linebuffer.length ();
-      needs_timestamp = (len > 0 && linebuffer[len - 1] == '\n');
-    }
-  else
-    vfprintf_maybe_filtered (stream, format, args, false);
+  vfprintf_maybe_filtered (stream, format, args, false);
 }
 
 void
-- 
2.31.1


  parent reply	other threads:[~2021-12-31 20:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-31 20:18 [PATCH 0/4] Change how stdlog timestamps are written Tom Tromey
2021-12-31 20:18 ` [PATCH 1/4] Simplify the CLI set_logging logic Tom Tromey
2021-12-31 20:18 ` [PATCH 2/4] Use unique_ptr in CLI logging code Tom Tromey
2021-12-31 20:19 ` [PATCH 3/4] Add new timestamped_file class Tom Tromey
2021-12-31 20:19 ` Tom Tromey [this message]
2022-03-24 16:21 ` [PATCH 0/4] Change how stdlog timestamps are written Tom Tromey
2022-03-28 20:12   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211231201901.1567610-5-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).