You are not logged in.

#1 2009-09-03 14:09:17

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 861

C++: how to disable console output?

Hi, I'm facing a stupid problem. I have to work with a crappy C++ library which prints messages on screen using cout. I want to get rid of these messages, but I cannot modify the code (well, I could, but I cannot reinstall it so I have to live with the verbose library). My question is: is it possible to "mute" cout before invoking the library, so that it does not output anything? I'm thinking about redirecting it to a null device or something like this. Or maybe there's a nicer way (I hope tongue)?
Thanks

Offline

#2 2009-09-03 15:07:22

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: C++: how to disable console output?

From Here, you may be able to do something like:

  int main() {
    std::streambuf* cout_sbuf = std::cout.rdbuf(); // save original sbuf
    std::ofstream   fout("/dev/null");
    std::cout.rdbuf(fout.rdbuf()); // redirect 'cout' to a 'fout'
    // ...
    std::cout.rdbuf(cout_sbuf); // restore the original stream buffer
  }

Offline

#3 2009-09-03 17:06:23

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: C++: how to disable console output?

Might want to do something similar to cerr if you want to be really sure nothing comes out of the library.

Offline

#4 2009-09-04 07:39:30

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 861

Re: C++: how to disable console output?

@Cerebral: thank you very much!
@scio: I only have to "mute" stdout, because this stupid library prints some useless warning messages on it, without having any "verbose" option to disable it. In my opinion, a library should never print to the stdout, or at least give the option to turn off the output.

Offline

#5 2009-09-04 17:11:06

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: C++: how to disable console output?

Whatever works for you.  I prefer libraries that allow you to set message handlers or read from a variable for errors.

In my opinion a library should never print anything, but if all you are worried about is stdout then you are all set smile

Edit: Oh, and you might want to add [SOLVED] to the subject.

Last edited by scio (2009-09-04 17:11:31)

Offline

Board footer

Powered by FluxBB