I am writing a GL application that uses external libraries that print errors on the console. I want to catch this and print it in the game console.
PS: Sorry for my bad English ....
There are two main approaches you could take to do this:
If all libraries use std::cout for the IO you want to capture, you can write your own basic_streambuf . Then you can just call std::cout.rdbuf(mybufinst); to replace a stream buffer, for example, using std::basic_stringbuf :
std::cout
basic_streambuf
std::cout.rdbuf(mybufinst);
std::basic_stringbuf
#include <sstream> #include <iostream> int main() { static std::basic_stringbuf<std::ostream::char_type> buf; std::cout.rdbuf(&buf); std::cout << "Hello captured world!\n"; std::cerr << "Stole: " << buf.str() << std::endl; }
You can use a platform-specific approach, for example. on POSIX systems, dup2() allows you to replace the file descriptor with another or on Windows with SetStdHandle() . You would like to use channels, not just another file, probably, and you need to be very careful in blocking (so you probably need to allocate a dedicated stream)
dup2()
SetStdHandle()