Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppressing cout output with in a function

Tags:

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++.

like image 442
Jithin Avatar asked Dec 12 '11 18:12

Jithin


1 Answers

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 );
}
like image 106
lapk Avatar answered Oct 24 '22 03:10

lapk