How to use SetTraceLogCallback()? #86
|
I'm trying to use SetTraceLogCallback, but I run into a few issues:
I can easily get the string from the Example code: private static class LogCallback extends TraceLogCallback {
@Override
public void call(int logLevel, BytePointer text, Pointer args) {
}
} |
Replies: 1 comment 1 reply
|
Raylib declares the callback as That explains both symptoms:
If you only need the template, this is all you can do in the Java callback: @Override
public void call(int level, BytePointer text, Pointer args) {
System.err.println(text.getString()); // e.g. "value=%i"
}For the actual expanded message, add a tiny native bridge and bind a second callback whose second argument is static FormattedTraceLogCallback javaLog;
static void bridge_trace_log(int level, const char *fmt, va_list ap)
{
char buf[4096];
va_list copy;
va_copy(copy, ap);
vsnprintf(buf, sizeof buf, fmt, copy);
va_end(copy);
if (javaLog) javaLog(level, buf);
}
void SetFormattedTraceLogCallback(FormattedTraceLogCallback cb)
{
javaLog = cb;
SetTraceLogCallback(cb ? bridge_trace_log : NULL);
}Expose |
TraceLogCallbackis exposing raylib’s nativeprintfmachinery, not a pre-formatted Java string.Raylib declares the callback as
void (*)(int logLevel, const char *text, va_list args)(header). InTraceLog,textis passed unchanged as the format string and the nativeva_listis passed separately. Jaylib therefore generates the third parameter as@ByVal @Cast("va_list*") Pointer(generated Javadoc).That explains both symptoms:
%iis valid for Cprintf, but Java’sFormatteruses%d; more importantly,textis only the template, so replacing%icannot recover the missing arguments.Pointer argsis an opaque, ABI-specificva_list. It has no type metadata, so reading it withgetInt(),getLong(),…