oaubert / python-vlc

Python vlc bindings
GNU Lesser General Public License v2.1
381 stars 108 forks source link

Not every logs are triggering the callback given to "log_set" #268

Closed azsde closed 8 months ago

azsde commented 8 months ago

Hello,

I am using the python-vlc module on a Raspberry Pi Zero, and I need to be able to detect some events by parsing the logs.

I have successfully redirected the log output to my callback with the following code:

# VLC Logs Callback
libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))
vsnprintf = libc.vsnprintf

vsnprintf.restype = ctypes.c_int
vsnprintf.argtypes = (
    ctypes.c_char_p,
    ctypes.c_size_t,
    ctypes.c_char_p,
    ctypes.c_void_p,
)

@vlc.CallbackDecorators.LogCb
def log_callback(data, level, ctx, fmt, args):

    # Format given fmt/args pair
    BUF_LEN = 1024
    outBuf = ctypes.create_string_buffer(BUF_LEN)
    vsnprintf(outBuf, BUF_LEN, fmt, args)

    # Print it out, or do something else
    print('MY LOGGER: ' + outBuf.raw.replace(b"\x00",b"").decode())

vlc_instance = vlc.Instance("--no-xlib")
vlc_instance.log_set(log_callback, None)

But some logs are not "sent" to this callback but directly printed in the console.

For instance:

python myScript.py
...
MY LOGGER: deinterlace -1, mode auto, is_needed 0
MY LOGGER: looking for vout window module matching "any": 6 candidates
error: XDG_RUNTIME_DIR not set in the environment.
error: XDG_RUNTIME_DIR not set in the environment.
MY LOGGER: no vout window modules matched
MY LOGGER: Opening vout display wrapper
...

Is there a way to catch those error messages straight from the python script ?

I thank you in advance for your help.

Regards,

Azsde.

oaubert commented 8 months ago

These messages are not native from libvlc, they come from other libraries, hence are not included in the libvlc log system. I would guess that redirecting sys.stderr to a StringIO before instanciating libvlc should allow you to catch these.