Skip to content

Commit 9715a46

Browse files
committed
Catch logs from SDL.
1 parent cbb56b4 commit 9715a46

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

build_sdl.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ def on_directive_handle(
205205
extern "Python" {
206206
// SDL_AudioCallback callback.
207207
void _sdl_audio_callback(void* userdata, Uint8* stream, int len);
208+
void _sdl_log_output_function(void *userdata, int category, SDL_LogPriority priority, const char *message);
208209
}
209210
"""
210211

tcod/sdl/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import annotations
2+
3+
import logging
4+
from typing import Any
5+
6+
from tcod.loader import ffi, lib
7+
8+
logger = logging.getLogger(__name__)
9+
10+
_LOG_PRIORITY = {
11+
int(lib.SDL_LOG_PRIORITY_VERBOSE): logging.DEBUG,
12+
int(lib.SDL_LOG_PRIORITY_DEBUG): logging.DEBUG,
13+
int(lib.SDL_LOG_PRIORITY_INFO): logging.INFO,
14+
int(lib.SDL_LOG_PRIORITY_WARN): logging.WARNING,
15+
int(lib.SDL_LOG_PRIORITY_ERROR): logging.ERROR,
16+
int(lib.SDL_LOG_PRIORITY_CRITICAL): logging.CRITICAL,
17+
}
18+
19+
20+
@ffi.def_extern() # type: ignore
21+
def _sdl_log_output_function(_userdata: Any, category: int, priority: int, message: Any) -> None:
22+
"""Pass logs sent by SDL to Python's logging system."""
23+
logger.log(_LOG_PRIORITY.get(priority, 0), "%i:%s", category, ffi.string(message).decode("utf-8"))
24+
25+
26+
lib.SDL_LogSetOutputFunction(lib._sdl_log_output_function, ffi.NULL)

0 commit comments

Comments
 (0)