Suppress output to cout from linked library

后端 未结 6 1169
感情败类
感情败类 2020-12-31 19:49

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

6条回答
  •  不知归路
    2020-12-31 20:20

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

提交回复
热议问题