Implement VLC logging

This commit is contained in:
Keith Edmunds 2024-07-28 19:45:55 +01:00
parent b423ab0624
commit 3c01fb63c3

View File

@ -1,7 +1,9 @@
# Standard library imports # Standard library imports
from __future__ import annotations from __future__ import annotations
import ctypes
import datetime as dt import datetime as dt
import platform
import threading import threading
from time import sleep from time import sleep
from typing import Optional from typing import Optional
@ -169,6 +171,36 @@ class _FadeTrack(QRunnable):
self.player.stop() self.player.stop()
# Define the VLC callback function type
VLC_LOG_CB = ctypes.CFUNCTYPE(
None,
ctypes.c_void_p,
ctypes.c_int,
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_void_p,
)
# Determine the correct C library for vsnprintf based on the platform
if platform.system() == "Windows":
libc = ctypes.CDLL("msvcrt")
elif platform.system() == "Linux":
libc = ctypes.CDLL("libc.so.6")
elif platform.system() == "Darwin": # macOS
libc = ctypes.CDLL("libc.dylib")
else:
raise OSError("Unsupported operating system")
# Define the vsnprintf function
libc.vsnprintf.argtypes = [
ctypes.c_char_p,
ctypes.c_size_t,
ctypes.c_char_p,
ctypes.c_void_p,
]
libc.vsnprintf.restype = ctypes.c_int
class _Music: class _Music:
""" """
Manage the playing of music tracks Manage the playing of music tracks
@ -183,6 +215,31 @@ class _Music:
self.start_dt: Optional[dt.datetime] = None self.start_dt: Optional[dt.datetime] = None
self.player_count: int = 0 self.player_count: int = 0
# Set up logging
self._set_vlc_log()
@VLC_LOG_CB
def log_callback(data, level, ctx, fmt, args):
try:
# Create a ctypes string buffer to hold the formatted message
buf = ctypes.create_string_buffer(1024)
# Use vsnprintf to format the string with the va_list
libc.vsnprintf(buf, len(buf), fmt, args)
# Decode the formatted message
message = buf.value.decode("utf-8", errors="replace")
log.error("VLC logging: " + message)
except Exception as e:
log.error(f"Error in VLC log callback: {e}")
def _set_vlc_log(self):
try:
vlc.libvlc_log_set(self.VLC, self.log_callback, None)
log.info("VLC logging set up successfully")
except Exception as e:
log.error(f"Failed to set up VLC logging: {e}")
def adjust_by_ms(self, ms: int) -> None: def adjust_by_ms(self, ms: int) -> None:
"""Move player position by ms milliseconds""" """Move player position by ms milliseconds"""