Iostreams Question: Best way to output to win32 debugger\outwindow ?

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; }

My next question is what is the best way to deal with an "output" device having a different cr\lf than another one?
Hi Brian, My suggestion is to create another separate filter which does nothing but convert \n to \r\n and add it to the chain: class newline_conversion_filter : public output_filter { public: template<typename Sink> void put(Sink& dest, int c) { if (c == '\n') { boost::io::put(dest, '\r'); boost::io::put(dest, '\n'); } else { boost::io::put(dest, c); } } }; Does this help? -- Christopher Diggins http://www.ootl.org

Brian Braatz wrote:
First off THANK YOU Johnathon Turkanis for help with "tee" earlier. That saved me a great amount of pain.
Glad to be of service ;-)
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?
Have you tried to use a newline filter: http://www.kangaroologic.com/iostreams/libs/iostreams/doc/?path=6.2.2 ? I'm not familiar with the function OutputDebugString, but you might be able to do this: struct debug_out_sink : boost::iostreams::sink { void write(const char* s, std::streamsize n) { std::string s(s, n); OutputDebugString(s.c_str()); } }; void Test_Profile() { // Note: library now uses namespace iostreams using namespace boost::iostreams; filtering_ostream out; std::ofstream log("log.txt"); debug_out_sink dbosink; out.push(tee(log)); out.push(tee(std::cout)); out.push(newline_filter(newline::windows)); out.push(dbosink); out << "LINE ONE" << endl << "Next line" << endl << "third line" << endl; } Jonathan
participants (3)
-
Brian Braatz
-
christopher diggins
-
Jonathan Turkanis