I'd like to make a logger for my GUI application. I currently use std::cout and std::cerr, but I'd like to replace these with an arbitrary buffer which isn't connected to stdout or stderr. My requirements are:
1. On the user-side, the usage should be identical to any ostream object (including support for std::endl).
2. When std::flush is received, it "signals" my logger so I can run this line of code: LogWindow->setText( mybuf.str() )
I need an idea for how this can be done. I think I need to extend a std::basic_stringbuf<char>, but which virtual method do I need to implement in order to print to my custom window?
My first attempt is this. I'm testing with std::cout, but the output is empty and running in the debugger doesn't trigger a breakpoint in my sync() method.
Definitely interesting stuff you have there. I'm running it through a debugger to see what I can do with it.
I'm not looking to re-direct std::cout, cerr, clog. I'm looking to implement my own ostream objects (I have more than 3 categories of windows which outputs will fit into. One for script outputs, one for developer outputs, one for processing outputs, and one for a LaTeX plugin).
While running a skeleton of your Debug.hpp, I had some fully business where pbase() and pptr() were always returning NULL. I checked it in the synch and overflow overrides so I'm not sure why that would happen.
If you don't set the three streambuf pointers in your buffer's constructor (as in the example in the original post), they will be null and no output will take place.
Thanks guys, now I know what's going on. First I tried to set the internal buffers (who's names aren't published, but I found the gcc implementation names in the header), but then I found setp() from LB's implementation. I definately still have problems with my overflow case, but I'm getting closer. I don't think I need further hints.
Line 48 above std::cout << pbase(); should have been std::cout << out.str();. Stupid mistake! That's what I get for drinking several bottles of 11% beer and coding at 2am.