7

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

Jithin
  • 1,108
  • 2
  • 12
  • 26
  • 2
    I suppose you could [freopen](http://www.cplusplus.com/reference/clibrary/cstdio/freopen/) `stdout` before and after each call to the shared library. Performance and threading may be a concern depending on your requirements. – Joe Dec 12 '11 at 18:17
  • 1
    You can use `std::cout.rdbuf()` to redirect output to a file, for example. – lapk Dec 12 '11 at 18:23
  • Also this shared library does not seem like it is of commercial quality. If you have source code for this library you should recompile it with logging disabled. A responsible library will allow this to happen in one place using a preprocessor define. – Joe Dec 12 '11 at 18:24
  • @Joe I am trying to use this inside a php extension which I am writing. If I define it once for the extension, then will it affect any cout coming from other libraries. – Jithin Dec 12 '11 at 18:26
  • `freopen` will affect `stdout` for everything in the same process. I hope that answers your question. – Joe Dec 12 '11 at 18:30
  • 1
    See http://stackoverflow.com/questions/8246317/redirecting-function-output-to-dev-null – szx Dec 12 '11 at 18:51
  • @Jithin: if you're writing the extension, then why not write it not to use `cout`? –  Dec 12 '11 at 19:42
  • @Hurky Extension I am writing, but the C++ library which I am using is not mine. – Jithin Dec 13 '11 at 03:51

2 Answers2

5

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 );
}
lapk
  • 3,838
  • 1
  • 23
  • 28
  • 1
    is there any performance difference between using rdbuf and freeopen? Which one would be a better approach? – Jithin Dec 13 '11 at 03:52
  • @Jithin My understanding was that `cout` needs to be redirected before each library function call, not just once. In that case `rdbuf()` is fastest, I think, - it simply sets a pointer, after opening file once. If `cout` needs to be redirected just once, then both ways (`rdbuf` or `freopen`) are essentially the same. – lapk Dec 13 '11 at 15:59
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.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131