Improved log handling

This commit is contained in:
jaseg 2016-02-10 17:38:38 +01:00
parent ec91bf3e57
commit 5605380fcb

15
mpv.py
View file

@ -148,7 +148,9 @@ class MpvEventLogMessage(Structure):
('text', c_char_p)] ('text', c_char_p)]
def as_dict(self): def as_dict(self):
return { name: getattr(self, name) for name, _t in self._fields_ } return { 'prefix': self.prefix.decode(),
'level': self.level.decode(),
'text': self.text.decode().rstrip() }
class MpvEventEndFile(c_int): class MpvEventEndFile(c_int):
EOF_OR_INIT_FAILURE = 0 EOF_OR_INIT_FAILURE = 0
@ -272,7 +274,7 @@ def load_lua():
class MPV: class MPV:
""" See man mpv(1) for the details of the implemented commands. """ """ See man mpv(1) for the details of the implemented commands. """
def __init__(self, **kwargs): def __init__(self, log_handler=None, **kwargs):
""" Create an MPV instance. """ Create an MPV instance.
Any kwargs given will be passed to mpv as options. """ Any kwargs given will be passed to mpv as options. """
@ -290,11 +292,17 @@ class MPV:
self._playback_cond.notify_all() self._playback_cond.notify_all()
if devent['event_id'] == MpvEventID.PROPERTY_CHANGE: if devent['event_id'] == MpvEventID.PROPERTY_CHANGE:
self._property_handlers[devent['reply_userdata']](devent['event']) self._property_handlers[devent['reply_userdata']](devent['event'])
if devent['event_id'] == MpvEventID.LOG_MESSAGE and log_handler is not None:
ev = devent['event']
log_handler('{}: {}: {}'.format(ev['level'], ev['prefix'], ev['text']))
for callback in self.event_callbacks: for callback in self.event_callbacks:
callback.call() callback.call()
self._event_thread = threading.Thread(target=event_loop, daemon=True) self._event_thread = threading.Thread(target=event_loop, daemon=True)
self._event_thread.start() self._event_thread.start()
if log_handler is not None:
self.set_loglevel('terminal-default')
_mpv_set_option_string(self.handle, b'audio-display', b'no') _mpv_set_option_string(self.handle, b'audio-display', b'no')
istr = lambda o: ('yes' if o else 'no') if type(o) is bool else str(o) istr = lambda o: ('yes' if o else 'no') if type(o) is bool else str(o)
for k,v in kwargs.items(): for k,v in kwargs.items():
@ -309,6 +317,9 @@ class MPV:
# def __del__(self): # def __del__(self):
# _mpv_terminate_destroy(self.handle) # _mpv_terminate_destroy(self.handle)
def set_loglevel(self, level):
_mpv_request_log_messages(self.handle, level.encode())
def command(self, name, *args): def command(self, name, *args):
""" Execute a raw command """ """ Execute a raw command """
args = [name.encode()] + [ str(arg).encode() for arg in args if arg is not None ] + [None] args = [name.encode()] + [ str(arg).encode() for arg in args if arg is not None ] + [None]