Skip to content

Commit 3117a8c

Browse files
committed
test: add a logger test
Adds a functional test for the Instance.set_logger method. This will also test the log_get_context, as this is part of the logger output.
1 parent fac5d12 commit 3117a8c

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

tests/test.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import ctypes
3333
import os
3434
import unittest
35+
import io
36+
import re
3537

3638
try:
3739
import urllib.parse as urllib # python3
@@ -203,33 +205,37 @@ def test_meta_get(self):
203205
self.assertEqual(m.get_meta(vlc.Meta.Date), '2013')
204206
self.assertEqual(m.get_meta(vlc.Meta.Genre), 'Sample')
205207

206-
def notest_log_get_context(self):
207-
"""Semi-working test for log_get_context.
208-
209-
It crashes with a Segmentation fault after displaying some
210-
messages. This should be fixed + a better test should be
211-
devised so that we do not clutter the terminal.
212-
"""
213-
libc = ctypes.cdll.LoadLibrary("libc.{}".format("so.6" if os.uname()[0] == 'Linux' else "dylib"))
214-
@vlc.CallbackDecorators.LogCb
215-
def log_handler(instance, log_level, ctx, fmt, va_list):
216-
bufferString = ctypes.create_string_buffer(4096)
217-
libc.vsprintf(bufferString, fmt, ctypes.cast(va_list, ctypes.c_void_p))
218-
msg = bufferString.value.decode('utf-8')
219-
module, _file, _line = vlc.libvlc_log_get_context(ctx)
220-
module = module.decode('utf-8')
221-
try:
222-
logger.warn(u"log_level={log_level}, module={module}, msg={msg}".format(log_level=log_level, module=module, msg=msg))
223-
except Exception as e:
224-
logger.exception(e)
225-
import pdb; pdb.set_trace()
226-
208+
def test_set_logger(self):
209+
vlc_logger = logging.Logger("VLC")
210+
vlc_logger.setLevel(logging.DEBUG)
211+
# push logs in memory
212+
in_mem = io.StringIO()
213+
handler = logging.StreamHandler(in_mem)
214+
formatter = logging.Formatter('%(asctime)s;;;%(name)s;;;%(levelname)s;;;%(vlc_module)s;;;%(file)s;;;%(line)d;;;%(message)s')
215+
handler.setFormatter(formatter)
216+
vlc_logger.addHandler(handler)
227217
instance = vlc.Instance('--vout dummy --aout dummy')
228-
instance.log_set(log_handler, None)
218+
instance.set_logger(vlc_logger)
229219
player = instance.media_player_new()
230220
media = instance.media_new(SAMPLE)
231221
player.set_media(media)
232222
player.play()
223+
player.stop()
224+
handler.flush()
225+
# check logs
226+
# there can be a lot of log message to check
227+
# just try to find a message we are sure to get
228+
# Example:
229+
# 2024-05-03 18:44:31,054;;;VLC;;;DEBUG;;;main;;;<src>;;;<line>;;;creating demux: access='file' demux='any' location='<path>' file='<path>'
230+
pattern = r"(?P<asctime>.*);;;VLC;;;DEBUG;;;main;;;(?P<src>.*);;;(?P<line>.*);;;creating demux: access='file' demux='any' location='(?P<path1>.*)' file='(?P<path2>.*)'"
231+
found = False
232+
for line in in_mem.getvalue().splitlines():
233+
#print(line)
234+
m = re.match(pattern, line)
235+
if m is not None:
236+
found = True
237+
break
238+
self.assertEqual(found, True, "Cannot find a proper log message for demux creation")
233239

234240

235241
if generate is not None:

0 commit comments

Comments
 (0)