@@ -65,6 +65,30 @@ class JniAllocator : public ICubismAllocator {
6565
6666static JniAllocator allocator;
6767static CubismFramework::Option option;
68+ static bool g_useJavaLogger = false ;
69+
70+ static void LogFunction (const char * message) {
71+ if (!g_useJavaLogger) return ;
72+
73+ JNIEnv* env;
74+ bool attached = false ;
75+ if (g_jvm->GetEnv ((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
76+ g_jvm->AttachCurrentThread ((void **)&env, nullptr );
77+ attached = true ;
78+ }
79+
80+ jclass cls = env->FindClass (" dev/eatgrapes/live2d/CubismFramework" );
81+ if (cls) {
82+ jmethodID mid = env->GetStaticMethodID (cls, " onLog" , " (Ljava/lang/String;)V" );
83+ if (mid) {
84+ jstring str = env->NewStringUTF (message);
85+ env->CallStaticVoidMethod (cls, mid, str);
86+ env->DeleteLocalRef (str);
87+ }
88+ }
89+
90+ if (attached) g_jvm->DetachCurrentThread ();
91+ }
6892
6993extern " C" {
7094
@@ -76,9 +100,10 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
76100 return JNI_VERSION_1_6;
77101}
78102
79- JNIEXPORT void JNICALL Java_dev_eatgrapes_live2d_CubismFramework_startUp (JNIEnv* env, jclass clazz) {
80- option.LogFunction = [](const char * message) { };
81- option.LoggingLevel = CubismFramework::Option::LogLevel_Off;
103+ JNIEXPORT void JNICALL Java_dev_eatgrapes_live2d_CubismFramework_startUpNative (JNIEnv* env, jclass clazz, jboolean hasCallback, jint logLevel) {
104+ g_useJavaLogger = hasCallback;
105+ option.LogFunction = LogFunction;
106+ option.LoggingLevel = static_cast <CubismFramework::Option::LogLevel>(logLevel);
82107 option.LoadFileFunction = LoadFile;
83108 option.ReleaseBytesFunction = ReleaseBytes;
84109 CubismFramework::StartUp (&allocator, &option);
0 commit comments