Suppress output to cout from linked library

后端 未结 6 1127
感情败类
感情败类 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 :) ...

    0 讨论(0)
  • 2020-12-31 20:22

    Apparently, freopen can do that.

    0 讨论(0)
  • 2020-12-31 20:31

    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.

    0 讨论(0)
  • 2020-12-31 20:34

    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.

    0 讨论(0)
  • 2020-12-31 20:39

    Well nobody seems to have hit on it, here's my linker suggestions:

    1. Interpose libc, provide your own write(), and filter output to file descriptors 1 and 2.
    2. Statically link your own code against libc, and then interpose the shared version to squelch write() as above.
    3. Interpose libc, providing a my_write() function that bypasses write() using dlsym().
    4. Wrap 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.

    Links

    Interposing

    • http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
    • http://www.jayconrod.com/cgi/view_post.py?23
    • http://dictionary.die.net/interposing
    • http://developers.sun.com/solaris/articles/lib_interposers.html

    Wrapping

    • http://www.jayconrod.com/cgi/view_post.py?23
    • http://okmij.org/ftp/syscall-interpose.html
    • Function interposition in Linux without dlsym
    • http://sourceware.org/ml/binutils/2000-09/msg00083.html
    0 讨论(0)
  • 2020-12-31 20:42

    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.

    0 讨论(0)
提交回复
热议问题