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