Skip to content

Commit

Permalink
Implement the AndroidChipLogging (#34899)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>

* Restyled by google-java-format

---------

Signed-off-by: Youngho Yoon <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
yhoyoon and restyled-commits authored Aug 12, 2024
1 parent 10531e1 commit 20d6857
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/platform/android/AndroidChipPlatform-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceConfig.h>
#include <platform/ConfigurationManager.h>
#include <platform/ConnectivityManager.h>
#include <platform/KeyValueStoreManager.h>
#include <platform/internal/BLEManager.h>

#include <android/log.h>

#include "AndroidChipPlatform-JNI.h"
#include "BLEManagerImpl.h"
#include "BleConnectCallback-JNI.h"
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/platform/android/CHIPPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/platform/android/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
23 changes: 23 additions & 0 deletions src/platform/android/java/chip/platform/AndroidChipLogging.java
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 20d6857

Please sign in to comment.