0

I have a GUI application in Python 2.7 using Gtk+ 3.10.8 running on Linux Mint 17.1.

The application uses the python bindings for the libvlc library to embed a video player. The libvlc library uses libav for its video handling.

My problem is that libav is extremely verbose and generates a constant stream of warnings, and libvlc does not provide any mechanism to alter the verbosity or suppress it.

Obviously, one can turn off the noise by running the python script thus (for example):

$ ./myprogram.py &> /dev/null

I would like to be able to achieve the same effect programmatically from within the program. I'd be happy simply to suppress all access from external libraries to stdout, but ultimately it would be better to capture that output and implement some logging.

All the examples I can find on this subject assume more direct control of the external process, i.e. such as declaring it as a subprocess. Is there another way to do it when I don't have direct access to the actual library in question (libav) from my Python code?

Edit: Thanks to everyone's help, the correct answer can be found at: Python version of freopen(), answer #3.

You just have to make sure that you are not running your code from an IDE that interferes with stdout in any way. Interactive IDEs tend to do that, and will throw an error like:

AttributeError: FileWrapper instance has no attribute 'fileno'
Community
  • 1
  • 1

2 Answers2

0

The easiest solution is probably to close stdout and open /dev/null as the new stdout. This should hopefully close the underlying filedescriptor used for stdout which means next file open will reuse that descriptor.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Thanks, though you didn't explain how exactly to do that (sorry, I'm a bit of a beginner), this was still the right answer. My actual problem was that I had already tried a source code example that did exactly that (without realizing) but it did not run because **I was running from an IDE which pre-empted stdout**. I went back to that example (and a bunch of others) and finally figured out what was going on. The example is [here](http://stackoverflow.com/questions/16582194/python-version-of-freopen), answer #3. – Chris Harrington Feb 06 '15 at 05:21
0

If you can call "C" library functions from your application you can use freopen() to redirect the standard output and standard error streams from your process to your own log files or to /dev/null I suppose.

The way I do this is:

FILE *stdout = fdopen(1, "w");
FILE *myout  = freopen("out.log", "w", stdout);

Similar for stderr, using 2 instance of 1 for the file descriptor parameter:

FILE *stderr = fdopen(2, "w");
FILE *myerr  = freopen("err.log", "w", stdout);

I do exactly this in my Java bindings for LibVLC.

caprica
  • 3,902
  • 4
  • 19
  • 39
  • Thanks, C is still a bit beyond me. However, searching for info on freopen() did lead me back to a source sample I'd seen yesterday that wrote a Python version of freopen(), and @JoachimPileborg 's answer below explained what that sample was doing. After figuring out that my IDE was preventing the code from running, I got [this example](http://stackoverflow.com/questions/16582194/python-version-of-freopen) to work. – Chris Harrington Feb 06 '15 at 05:25