From cb6d4c57ca60cc221b6648e724d38a30e99f4768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20=C4=8Eurech?= Date: Thu, 6 Jun 2024 13:43:38 +0200 Subject: [PATCH] Cherry pick: Resolved #583: Added log listener (#584) --- .../powerauth/system/PowerAuthLog.java | 28 ++++++++++ .../system/PowerAuthLogListener.java | 51 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLogListener.java diff --git a/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLog.java b/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLog.java index 1bac9a1e..7aeb8773 100644 --- a/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLog.java +++ b/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLog.java @@ -16,6 +16,8 @@ package io.getlime.security.powerauth.system; +import androidx.annotation.Nullable; + /** * Class that provides logging facility for PowerAuth SDK library. * @@ -37,6 +39,11 @@ public class PowerAuthLog { */ private static final String LOG_TAG = "PowerAuthLibrary"; + /** + * Listener that can tap into the log stream and process it on it's own. + */ + public @Nullable static PowerAuthLogListener logListener; + /** * Controls logging from PowerAuth classes * @param enabled enables or disables debug logs @@ -74,6 +81,9 @@ public static void d(String format, Object... args) { if (logIsEnabled) { String message = String.format(format, args); android.util.Log.d(LOG_TAG, message); + if (logListener != null) { + logListener.powerAuthDebugLog(message); + } } } @@ -87,5 +97,23 @@ public static void d(String format, Object... args) { public static void e(String format, Object... args) { String message = String.format(format, args); android.util.Log.e(LOG_TAG, message); + if (logListener != null) { + logListener.powerAuthErrorLog(message); + } + } + + /** + * Adds a formatted WARNING log message. Unlike DEBUG message calling this method always + * produce error record. + * @param format format string, just like for {@link java.lang.String#format String.format} + * @param args Arguments referenced by the format specifiers in the format + * string. + */ + public static void w(String format, Object... args) { + String message = String.format(format, args); + android.util.Log.w(LOG_TAG, message); + if (logListener != null) { + logListener.powerAuthWarningLog(message); + } } } diff --git a/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLogListener.java b/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLogListener.java new file mode 100644 index 00000000..6b943097 --- /dev/null +++ b/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLogListener.java @@ -0,0 +1,51 @@ +/* + * Copyright 2024 Wultra s.r.o. + * + * 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 io.getlime.security.powerauth.system; + +import androidx.annotation.NonNull; + +/** + * Interface that represents a listener that can tap into the library logs and use them for example + * to report to a online system or to a logfile for user to send with some report. Note that by default + * all logs are logged into the Logcat. + * + * @author Jan Kobersky + */ +public interface PowerAuthLogListener { + + /** + * Debug message reported by the library. Debug messages are only reported when + * {@link PowerAuthLog} setEnabled is set to true. + * + * @param message Debug message. + */ + void powerAuthDebugLog(@NonNull String message); + + /** + * Warning reported by the library. + * + * @param message Warning message. + */ + void powerAuthWarningLog(@NonNull String message); + + /** + * Error reported by the library. + * + * @param message Error message. + */ + void powerAuthErrorLog(@NonNull String message); +}