Skip to content

Commit

Permalink
Cherry pick: Resolved #583: Added log listener (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
hvge committed Jun 6, 2024
1 parent 2a8541a commit cb6d4c5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.getlime.security.powerauth.system;

import androidx.annotation.Nullable;

/**
* Class that provides logging facility for PowerAuth SDK library.
*
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit cb6d4c5

Please sign in to comment.