| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-present eyeo GmbH | |
| 4 * | |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License version 3 as | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License | |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
| 16 */ | |
| 17 | |
| 18 #include "JniCallbacks.h" | |
| 19 #include "JniLogSystem.h" | |
| 20 | |
| 21 // precached in JNI_OnLoad and released in JNI_OnUnload | |
| 22 JniGlobalReference<jclass>* logLevelClass; | |
| 23 | |
| 24 void JniLogSystem_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved) | |
| 25 { | |
| 26 logLevelClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("LogSys
tem$LogLevel"))); | |
| 27 } | |
| 28 | |
| 29 void JniLogSystem_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved) | |
| 30 { | |
| 31 if (logLevelClass) | |
| 32 { | |
| 33 delete logLevelClass; | |
| 34 logLevelClass = NULL; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 JniLogSystemCallback::JniLogSystemCallback(JNIEnv* env, jobject callbackObject) | |
| 39 : JniCallbackBase(env, callbackObject), AdblockPlus::LogSystem() | |
| 40 { | |
| 41 } | |
| 42 | |
| 43 void JniLogSystemCallback::operator()(AdblockPlus::LogSystem::LogLevel logLevel, | |
| 44 const std::string& message, const std::string& source) | |
| 45 { | |
| 46 JNIEnvAcquire env(GetJavaVM()); | |
| 47 | |
| 48 jmethodID method = env->GetMethodID( | |
| 49 *JniLocalReference<jclass>(*env, | |
| 50 env->GetObjectClass(GetCallbackObject())), | |
| 51 "logCallback", | |
| 52 "(" TYP("LogSystem$LogLevel") "Ljava/lang/String;Ljava/lang/String;)V"); | |
| 53 | |
| 54 // TODO: Set log level from Java and handle it here (to reduce C++->Java calls
) | |
| 55 | |
| 56 if (method) | |
| 57 { | |
| 58 const char* enumName = 0; | |
| 59 | |
| 60 switch (logLevel) | |
| 61 { | |
| 62 default: | |
| 63 case AdblockPlus::LogSystem::LOG_LEVEL_TRACE: | |
| 64 enumName = "TRACE"; | |
| 65 break; | |
| 66 case AdblockPlus::LogSystem::LOG_LEVEL_LOG: | |
| 67 enumName = "LOG"; | |
| 68 break; | |
| 69 case AdblockPlus::LogSystem::LOG_LEVEL_INFO: | |
| 70 enumName = "INFO"; | |
| 71 break; | |
| 72 case AdblockPlus::LogSystem::LOG_LEVEL_WARN: | |
| 73 enumName = "WARN"; | |
| 74 break; | |
| 75 case AdblockPlus::LogSystem::LOG_LEVEL_ERROR: | |
| 76 enumName = "ERROR"; | |
| 77 break; | |
| 78 } | |
| 79 | |
| 80 jclass enumClass = logLevelClass->Get(); | |
| 81 if (enumClass) | |
| 82 { | |
| 83 jfieldID enumField = env->GetStaticFieldID(enumClass, enumName, | |
| 84 TYP("LogSystem$LogLevel")); | |
| 85 JniLocalReference<jobject> jLogLevel(*env, | |
| 86 env->GetStaticObjectField(enumClass, enumField)); | |
| 87 | |
| 88 JniLocalReference<jstring> jMessage(*env, | |
| 89 env->NewStringUTF(message.c_str())); | |
| 90 JniLocalReference<jstring> jSource(*env, | |
| 91 env->NewStringUTF(source.c_str())); | |
| 92 | |
| 93 env->CallVoidMethod(GetCallbackObject(), method, *jLogLevel, *jMessage, | |
| 94 *jSource); | |
| 95 } | |
| 96 | |
| 97 CheckAndLogJavaException(*env); | |
| 98 } | |
| 99 } | |
| OLD | NEW |