diff --git a/native/jni/Android.mk b/native/jni/Android.mk index f89eea735..d78da969b 100644 --- a/native/jni/Android.mk +++ b/native/jni/Android.mk @@ -79,8 +79,9 @@ LATIN_IME_CORE_SRC_FILES := \ typing_traversal.cpp \ typing_weighting.cpp) \ $(addprefix utils/, \ + autocorrection_threshold_utils.cpp \ char_utils.cpp \ - autocorrection_threshold_utils.cpp) + log_utils.cpp) LOCAL_SRC_FILES := \ $(LATIN_IME_JNI_SRC_FILES) \ diff --git a/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h b/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h index 0fd4f6961..cbea18f90 100644 --- a/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h +++ b/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h @@ -30,7 +30,7 @@ namespace latinime { class BinaryDictionaryInfo { public: - BinaryDictionaryInfo(JNIEnv *env, const uint8_t *const dictBuf, + AK_FORCE_INLINE BinaryDictionaryInfo(JNIEnv *env, const uint8_t *const dictBuf, const int dictSize, const int mmapFd, const int dictBufOffset, const bool isUpdatable) : mDictBuf(dictBuf), mDictSize(dictSize), mMmapFd(mmapFd), mDictBufOffset(dictBufOffset), mIsUpdatable(isUpdatable), diff --git a/native/jni/src/utils/log_utils.cpp b/native/jni/src/utils/log_utils.cpp new file mode 100644 index 000000000..5ab2b2862 --- /dev/null +++ b/native/jni/src/utils/log_utils.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2013, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "log_utils.h" + +#include +#include + +#include "defines.h" + +namespace latinime { + /* static */ void LogUtils::logToJava(JNIEnv *const env, const char *const format, ...) { + static const char *TAG = "LatinIME:LogUtils"; + const jclass androidUtilLogClass = env->FindClass("android/util/Log"); + if (!androidUtilLogClass) { + // If we can't find the class, we are probably in off-device testing, and + // it's expected. Regardless, logging is not essential to functionality, so + // we should just return. However, FindClass has thrown an exception behind + // our back and there is no way to prevent it from doing that, so we clear + // the exception before we return. + env->ExceptionClear(); + return; + } + const jmethodID logDotIMethodId = env->GetStaticMethodID(androidUtilLogClass, "i", + "(Ljava/lang/String;Ljava/lang/String;)I"); + if (!logDotIMethodId) { + env->ExceptionClear(); + if (androidUtilLogClass) env->DeleteLocalRef(androidUtilLogClass); + return; + } + const jstring javaTag = env->NewStringUTF(TAG); + + static const int DEFAULT_LINE_SIZE = 128; + char fixedSizeCString[DEFAULT_LINE_SIZE]; + va_list argList; + va_start(argList, format); + // Get the necessary size. Add 1 for the 0 terminator. + const int size = vsnprintf(fixedSizeCString, DEFAULT_LINE_SIZE, format, argList) + 1; + va_end(argList); + + jstring javaString; + if (size <= DEFAULT_LINE_SIZE) { + // The buffer was large enough. + javaString = env->NewStringUTF(fixedSizeCString); + } else { + // The buffer was not large enough. + va_start(argList, format); + char variableSizeCString[size]; + vsnprintf(variableSizeCString, size, format, argList); + va_end(argList); + javaString = env->NewStringUTF(variableSizeCString); + } + + env->CallStaticIntMethod(androidUtilLogClass, logDotIMethodId, javaTag, javaString); + if (javaString) env->DeleteLocalRef(javaString); + if (javaTag) env->DeleteLocalRef(javaTag); + if (androidUtilLogClass) env->DeleteLocalRef(androidUtilLogClass); + } +} diff --git a/native/jni/src/utils/log_utils.h b/native/jni/src/utils/log_utils.h index ccbecfc1d..6ac16d91a 100644 --- a/native/jni/src/utils/log_utils.h +++ b/native/jni/src/utils/log_utils.h @@ -17,9 +17,6 @@ #ifndef LATINIME_LOG_UTILS_H #define LATINIME_LOG_UTILS_H -#include -#include - #include "defines.h" #include "jni.h" @@ -29,31 +26,9 @@ class LogUtils { public: static void logToJava(JNIEnv *const env, const char *const format, ...) #ifdef __GNUC__ - __attribute__ ((format (printf, 2, 3))) + __attribute__ ((format (printf, 2, 3))) #endif // __GNUC__ - { - static const char *TAG = "LatinIME:LogUtils"; - const jclass androidUtilLogClass = env->FindClass("android/util/Log"); - const jmethodID logDotIMethodId = env->GetStaticMethodID(androidUtilLogClass, "i", - "(Ljava/lang/String;Ljava/lang/String;)I"); - const jstring javaTag = env->NewStringUTF(TAG); - - va_list argList; - va_start(argList, format); - // Get the necessary size. Add 1 for the 0 terminator. - const int size = vsnprintf(0, 0, format, argList) + 1; - va_end(argList); - char cString[size]; - va_start(argList, format); - vsnprintf(cString, size, format, argList); - va_end(argList); - - jstring javaString = env->NewStringUTF(cString); - env->CallStaticIntMethod(androidUtilLogClass, logDotIMethodId, javaTag, javaString); - env->DeleteLocalRef(javaString); - env->DeleteLocalRef(javaTag); - env->DeleteLocalRef(androidUtilLogClass); - } + ; private: DISALLOW_COPY_AND_ASSIGN(LogUtils);