Skip to content

Commit 20d6857

Browse files
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 <commits@restyled.io>
1 parent 10531e1 commit 20d6857

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

src/platform/android/AndroidChipPlatform-JNI.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@
2828
#include <lib/support/CodeUtils.h>
2929
#include <lib/support/JniReferences.h>
3030
#include <lib/support/JniTypeWrappers.h>
31+
#include <lib/support/logging/CHIPLogging.h>
3132
#include <platform/CHIPDeviceConfig.h>
3233
#include <platform/ConfigurationManager.h>
3334
#include <platform/ConnectivityManager.h>
3435
#include <platform/KeyValueStoreManager.h>
3536
#include <platform/internal/BLEManager.h>
3637

38+
#include <android/log.h>
39+
3740
#include "AndroidChipPlatform-JNI.h"
3841
#include "BLEManagerImpl.h"
3942
#include "BleConnectCallback-JNI.h"
@@ -45,6 +48,8 @@
4548
using namespace chip;
4649

4750
#define JNI_METHOD(RETURN, METHOD_NAME) extern "C" JNIEXPORT RETURN JNICALL Java_chip_platform_AndroidChipPlatform_##METHOD_NAME
51+
#define JNI_LOGGING_METHOD(RETURN, METHOD_NAME) \
52+
extern "C" JNIEXPORT RETURN JNICALL Java_chip_platform_AndroidChipLogging_##METHOD_NAME
4853
#define JNI_MDNSCALLBACK_METHOD(RETURN, METHOD_NAME) \
4954
extern "C" JNIEXPORT RETURN JNICALL Java_chip_platform_ChipMdnsCallbackImpl_##METHOD_NAME
5055

@@ -245,6 +250,30 @@ JNI_METHOD(void, nativeSetDnssdDelegates)(JNIEnv * env, jclass self, jobject res
245250
chip::Dnssd::InitializeWithObjects(resolver, browser, chipMdnsCallback);
246251
}
247252

253+
JNI_LOGGING_METHOD(void, setLogFilter)(JNIEnv * env, jclass clazz, jint level)
254+
{
255+
using namespace chip::Logging;
256+
257+
uint8_t category = kLogCategory_Detail;
258+
switch (level)
259+
{
260+
case ANDROID_LOG_VERBOSE:
261+
case ANDROID_LOG_DEBUG:
262+
category = kLogCategory_Detail;
263+
break;
264+
case ANDROID_LOG_INFO:
265+
category = kLogCategory_Progress;
266+
break;
267+
case ANDROID_LOG_WARN:
268+
case ANDROID_LOG_ERROR:
269+
category = kLogCategory_Error;
270+
break;
271+
default:
272+
break;
273+
}
274+
SetLogFilter(category);
275+
}
276+
248277
JNI_MDNSCALLBACK_METHOD(void, handleServiceResolve)
249278
(JNIEnv * env, jclass self, jstring instanceName, jstring serviceType, jstring hostName, jstring address, jint port,
250279
jobject attributes, jlong callbackHandle, jlong contextHandle)

src/platform/android/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ android_library("java") {
114114

115115
sources = [
116116
"java/chip/platform/AndroidBleManager.java",
117+
"java/chip/platform/AndroidChipLogging.java",
117118
"java/chip/platform/AndroidChipPlatform.java",
118119
"java/chip/platform/AndroidChipPlatformException.java",
119120
"java/chip/platform/BleCallback.java",

src/platform/android/CHIPPlatformConfig.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ using CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE = const char *;
4848
#endif // CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS
4949

5050
#ifndef CHIP_LOG_FILTERING
51-
#define CHIP_LOG_FILTERING 0
51+
#define CHIP_LOG_FILTERING 1
5252
#endif // CHIP_LOG_FILTERING
5353

5454
#ifndef CHIP_CONFIG_BDX_MAX_NUM_TRANSFERS

src/platform/android/Logging.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,21 @@ namespace Platform {
1212

1313
void LogV(const char * module, uint8_t category, const char * msg, va_list v)
1414
{
15-
int priority = (category == kLogCategory_Error) ? ANDROID_LOG_ERROR : ANDROID_LOG_DEBUG;
15+
int priority = ANDROID_LOG_DEBUG;
16+
switch (category)
17+
{
18+
case kLogCategory_Error:
19+
priority = ANDROID_LOG_ERROR;
20+
break;
21+
case kLogCategory_Progress:
22+
priority = ANDROID_LOG_INFO;
23+
break;
24+
case kLogCategory_Detail:
25+
priority = ANDROID_LOG_DEBUG;
26+
break;
27+
default:
28+
break;
29+
}
1630
__android_log_vprint(priority, module, msg, v);
1731
}
1832

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package chip.platform;
19+
20+
public class AndroidChipLogging {
21+
// logging level is in android.util.Log class
22+
public static native void setLogFilter(int level);
23+
}

0 commit comments

Comments
 (0)