Suppress output to cout from linked library

2019-02-09 19:14发布

问题:

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.

回答1:

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


回答2:

Apparently, freopen can do that.



回答3:

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.



回答4:

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.



回答5:

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



回答6:

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.



标签: c++ linux linker