I am using a shared library whose functions are doing std::cout everywhere. Is is possible to do anything at the caller level wherein I can suppress the cout outout or redirect it to some location?
Is it even possible to attempt such a thing in c++.
I am using a shared library whose functions are doing std::cout everywhere. Is is possible to do anything at the caller level wherein I can suppress the cout outout or redirect it to some location?
Is it even possible to attempt such a thing in c++.
Something like this, just make function wrappers for your library calls that would redirect cout.
int main( void )
{
std::ofstream lStream( "garbage.txt" );
std::streambuf* lBufferOld = std::cout.rdbuf();
std::cout.rdbuf( lStream.rdbuf() );
std::cout << "Calling library function" << std::endl;
std::cout.rdbuf( lBufferOld );
std::cout << "Normal output" << std::endl;
std::cout.rdbuf( lStream.rdbuf() );
std::cout << "Calling another library function" << std::endl;
std::cout.rdbuf( lBufferOld );
std::cout << "Another normal output" << std::endl;
lStream.close();
return ( 0 );
}
You could always filter all I/O by creating a class to handle the output. Given the class might be used application-wide, a static class might be in order, but you could instantiate the an instance of the class as needed.
In addition to writing something or not to cout or even choosing a different output, based on the argument string, the class might also format the text based on the kind of output chosen.
I looked at ostream and offhand did not see any way you could modify cout directly. You've encountered a need that has come up before, so hopefully someone else reading this may have better ideas on creating the class I suggested.