How can I process a va_list argument with Java, having received it from the native library?
I am using a C library that facilitates logging through a callback function. The library is libghoto2 and I am using a JNA wrapper libgphoto2-java to access its features.
See the errordumper
method in this C file for an example of how the logging should be done.
I've managed to add a Java-written callback function using the library's gp_log_add_func
. The only problem is, the callback function's signature contains a va_list
argument that I don't know how to process.
As the C example from earlier shows, va_list args
is passed directly into vfprintf
. Reading the vfprintf
manual it becomes clear that it is some sort of iterable data structure that has "been initialized with the va_start
macro" and after iterating using va_arg
a cleanup with va_end
is required.
But the only way I found for keeping the JVM from crashing, is to make the args
parameter of type com.sun.jna.Pointer
while a String[]
or Object[]
would be more suitable.
How do I get the data out of this va_list?
N.B. In order to get access to gp_log_add_func, I added some Java code:
Additions to org.gphoto2.jna.GPhoto2Native:
int gp_log_add_func(int logLevel, LogFunc func, Pointer data);
Created org.gphoto2.jna.LogFunc:
public interface LogFunc extends Callback {
public static final int GP_LOG_ERROR = 0;
public static final int GP_LOG_VERBOSE = 1;
public static final int GP_LOG_DEBUG = 2;
public static final int GP_LOG_DATA = 3;
public static final int GP_LOG_ALL = GP_LOG_DATA;
//the args argument is a va_list
public void log(int logLevel, String domain, String format, Pointer args, Pointer data);
}
The implementation and usage of org.gphoto2.jna.LogFunc:
LogFunc callback = new LogFunc() {
public void log(int logLevel, String domain, String format, Pointer args, Pointer data) {
System.out.println("[" + domain + "] " + format);
System.out.println(args.toString());
}
};
GPhoto2Native.INSTANCE.gp_log_add_func(LogFunc.GP_LOG_ALL, callback, null);