Use new mpw log sink logging mechanism for JNI logging.
This commit is contained in:
parent
11d6660e5a
commit
9a362f13a3
@ -44,7 +44,6 @@ library {
|
|||||||
def arch = targetMachine.getArchitecture().getName().replace('-', '_')
|
def arch = targetMachine.getArchitecture().getName().replace('-', '_')
|
||||||
|
|
||||||
compile.macros.put("MPW_SODIUM", "1")
|
compile.macros.put("MPW_SODIUM", "1")
|
||||||
compile.macros.put("MPW_LOG", "mpw_log_app")
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// libsodium
|
// libsodium
|
||||||
|
@ -10,50 +10,46 @@
|
|||||||
static JavaVM* _vm;
|
static JavaVM* _vm;
|
||||||
static jobject logger;
|
static jobject logger;
|
||||||
|
|
||||||
void mpw_log_app(LogLevel level, const char *format, ...) {
|
MPLogSink mpw_log_sink_jni;
|
||||||
|
bool mpw_log_sink_jni(const MPLogEvent *record) {
|
||||||
|
bool sunk = false;
|
||||||
|
|
||||||
JNIEnv *env;
|
JNIEnv *env;
|
||||||
if ((*_vm)->GetEnv( _vm, (void **)&env, JNI_VERSION_1_6 ) != JNI_OK)
|
if ((*_vm)->GetEnv( _vm, (void **)&env, JNI_VERSION_1_6 ) != JNI_OK)
|
||||||
return;
|
return sunk;
|
||||||
|
|
||||||
va_list args;
|
|
||||||
va_start( args, format );
|
|
||||||
|
|
||||||
if (logger && (*env)->PushLocalFrame( env, 16 ) == OK) {
|
if (logger && (*env)->PushLocalFrame( env, 16 ) == OK) {
|
||||||
jmethodID method = NULL;
|
jmethodID method = NULL;
|
||||||
jclass Logger = (*env)->GetObjectClass( env, logger );
|
jclass Logger = (*env)->GetObjectClass( env, logger );
|
||||||
if (level >= LogLevelTrace)
|
switch (record->level) {
|
||||||
|
case LogLevelTrace:
|
||||||
method = (*env)->GetMethodID( env, Logger, "trace", "(Ljava/lang/String;)V" );
|
method = (*env)->GetMethodID( env, Logger, "trace", "(Ljava/lang/String;)V" );
|
||||||
else if (level == LogLevelDebug)
|
break;
|
||||||
|
case LogLevelDebug:
|
||||||
method = (*env)->GetMethodID( env, Logger, "debug", "(Ljava/lang/String;)V" );
|
method = (*env)->GetMethodID( env, Logger, "debug", "(Ljava/lang/String;)V" );
|
||||||
else if (level == LogLevelInfo)
|
break;
|
||||||
|
case LogLevelInfo:
|
||||||
method = (*env)->GetMethodID( env, Logger, "info", "(Ljava/lang/String;)V" );
|
method = (*env)->GetMethodID( env, Logger, "info", "(Ljava/lang/String;)V" );
|
||||||
else if (level == LogLevelWarning)
|
break;
|
||||||
|
case LogLevelWarning:
|
||||||
method = (*env)->GetMethodID( env, Logger, "warn", "(Ljava/lang/String;)V" );
|
method = (*env)->GetMethodID( env, Logger, "warn", "(Ljava/lang/String;)V" );
|
||||||
else if (level <= LogLevelError)
|
break;
|
||||||
|
case LogLevelError:
|
||||||
|
case LogLevelFatal:
|
||||||
method = (*env)->GetMethodID( env, Logger, "error", "(Ljava/lang/String;)V" );
|
method = (*env)->GetMethodID( env, Logger, "error", "(Ljava/lang/String;)V" );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
va_list _args;
|
if (method && record->message) {
|
||||||
va_copy( _args, args );
|
// TODO: log file, line & function as markers?
|
||||||
int length = vsnprintf( NULL, 0, format, _args );
|
(*env)->CallVoidMethod( env, logger, method, (*env)->NewStringUTF( env, record->message ) );
|
||||||
va_end( _args );
|
sunk = true;
|
||||||
|
|
||||||
if (length > 0) {
|
|
||||||
size_t size = (size_t) (length + 1);
|
|
||||||
char *message = malloc( size );
|
|
||||||
va_copy( _args, args );
|
|
||||||
if (message && (length = vsnprintf( message, size, format, _args )) > 0)
|
|
||||||
(*env)->CallVoidMethod( env, logger, method, (*env)->NewStringUTF( env, message ) );
|
|
||||||
va_end( _args );
|
|
||||||
mpw_free( &message, (size_t)max( 0, length ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
(*env)->PopLocalFrame( env, NULL );
|
(*env)->PopLocalFrame( env, NULL );
|
||||||
}
|
}
|
||||||
else
|
|
||||||
// Can't log via slf4j, fall back to cli logger.
|
|
||||||
mpw_vlog_cli( level, format, args );
|
|
||||||
|
|
||||||
va_end( args );
|
return sunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
||||||
@ -83,6 +79,8 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
|||||||
else
|
else
|
||||||
mpw_verbosity = LogLevelFatal;
|
mpw_verbosity = LogLevelFatal;
|
||||||
|
|
||||||
|
mpw_log_sink_register( &mpw_log_sink_jni );
|
||||||
|
|
||||||
return JNI_VERSION_1_6;
|
return JNI_VERSION_1_6;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user