From 10ceb254b50e789c7d12c37f058e5edbdccb70a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Juho=20V=C3=A4h=C3=A4-Herttua?= Date: Sat, 26 May 2012 09:54:06 +0300 Subject: [PATCH] Add log level and callback functions to the python bindings --- src/bindings/python/Shairplay.py | 33 ++++++++++++++++++++++++++++++++ src/test/test.py | 5 +++++ 2 files changed, 38 insertions(+) diff --git a/src/bindings/python/Shairplay.py b/src/bindings/python/Shairplay.py index 1396524..69aec6b 100644 --- a/src/bindings/python/Shairplay.py +++ b/src/bindings/python/Shairplay.py @@ -28,6 +28,18 @@ import platform from ctypes import * +class RaopLogLevel: + EMERG = 0 + ALERT = 1 + CRIT = 2 + ERR = 3 + WARNING = 4 + NOTICE = 5 + INFO = 6 + DEBUG = 7 + +raop_log_callback_prototype = CFUNCTYPE(None, c_int, c_char_p) + audio_init_prototype = CFUNCTYPE(py_object, c_void_p, c_int, c_int, c_int) audio_process_prototype = CFUNCTYPE(None, c_void_p, c_void_p, c_void_p, c_int) audio_destroy_prototype = CFUNCTYPE(None, c_void_p, c_void_p) @@ -65,6 +77,10 @@ def InitShairplay(libshairplay): # Initialize raop related functions libshairplay.raop_init.restype = c_void_p libshairplay.raop_init.argtypes = [c_int, POINTER(RaopNativeCallbacks), c_char_p] + libshairplay.raop_set_log_level.restype = None + libshairplay.raop_set_log_level.argtypes = [c_void_p, c_int] + libshairplay.raop_set_log_callback.restype = None + libshairplay.raop_set_log_callback.argtypes = [c_void_p, raop_log_callback_prototype] libshairplay.raop_is_running.restype = c_int libshairplay.raop_is_running.argtypes = [c_void_p] libshairplay.raop_start.restype = c_int @@ -145,6 +161,12 @@ class RaopCallbacks: pass class RaopService: + def log_callback_cb(self, level, message): + if self.log_callback != None: + self.log_callback(level, message) + else: + print message + def audio_init_cb(self, cls, bits, channels, samplerate): session = self.callbacks.audio_init(bits, channels, samplerate) self.sessions.append(session) @@ -201,6 +223,11 @@ class RaopService: if self.instance == None: raise RuntimeError("Initializing library failed") + # We need to hold a reference to the log callback wrapper + self.log_callback_pointer = raop_log_callback_prototype(self.log_callback_cb) + self.libshairplay.raop_set_log_callback(self.instance, self.log_callback_pointer) + self.log_callback = None + def __del__(self): if self.instance != None: self.libshairplay.raop_destroy(self.instance) @@ -212,6 +239,12 @@ class RaopService: else: return False + def set_log_level(self, level): + self.libshairplay.raop_set_log_level(self.instance, level) + + def set_log_callback(self, log_callback): + self.log_callback = log_callback + def start(self, port, hwaddrstr, password=None): port = c_ushort(port) hwaddr = create_string_buffer(hwaddrstr, len(hwaddrstr)) diff --git a/src/test/test.py b/src/test/test.py index 227321a..45eab3b 100644 --- a/src/test/test.py +++ b/src/test/test.py @@ -20,7 +20,12 @@ class SampleCallbacks(RaopCallbacks): shairplay = LoadShairplay(".") callbacks = SampleCallbacks() +def log_callback(level, message): + print "Level", level, ":", message + raop = RaopService(shairplay, 10, callbacks) +raop.set_log_level(RaopLogLevel.DEBUG) +raop.set_log_callback(log_callback) port = raop.start(5000, hwaddr) dnssd = DnssdService(shairplay) -- 2.34.1