I need to link my C++ programs against a couple shared libraries which generate way too much output to std::cout
and std::cerr
rendering them both
Three ideas (none of which I really like ...):
You can change the buffer cout
/cerr
are writing to by using rdbuf()
. You could do this everytime just before you're calling a function in the library and resetting it afterwards (maybe using wrapper functions).
You could change the buffer permanently and use different cout/cerr objects for your own application.
You could use modified standard header files for compiling the libraries. They could define new global stream objects cout_new
and use macros to redefine cout
to cout_new
. You could just tell the compiler to use new new version of the header files just for compiling the libraries (so you don't have to modify their source code).
As I said, none of these solutions is "clean", but you asked for it :) ...
Apparently, freopen can do that.
Since stdout (file descriptor 1) and stderr (file descriptor 2) are valid for the whole process and you can't make one part of the program have it point to a different file, there's only one way to do it: use dup(2)
to duplicate them and use those file descriptors in your own code. Close the fd's 1 and 2, open
/dev/null for writing and use dup2
to try to set them to 1 or 2 respectively if it isn't already. Pretty ugly, but that'd work.
If they are really outputting through std::cout
and std::cerr
then you could replace those object's stream buffers, but you would have to redirect your own program's output through other streams. See this question for how to do this.
However, if they use std::printf()
etc. then this won't work.
Well nobody seems to have hit on it, here's my linker suggestions:
write()
, and filter output to file descriptors 1
and 2
.write()
as above.my_write()
function that bypasses write()
using dlsym()
.write
when linking the shared libraries by passing -Wl,--wrap=write
. Then squelch any output to file descriptors 1
and 2
in a function called __wrap_write
. Other file descriptors should call through to __real_write
.Note that for those that aren't aware, file descriptors 1
and 2
correspond to stdout
and stderr
, which are eventually written to in the cout
/cerr
machinery. Often this is implemented cout
calls fwrite
which calls write
, with varying levels of buffering and shenanigans at the different levels.
Your best bet is option 4, the downside is you must tweak the final link for the shared libraries.
Next best is option 2 above, the downside is your final executable is much bigger, but don't have to use silly functions in your own code.
Interposing
Wrapping
If you don't need to use either yourself, the easiest thing to do would be to just redirect standard output and standard error from the command line when you start your program.
$ myprog >/dev/null 2>&1
If you do want to use them yourself, the trick would be to change the streambuf they use. There's some code and discussion about how to do that here. Its really too long to post here.