public inbox for sid@sourceware.org
 help / color / mirror / Atom feed
From: Jim Blandy <jimb@redhat.com>
To: sid@sources.redhat.com
Subject: PATCH: make trace_stream better preserve std::ofstream semantics
Date: Mon, 19 Jan 2004 21:59:00 -0000	[thread overview]
Message-ID: <vt2r7xvob26.fsf@zenia.home> (raw)


If the user redirects trace output to a file using something like:

    $ sid ... -e "set basic-0/cpu trace-filename trace.hello.basic"

then trace.hello.basic remains an empty file.

The problem is (I think) that basic_cpu::update_trace_destination
tries to close basic_cpu::trace_stream when it is already closed.

cpu_trace_stream is a subclass of std::ofstream, and does not override
its 'close' method.  According to ISO C++, std::ofstream::close sets
the stream's failbit if rdbuf ()->close fails (returns null).
std::basic_filebuf::close will return null if the filebuf is not open.

The first time basic_cpu::update_trace_destination gets called, cout_p
is true, and the base class's stream itself has never been opened.
The call to close sets the failbit, and subsequent output is ignored.

This patch overrides some more functions in cpu_trace_stream to make
their behavior better match that of its base class.  In particular,
the stream is treated as open if output is being directed to either
cout or the stream itself.

This doesn't completely reproduce the behavior of std::ofstream.  In
particular, it doesn't set failbit quite as often as it should, to be
completely consistent.


Rather than subclassing std::ofstream and doing this odd redirection
stuff, wouldn't it be simpler to just make trace_stream an instance of
plain old std::ofstream, and then call rdbuf () to set its streambuf
to either cout's streambuf, or a filebuf for the trace file?


sid/include/ChangeLog:
2004-01-13  Jim Blandy  <jimb@redhat.com>

	* sidcpuutil.h (sidutil::basic_cpu::cpu_trace_stream::is_open)
	(sidutil::basic_cpu::cpu_trace_stream::close): Override (non-virtual)
	definitions from std::ofstream, to better preserve std::ofstream's
	behavior, taking cout_p into account.

Index: sid/include/sidcpuutil.h
===================================================================
RCS file: /cvs/cvsfiles/devo/sid/include/sidcpuutil.h,v
retrieving revision 1.47.2.1
diff -c -r1.47.2.1 sidcpuutil.h
*** sid/include/sidcpuutil.h	22 Oct 2003 00:35:32 -0000	1.47.2.1
--- sid/include/sidcpuutil.h	14 Jan 2004 05:10:37 -0000
***************
*** 222,231 ****
--- 222,241 ----
  	:std::ofstream (filename.c_str ()), cout_p (false) {}
        void divert_to_file () { cout_p = false; }
        void divert_to_cout () { cout_p = true; }
+       bool is_open ()
+       {
+         return cout_p || std::ofstream::is_open ();
+       }
        void open (const std::string& filename)
        {
  	std::ofstream::open (filename.c_str (), std::ios::app);
  	cout_p = false;
+       }
+       void close ()
+       {
+         if (std::ofstream::is_open ())
+           std::ofstream::close ();
+         cout_p = false;
        }
        void end_line ()
        {

             reply	other threads:[~2004-01-19 21:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-19 21:59 Jim Blandy [this message]
2004-01-20  1:02 ` Ben Elliston

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=vt2r7xvob26.fsf@zenia.home \
    --to=jimb@redhat.com \
    --cc=sid@sources.redhat.com \
    /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).