[2/3] Don't let tee_file own a stream
Commit Message
Right now, tee_file owns the second stream it writes to. This is done
for the convenience of the users. In a subsequent patch, this will no
longer be convenient, so this patch moves the responsibility for
ownership to the users of tee_file.
---
gdb/cli/cli-interp.c | 11 ++++++-----
gdb/cli/cli-interp.h | 1 +
gdb/mi/mi-interp.c | 15 ++++++---------
gdb/mi/mi-interp.h | 2 +-
gdb/ui-file.c | 4 ++--
gdb/ui-file.h | 8 ++++----
6 files changed, 20 insertions(+), 21 deletions(-)
Comments
On 11/17/22 13:29, Tom Tromey via Gdb-patches wrote:
> Right now, tee_file owns the second stream it writes to. This is done
> for the convenience of the users. In a subsequent patch, this will no
> longer be convenient, so this patch moves the responsibility for
> ownership to the users of tee_file.
I think the idea makes sense, from a software design / decoupling point
of view.
> diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
> index af242208a0b..482166ea3a5 100644
> --- a/gdb/mi/mi-interp.c
> +++ b/gdb/mi/mi-interp.c
> @@ -1275,21 +1275,16 @@ mi_interp::set_logging (ui_file_up logfile, bool logging_redirect,
> {
> mi->saved_raw_stdout = mi->raw_stdout;
>
> - /* If something is being redirected, then grab logfile. */
> - ui_file *logfile_p = nullptr;
> - if (logging_redirect || debug_redirect)
> - {
> - logfile_p = logfile.get ();
> - mi->saved_raw_file_to_delete = logfile_p;
> - }
> + ui_file *logfile_p = logfile.get ();
> + mi->saved_raw_file_to_delete = logfile.release ();
>
> /* If something is not being redirected, then a tee containing both the
> logfile and stdout. */
> ui_file *tee = nullptr;
> if (!logging_redirect || !debug_redirect)
> {
> - tee = new tee_file (mi->raw_stdout, std::move (logfile));
> - mi->saved_raw_file_to_delete = tee;
> + tee = new tee_file (mi->raw_stdout, logfile_p);
> + mi->saved_tee_to_delete = tee;
> }
>
> mi->raw_stdout = logging_redirect ? logfile_p : tee;
> @@ -1297,9 +1292,11 @@ mi_interp::set_logging (ui_file_up logfile, bool logging_redirect,
> else
> {
> delete mi->saved_raw_file_to_delete;
> + delete mi->saved_tee_to_delete;
> mi->raw_stdout = mi->saved_raw_stdout;
> mi->saved_raw_stdout = nullptr;
> mi->saved_raw_file_to_delete = nullptr;
> + mi->saved_tee_to_delete = nullptr;
Should the two _to_delete ones be ui_file_ups?
Otherwise, everything related to logging / redirection confuse me, so I
can't really comment on the change itself.
Simon
>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
>> + mi->saved_tee_to_delete = nullptr;
Simon> Should the two _to_delete ones be ui_file_ups?
It would be better. I'll add on a patch to do this.
Simon> Otherwise, everything related to logging / redirection confuse me, so I
Simon> can't really comment on the change itself.
It's a real mess.
Tom
@@ -386,17 +386,18 @@ cli_interp_base::set_logging (ui_file_up logfile, bool logging_redirect,
m_saved_output->targ = gdb_stdtarg;
m_saved_output->targerr = gdb_stdtargerr;
+ ui_file *logfile_p = logfile.get ();
+ m_saved_output->file_to_delete = std::move (logfile);
+
/* If something is not being redirected, then a tee containing both the
logfile and stdout. */
- ui_file *logfile_p = logfile.get ();
ui_file *tee = nullptr;
if (!logging_redirect || !debug_redirect)
{
- tee = new tee_file (gdb_stdout, std::move (logfile));
- m_saved_output->file_to_delete.reset (tee);
+ m_saved_output->tee_to_delete.reset
+ (new tee_file (gdb_stdout, logfile_p));
+ tee = m_saved_output->tee_to_delete.get ();
}
- else
- m_saved_output->file_to_delete = std::move (logfile);
m_saved_output->log_to_delete.reset
(new timestamped_file (debug_redirect ? logfile_p : tee));
@@ -41,6 +41,7 @@ class cli_interp_base : public interp
ui_file *log;
ui_file *targ;
ui_file *targerr;
+ ui_file_up tee_to_delete;
ui_file_up file_to_delete;
ui_file_up log_to_delete;
};
@@ -1275,21 +1275,16 @@ mi_interp::set_logging (ui_file_up logfile, bool logging_redirect,
{
mi->saved_raw_stdout = mi->raw_stdout;
- /* If something is being redirected, then grab logfile. */
- ui_file *logfile_p = nullptr;
- if (logging_redirect || debug_redirect)
- {
- logfile_p = logfile.get ();
- mi->saved_raw_file_to_delete = logfile_p;
- }
+ ui_file *logfile_p = logfile.get ();
+ mi->saved_raw_file_to_delete = logfile.release ();
/* If something is not being redirected, then a tee containing both the
logfile and stdout. */
ui_file *tee = nullptr;
if (!logging_redirect || !debug_redirect)
{
- tee = new tee_file (mi->raw_stdout, std::move (logfile));
- mi->saved_raw_file_to_delete = tee;
+ tee = new tee_file (mi->raw_stdout, logfile_p);
+ mi->saved_tee_to_delete = tee;
}
mi->raw_stdout = logging_redirect ? logfile_p : tee;
@@ -1297,9 +1292,11 @@ mi_interp::set_logging (ui_file_up logfile, bool logging_redirect,
else
{
delete mi->saved_raw_file_to_delete;
+ delete mi->saved_tee_to_delete;
mi->raw_stdout = mi->saved_raw_stdout;
mi->saved_raw_stdout = nullptr;
mi->saved_raw_file_to_delete = nullptr;
+ mi->saved_tee_to_delete = nullptr;
}
mi->out->set_raw (mi->raw_stdout);
@@ -57,7 +57,7 @@ class mi_interp final : public interp
done. */
struct ui_file *saved_raw_stdout;
struct ui_file *saved_raw_file_to_delete;
-
+ struct ui_file *saved_tee_to_delete;
/* MI's builder. */
struct ui_out *mi_uiout;
@@ -384,9 +384,9 @@ stderr_file::stderr_file (FILE *stream)
-tee_file::tee_file (ui_file *one, ui_file_up &&two)
+tee_file::tee_file (ui_file *one, ui_file *two)
: m_one (one),
- m_two (std::move (two))
+ m_two (two)
{}
tee_file::~tee_file ()
@@ -329,9 +329,9 @@ class stderr_file : public stdio_file
class tee_file : public ui_file
{
public:
- /* Create a file which writes to both ONE and TWO. ONE will remain
- open when this object is destroyed; but TWO will be closed. */
- tee_file (ui_file *one, ui_file_up &&two);
+ /* Create a file which writes to both ONE and TWO. Ownership of
+ both files is up to the user. */
+ tee_file (ui_file *one, ui_file *two);
~tee_file () override;
void write (const char *buf, long length_buf) override;
@@ -364,7 +364,7 @@ class tee_file : public ui_file
private:
/* The two underlying ui_files. */
ui_file *m_one;
- ui_file_up m_two;
+ ui_file *m_two;
};
/* A ui_file implementation that filters out terminal escape