digitalmars.D.learn - Redirecting C++ ostreams
- Jeremy DeHaan (12/12) Jun 11 2013 Hey guys,
- Craig Dillabaugh (3/16) Jun 12 2013 Do you have access to the source code of the library? Or are you
- Jeremy DeHaan (8/10) Jun 12 2013 I do some-what.
- Jeremy DeHaan (18/18) Jun 13 2013 Not sure if anyone cares too much since I doubt this will come up
Hey guys, I have something I am curious about, but haven't had much luck with when doing research and experimenting. Basically, I am working with a library that uses ostreams internally and I want to somehow redirect that to what ever stderr is pointing to. The reason I am trying to do this is because I was considering the user might want to have some sort of log they want error messages written to, and if they redirect stderr to a file I'd like the underlying ostreams to be directed to the same location. Any ideas or suggestions would be welcome! Thanks in advance
Jun 11 2013
On Wednesday, 12 June 2013 at 04:05:22 UTC, Jeremy DeHaan wrote:Hey guys, I have something I am curious about, but haven't had much luck with when doing research and experimenting. Basically, I am working with a library that uses ostreams internally and I want to somehow redirect that to what ever stderr is pointing to. The reason I am trying to do this is because I was considering the user might want to have some sort of log they want error messages written to, and if they redirect stderr to a file I'd like the underlying ostreams to be directed to the same location. Any ideas or suggestions would be welcome! Thanks in advanceDo you have access to the source code of the library? Or are you just linking to it?
Jun 12 2013
On Wednesday, 12 June 2013 at 16:15:08 UTC, Craig Dillabaugh wrote:Do you have access to the source code of the library? Or are you just linking to it?I do some-what. I don't want to change the source code of the library itself, but I can change the C/C++ code of the binding that is used by D. If nothing else, I suppose I could write to the ostream directly, but I thought that it would be nice to be able to use stderr in all of the D code.
Jun 12 2013
Not sure if anyone cares too much since I doubt this will come up very often, but on the off chance it does I came up with a solution that will at least work for me. In my C/C++ code I created two variables; std::ostringstream outputStream and std::string outputString. outputStream is what I used to redirect the underlying ostream, and then I made a function as follows: const char* getOutput() { outputString = outputStream.str(); outputStream.str(""); return outputString.c_str(); } And then in my D code(after linking it to my C code) I do one of these after I know something was written to the ostream: stderr.write(text(getOutput())); This way I can redirect stderr wherever I want and still send the contents of the ostream to the same place.
Jun 13 2013