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 useless for my uses. I have access to the C++ source code of these libraries, but cannot modify them.
Is there a way to redirect their output to a different stream or suppress it when linked against my code? I would prefer a clean way in C++, but fearing that that would be impossible I will also be happy with dirty linker hacks. Also a “proxy libstdc++” would be fine as a last resort.
I am working with a GNU toolchain (g++, libtool, ld) under Linux.
Well nobody seems to have hit on it, here’s my linker suggestions:
write(), and filter output to file descriptors1and2.write()as above.my_write()function that bypasseswrite()usingdlsym().writewhen linking the shared libraries by passing-Wl,--wrap=write. Then squelch any output to file descriptors1and2in a function called__wrap_write. Other file descriptors should call through to__real_write.Note that for those that aren’t aware, file descriptors
1and2correspond tostdoutandstderr, which are eventually written to in thecout/cerrmachinery. Often this is implementedcoutcallsfwritewhich callswrite, 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.
Links
Interposing
Wrapping