android/LogListener: cache the jmethodID

This commit is contained in:
Max Kellermann 2021-05-27 16:12:12 +02:00
parent a83bf97b98
commit 4db882f666
2 changed files with 7 additions and 8 deletions

View File

@ -26,6 +26,10 @@
LogListener::LogListener(JNIEnv *env, jobject obj) noexcept
:Java::GlobalObject(env, obj)
{
Java::Class cls(env, env->GetObjectClass(Get()));
onLogMethod = env->GetMethodID(cls, "onLog", "(ILjava/lang/String;)V");
assert(onLogMethod);
}
void
@ -34,18 +38,11 @@ LogListener::OnLog(JNIEnv *env, int priority,
{
assert(env != nullptr);
Java::Class cls(env, env->GetObjectClass(Get()));
jmethodID method = env->GetMethodID(cls, "onLog",
"(ILjava/lang/String;)V");
assert(method);
std::va_list args;
va_start(args, fmt);
const auto log = FormatStringV(fmt, args);
va_end(args);
env->CallVoidMethod(Get(), method, priority,
env->CallVoidMethod(Get(), onLogMethod, priority,
Java::String(env, log.c_str()).Get());
}

View File

@ -23,6 +23,8 @@
#include "java/Object.hxx"
class LogListener : public Java::GlobalObject {
jmethodID onLogMethod;
public:
LogListener(JNIEnv *env, jobject obj) noexcept;