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 <shane@smiskol.com>
pull/35571/merge
Dean Lee 3 days ago committed by GitHub
parent fa5fce465a
commit 53d757a84f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      system/ui/lib/application.py

@ -1,4 +1,5 @@
import atexit import atexit
import cffi
import os import os
import time import time
import pyray as rl 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))) 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): 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 *)") @rl.ffi.callback("void(int, char *, void *)")
def trace_log_callback(log_level, text, args): def trace_log_callback(log_level, text, args):
try: try:
text_str = rl.ffi.string(text).decode('utf-8') text_addr = int(rl.ffi.cast("uintptr_t", text))
except (TypeError, UnicodeDecodeError): args_addr = int(rl.ffi.cast("uintptr_t", args))
text_str = str(text) 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: if log_level == rl.TraceLogLevel.LOG_ERROR:
cloudlog.error(f"raylib: {text_str}") cloudlog.error(f"raylib: {text_str}")

Loading…
Cancel
Save