Boost logo

Boost :

From: Brian Braatz (brianb_at_[hidden])
Date: 2005-02-28 16:54:02


First off THANK YOU Johnathon Turkanis for help with "tee" earlier. That
saved me a great amount of pain.

Below is part of what I am trying to use it with.

My next question is what is the best way to deal with an "output" device
having a different cr\lf than another one?

My basic problem is this-

I want to be able to take

cout << "hi" << endl << "there" << endl;

and have it properly give an end of line for the MSVC debugger (the same
issue exists with the windows edit control)

the problem is with both the windows edit control and the MSVC debug out
window, you need to give it a \r\n instead of just a \n

As you will see in my code below, there is "stupidity" (i.e. look for
the memcpy :) )

My question is this- What is your suggested way to do what I am trying
to do.
(code below DOES work)

using namespace std;
using namespace boost::io;

    struct tee : boost::io::multichar_output_filter
    {
        tee(std::ostream& dest) : dest(dest) { }

        template<typename Sink>
        void write(Sink& snk, const char* s, std::streamsize n)
        {
            // Write to the downstream Sink
            boost::io::write(snk, s, n);

            // Write to the stored ostream:
            dest.write(s, n);
        }

        std::ostream& dest;
    };

    struct debug_out_sink : public sink
    {
        debug_out_sink()
        { }
       void write( const char* s, std::streamsize n)
        {
            // copy the incoming data into a new location so we can NULL
terminate
            char * sout = new char[(n+1)*2];
            memcpy(sout,(void*)s,n);
            sout[n] = 0;
            // make a std::string from null terminated string
            std::string ssOut = sout;
            
            // replace all occurances of \n with \r\n
            // this is the end of line sequence used for the win32
debugger
            // (this is also for edit controls on the windows platform)
            boost::replace_all(ssOut,"\n","\r\n");
            OutputDebugString(ssOut.c_str());
            
            // clean up
            delete [] sout;
            sout = NULL;
        }
    };
       
        void Test_Profile()
        {
        using namespace boost::io;
        filtering_ostream out;
        std::ofstream log("log.txt");
        debug_out_sink dbosink;
        out.push(tee(log));
        out.push(tee(std::cout));
        out.push(dbosink);
        
        out <<"LINE ONE" << endl << "Next line" << endl << "third line"
<< endl;
    }


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk