From 20d6857cf5f3c67ffe24cba02f67ff1b372bab29 Mon Sep 17 00:00:00 2001 From: Youngho Yoon <34558998+yhoyoon@users.noreply.github.com> Date: Tue, 13 Aug 2024 08:49:39 +0900 Subject: [PATCH] Implement the AndroidChipLogging (#34899) * Refine android log priority based on category This change adds ANDROID_LOG_INFO priority for kLogCategory_Progress. Refine android log priorities to enhance debugging efficiency and prepare an environment where lower-priority logs can be controlled and excluded using the log filter function. Signed-off-by: Youngho Yoon <34558998+yhoyoon@users.noreply.github.com> * Implement the AndroidChipLogging Implemented the AndroidChipLogging.setLogFilter to enhance the logging mechanism. This method allows for the filtering out of unnecessary or redundant log messages, significantly reducing noise in the logs. To use the log filter, call AndroidChipLogging.setLogFilter with the appropriate filter criteria. The arg is log level in android.util.Log. Example: AndroidChipLogging.setLogFilter(android.util.Log.ERROR) Signed-off-by: Youngho Yoon <34558998+yhoyoon@users.noreply.github.com> * Restyled by google-java-format --------- Signed-off-by: Youngho Yoon <34558998+yhoyoon@users.noreply.github.com> Co-authored-by: Restyled.io --- .../android/AndroidChipPlatform-JNI.cpp | 29 +++++++++++++++++++ src/platform/android/BUILD.gn | 1 + src/platform/android/CHIPPlatformConfig.h | 2 +- src/platform/android/Logging.cpp | 16 +++++++++- .../chip/platform/AndroidChipLogging.java | 23 +++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/platform/android/java/chip/platform/AndroidChipLogging.java diff --git a/src/platform/android/AndroidChipPlatform-JNI.cpp b/src/platform/android/AndroidChipPlatform-JNI.cpp index a5863a0c739e32..45f54835d44e08 100644 --- a/src/platform/android/AndroidChipPlatform-JNI.cpp +++ b/src/platform/android/AndroidChipPlatform-JNI.cpp @@ -28,12 +28,15 @@ #include #include #include +#include #include #include #include #include #include +#include + #include "AndroidChipPlatform-JNI.h" #include "BLEManagerImpl.h" #include "BleConnectCallback-JNI.h" @@ -45,6 +48,8 @@ using namespace chip; #define JNI_METHOD(RETURN, METHOD_NAME) extern "C" JNIEXPORT RETURN JNICALL Java_chip_platform_AndroidChipPlatform_##METHOD_NAME +#define JNI_LOGGING_METHOD(RETURN, METHOD_NAME) \ + extern "C" JNIEXPORT RETURN JNICALL Java_chip_platform_AndroidChipLogging_##METHOD_NAME #define JNI_MDNSCALLBACK_METHOD(RETURN, METHOD_NAME) \ extern "C" JNIEXPORT RETURN JNICALL Java_chip_platform_ChipMdnsCallbackImpl_##METHOD_NAME @@ -245,6 +250,30 @@ JNI_METHOD(void, nativeSetDnssdDelegates)(JNIEnv * env, jclass self, jobject res chip::Dnssd::InitializeWithObjects(resolver, browser, chipMdnsCallback); } +JNI_LOGGING_METHOD(void, setLogFilter)(JNIEnv * env, jclass clazz, jint level) +{ + using namespace chip::Logging; + + uint8_t category = kLogCategory_Detail; + switch (level) + { + case ANDROID_LOG_VERBOSE: + case ANDROID_LOG_DEBUG: + category = kLogCategory_Detail; + break; + case ANDROID_LOG_INFO: + category = kLogCategory_Progress; + break; + case ANDROID_LOG_WARN: + case ANDROID_LOG_ERROR: + category = kLogCategory_Error; + break; + default: + break; + } + SetLogFilter(category); +} + JNI_MDNSCALLBACK_METHOD(void, handleServiceResolve) (JNIEnv * env, jclass self, jstring instanceName, jstring serviceType, jstring hostName, jstring address, jint port, jobject attributes, jlong callbackHandle, jlong contextHandle) diff --git a/src/platform/android/BUILD.gn b/src/platform/android/BUILD.gn index aa6f59ab5110e4..640b52490078eb 100644 --- a/src/platform/android/BUILD.gn +++ b/src/platform/android/BUILD.gn @@ -114,6 +114,7 @@ android_library("java") { sources = [ "java/chip/platform/AndroidBleManager.java", + "java/chip/platform/AndroidChipLogging.java", "java/chip/platform/AndroidChipPlatform.java", "java/chip/platform/AndroidChipPlatformException.java", "java/chip/platform/BleCallback.java", diff --git a/src/platform/android/CHIPPlatformConfig.h b/src/platform/android/CHIPPlatformConfig.h index 19ba3d113517ef..352dc41f41a0b5 100644 --- a/src/platform/android/CHIPPlatformConfig.h +++ b/src/platform/android/CHIPPlatformConfig.h @@ -48,7 +48,7 @@ using CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE = const char *; #endif // CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS #ifndef CHIP_LOG_FILTERING -#define CHIP_LOG_FILTERING 0 +#define CHIP_LOG_FILTERING 1 #endif // CHIP_LOG_FILTERING #ifndef CHIP_CONFIG_BDX_MAX_NUM_TRANSFERS diff --git a/src/platform/android/Logging.cpp b/src/platform/android/Logging.cpp index 404c76f803ba12..ccdaa8dffca4ac 100644 --- a/src/platform/android/Logging.cpp +++ b/src/platform/android/Logging.cpp @@ -12,7 +12,21 @@ namespace Platform { void LogV(const char * module, uint8_t category, const char * msg, va_list v) { - int priority = (category == kLogCategory_Error) ? ANDROID_LOG_ERROR : ANDROID_LOG_DEBUG; + int priority = ANDROID_LOG_DEBUG; + switch (category) + { + case kLogCategory_Error: + priority = ANDROID_LOG_ERROR; + break; + case kLogCategory_Progress: + priority = ANDROID_LOG_INFO; + break; + case kLogCategory_Detail: + priority = ANDROID_LOG_DEBUG; + break; + default: + break; + } __android_log_vprint(priority, module, msg, v); } diff --git a/src/platform/android/java/chip/platform/AndroidChipLogging.java b/src/platform/android/java/chip/platform/AndroidChipLogging.java new file mode 100644 index 00000000000000..4f41b21b78b732 --- /dev/null +++ b/src/platform/android/java/chip/platform/AndroidChipLogging.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * 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. + * + */ +package chip.platform; + +public class AndroidChipLogging { + // logging level is in android.util.Log class + public static native void setLogFilter(int level); +}