|
32 | 32 | import ctypes
|
33 | 33 | import os
|
34 | 34 | import unittest
|
| 35 | +import io |
| 36 | +import re |
35 | 37 |
|
36 | 38 | try:
|
37 | 39 | import urllib.parse as urllib # python3
|
@@ -203,33 +205,37 @@ def test_meta_get(self):
|
203 | 205 | self.assertEqual(m.get_meta(vlc.Meta.Date), '2013')
|
204 | 206 | self.assertEqual(m.get_meta(vlc.Meta.Genre), 'Sample')
|
205 | 207 |
|
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) |
227 | 217 | instance = vlc.Instance('--vout dummy --aout dummy')
|
228 |
| - instance.log_set(log_handler, None) |
| 218 | + instance.set_logger(vlc_logger) |
229 | 219 | player = instance.media_player_new()
|
230 | 220 | media = instance.media_new(SAMPLE)
|
231 | 221 | player.set_media(media)
|
232 | 222 | 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") |
233 | 239 |
|
234 | 240 |
|
235 | 241 | if generate is not None:
|
|
0 commit comments