From 53d757a84f31fb69e9f1c86b7dd621955005d74b Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Fri, 27 Jun 2025 06:12:16 +0800 Subject: [PATCH] ui: fix raylib log message formatting by handing va_list arguments (#35561) * fix raylib log message formatting by handing va_list arguments * dd * improve&simplify * chadder says this works * consis * clean up --------- Co-authored-by: Shane Smiskol --- system/ui/lib/application.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/system/ui/lib/application.py b/system/ui/lib/application.py index d6300c52d0..0546348cdd 100644 --- a/system/ui/lib/application.py +++ b/system/ui/lib/application.py @@ -1,4 +1,5 @@ import atexit +import cffi import os import time import pyray as rl @@ -246,12 +247,29 @@ class GuiApplication: rl.gui_set_style(rl.GuiControl.DEFAULT, rl.GuiControlProperty.BASE_COLOR_NORMAL, rl.color_to_int(rl.Color(50, 50, 50, 255))) def _set_log_callback(self): + ffi_libc = cffi.FFI() + ffi_libc.cdef(""" + int vasprintf(char **strp, const char *fmt, void *ap); + void free(void *ptr); + """) + libc = ffi_libc.dlopen(None) + @rl.ffi.callback("void(int, char *, void *)") def trace_log_callback(log_level, text, args): try: - text_str = rl.ffi.string(text).decode('utf-8') - except (TypeError, UnicodeDecodeError): - text_str = str(text) + text_addr = int(rl.ffi.cast("uintptr_t", text)) + args_addr = int(rl.ffi.cast("uintptr_t", args)) + text_libc = ffi_libc.cast("char *", text_addr) + args_libc = ffi_libc.cast("void *", args_addr) + + out = ffi_libc.new("char **") + if libc.vasprintf(out, text_libc, args_libc) >= 0 and out[0] != ffi_libc.NULL: + text_str = ffi_libc.string(out[0]).decode("utf-8", "replace") + libc.free(out[0]) + else: + text_str = rl.ffi.string(text).decode("utf-8", "replace") + except Exception as e: + text_str = f"[Log decode error: {e}]" if log_level == rl.TraceLogLevel.LOG_ERROR: cloudlog.error(f"raylib: {text_str}")