Something like this, just create function wrappers for library calls that will 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 );
}
source
share