Skip to content

Commit

Permalink
Slim down the static logger to just be an instance of InternalEmbrace…
Browse files Browse the repository at this point in the history
…Logger
  • Loading branch information
bidetofevil committed Apr 3, 2024
1 parent 740b7f5 commit 8c35994
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.embrace.android.embracesdk.fcm.swazzle.callback.com.android.fcm;

import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logDebug;
import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logError;
import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger;

import androidx.annotation.NonNull;

Expand All @@ -19,10 +18,10 @@ private FirebaseSwazzledHooks() {
@SuppressWarnings("MethodNameCheck")
@InternalApi
public static void _onMessageReceived(@NonNull RemoteMessage message) {
logDebug("Embrace received push notification message");
logger.logDebug("Embrace received push notification message");

if (!Embrace.getInstance().isStarted()) {
logError("Embrace received push notification data before the SDK was started");
logger.logError("Embrace received push notification data before the SDK was started");
return;
}

Expand All @@ -38,29 +37,29 @@ private static void handleRemoteMessage(@NonNull RemoteMessage message) {
try {
messageId = message.getMessageId();
} catch (Exception e) {
logError("Failed to capture FCM messageId", e);
logger.logError("Failed to capture FCM messageId", e);
}

String topic = null;
try {
topic = message.getFrom();
} catch (Exception e) {
logError("Failed to capture FCM topic", e);
logger.logError("Failed to capture FCM topic", e);
}

Integer messagePriority = null;
try {
messagePriority = message.getPriority();
} catch (Exception e) {
logError("Failed to capture FCM message priority", e);
logger.logError("Failed to capture FCM message priority", e);
}

RemoteMessage.Notification notification = null;

try {
notification = message.getNotification();
} catch (Exception e) {
logError("Failed to capture FCM RemoteMessage Notification", e);
logger.logError("Failed to capture FCM RemoteMessage Notification", e);
}

String title = null;
Expand All @@ -70,19 +69,19 @@ private static void handleRemoteMessage(@NonNull RemoteMessage message) {
try {
title = notification.getTitle();
} catch (Exception e) {
logError("Failed to capture FCM title", e);
logger.logError("Failed to capture FCM title", e);
}

try {
body = notification.getBody();
} catch (Exception e) {
logError("Failed to capture FCM body", e);
logger.logError("Failed to capture FCM body", e);
}

try {
notificationPriority = notification.getNotificationPriority();
} catch (Exception e) {
logError("Failed to capture FCM notificationPriority", e);
logger.logError("Failed to capture FCM notificationPriority", e);
}
}

Expand All @@ -91,20 +90,20 @@ private static void handleRemoteMessage(@NonNull RemoteMessage message) {

try {
Embrace.getInstance().logPushNotification(
title,
body,
topic,
messageId,
notificationPriority,
messagePriority,
hasNotification,
hasData
title,
body,
topic,
messageId,
notificationPriority,
messagePriority,
hasNotification,
hasData
);
} catch (Exception e) {
logError("Failed to log push Notification", e);
logger.logError("Failed to log push Notification", e);
}
} catch (Exception e) {
logError("Push Notification Error", e);
logger.logError("Push Notification Error", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.embrace.android.embracesdk.okhttp3.swazzle.callback.okhttp3;

import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger;

import java.util.List;

import io.embrace.android.embracesdk.annotation.InternalApi;
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger;
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3ApplicationInterceptor;
import io.embrace.android.embracesdk.okhttp3.EmbraceOkHttp3NetworkInterceptor;
import okhttp3.Interceptor;
Expand Down Expand Up @@ -34,13 +35,13 @@ private Builder() {
*/
@SuppressWarnings("MethodNameCheck")
public static void _preBuild(okhttp3.OkHttpClient.Builder thiz) {
InternalStaticEmbraceLogger.logDebug("Embrace OkHTTP Wrapper; onPrebuild");
logger.logDebug("Embrace OkHTTP Wrapper; onPrebuild");
addEmbraceInterceptors(thiz);
}

@SuppressWarnings("MethodNameCheck")
public static void _constructorOnPostBody(okhttp3.OkHttpClient.Builder thiz) {
InternalStaticEmbraceLogger.logDebug("Embrace OkHTTP Wrapper; onPostBody");
logger.logDebug("Embrace OkHTTP Wrapper; onPostBody");
addEmbraceInterceptors(thiz);
}

Expand All @@ -51,17 +52,16 @@ public static void _constructorOnPostBody(okhttp3.OkHttpClient.Builder thiz) {
*/
private static void addEmbraceInterceptors(okhttp3.OkHttpClient.Builder thiz) {
try {
InternalStaticEmbraceLogger.logDebug("Embrace OkHTTP Wrapper;"
+ " Adding interceptors");
logger.logDebug("Embrace OkHTTP Wrapper; Adding interceptors");
addInterceptor(thiz.interceptors(), new EmbraceOkHttp3ApplicationInterceptor());
addInterceptor(thiz.networkInterceptors(), new EmbraceOkHttp3NetworkInterceptor());
} catch (NoSuchMethodError exception) {
// The customer may be overwriting OkHttpClient with their own implementation, and some of the
// methods we use are missing.
InternalStaticEmbraceLogger.logError("Altered OkHttpClient implementation, could not add OkHttp interceptor. ",
logger.logError("Altered OkHttpClient implementation, could not add OkHttp interceptor. ",
exception);
} catch (Exception exception) {
InternalStaticEmbraceLogger.logError("Could not add OkHttp interceptor. ", exception);
logger.logError("Could not add OkHttp interceptor. ", exception);
}
}

Expand All @@ -76,7 +76,7 @@ private static void addInterceptor(List<Interceptor> interceptors,
if (interceptors != null && !containsInstance(interceptors, interceptor.getClass())) {
interceptors.add(0, interceptor);
} else {
InternalStaticEmbraceLogger.logDebug(
logger.logDebug(
"Not adding interceptor [" + interceptor.getClass().getSimpleName() + "]"
);
}
Expand All @@ -94,7 +94,7 @@ private static <T> boolean containsInstance(List<T> elementsList,
Class<? extends T> clazz) {
for (T classInstance : elementsList) {
if (clazz.isInstance(classInstance)) {
InternalStaticEmbraceLogger.logDebug(
logger.logDebug(
"[" + clazz.getSimpleName() + "] already present in list"
);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.embrace.android.embracesdk.IntegrationTestRule
import io.embrace.android.embracesdk.recordSession
import io.mockk.every
import io.mockk.mockk
import java.io.InputStream
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.os.Handler
import android.os.Looper
import android.widget.Toast
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger
import io.embrace.android.embracesdk.samples.AutomaticVerificationChecker
import io.embrace.android.embracesdk.samples.VerificationActions
import io.embrace.android.embracesdk.samples.VerifyIntegrationException
Expand Down Expand Up @@ -61,7 +61,7 @@ internal class EmbraceAutomaticVerification(
private const val EMBRACE_CONTACT_EMAIL = "[email protected]"
private const val VERIFY_INTEGRATION_DELAY = 200L
private const val ON_FOREGROUND_TIMEOUT = 5000L
internal val instance = EmbraceAutomaticVerification(logger = InternalStaticEmbraceLogger.logger)
internal val instance = EmbraceAutomaticVerification(logger = logger)
}

fun verifyIntegration() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.embrace.android.embracesdk

import io.embrace.android.embracesdk.annotation.InternalApi
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger
import io.embrace.android.embracesdk.samples.EmbraceCrashSamples

/**
Expand All @@ -15,7 +15,7 @@ import io.embrace.android.embracesdk.samples.EmbraceCrashSamples
public object EmbraceSamples {

private val embraceCrashSamples = EmbraceCrashSamples
private val embraceAutomaticVerification = EmbraceAutomaticVerification(logger = InternalStaticEmbraceLogger.logger)
private val embraceAutomaticVerification = EmbraceAutomaticVerification(logger = logger)

/**
* Starts an automatic verification of the following Embrace features:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.embrace.android.embracesdk;

import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger;

import android.util.Pair;

import io.embrace.android.embracesdk.annotation.InternalApi;
import io.embrace.android.embracesdk.payload.TapBreadcrumb.TapBreadcrumbType;
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger;

/**
* @hide
Expand Down Expand Up @@ -35,10 +36,10 @@ static void logOnClickEvent(android.view.View view, TapBreadcrumbType breadcrumb
} catch (NoSuchMethodError exception) {
// The customer may be overwriting View with their own implementation, and some of the
// methods we use are missing.
InternalStaticEmbraceLogger.logError("Could not log onClickEvent. Some methods are missing. ",
logger.logError("Could not log onClickEvent. Some methods are missing. ",
exception);
} catch (Exception exception) {
InternalStaticEmbraceLogger.logError("Could not log onClickEvent.", exception);
logger.logError("Could not log onClickEvent.", exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.embrace.android.embracesdk.internal.network.http;

import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand All @@ -8,7 +10,6 @@
import java.util.regex.Pattern;

import io.embrace.android.embracesdk.annotation.InternalApi;
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger;

@InternalApi
public class EmbraceHttpPathOverride {
Expand Down Expand Up @@ -54,31 +55,31 @@ public static String getURLString(@NonNull HttpPathOverrideRequest request, @Nul

private static Boolean validatePathOverride(String path) {
if (path == null) {
InternalStaticEmbraceLogger.logError("URL relative path cannot be null");
logger.logError("URL relative path cannot be null");
return false;
}
if (path.length() == 0) {
InternalStaticEmbraceLogger.logError("Relative path must have non-zero length");
if (path.isEmpty()) {
logger.logError("Relative path must have non-zero length");
return false;
}
if (path.length() > RELATIVE_PATH_MAX_LENGTH) {
InternalStaticEmbraceLogger.logError(String.format(Locale.US,
"Relative path %s is greater than the maximum allowed length of %d. It will be ignored",
path, RELATIVE_PATH_MAX_LENGTH));
logger.logError(String.format(Locale.US,
"Relative path %s is greater than the maximum allowed length of %d. It will be ignored",
path, RELATIVE_PATH_MAX_LENGTH));
return false;
}
if (!StandardCharsets.US_ASCII.newEncoder().canEncode(path)) {
InternalStaticEmbraceLogger.logError("Relative path must not contain unicode " +
"characters. Relative path " + path + " will be ignored.");
logger.logError("Relative path must not contain unicode " +
"characters. Relative path " + path + " will be ignored.");
return false;
}
if (!path.startsWith("/")) {
InternalStaticEmbraceLogger.logError("Relative path must start with a /");
logger.logError("Relative path must start with a /");
return false;
}
if (!RELATIVE_PATH_PATTERN.matcher(path).matches()) {
InternalStaticEmbraceLogger.logError("Relative path contains invalid chars. " +
"Relative path " + path + " will be ignored.");
logger.logError("Relative path contains invalid chars. " +
"Relative path " + path + " will be ignored.");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.embrace.android.embracesdk.internal.network.http;

import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger;

class EmbraceHttpUrlConnectionOverride implements HttpPathOverrideRequest {

private final HttpURLConnection connection;
Expand All @@ -30,8 +30,7 @@ public String getOverriddenURL(@NonNull String pathOverride) {
return new URL(connection.getURL().getProtocol(), connection.getURL().getHost(),
connection.getURL().getPort(), pathOverride + "?" + connection.getURL().getQuery()).toString();
} catch (MalformedURLException e) {
InternalStaticEmbraceLogger.logError("Failed to override path of " +
connection.getURL() + " with " + pathOverride);
logger.logError("Failed to override path of " + connection.getURL() + " with " + pathOverride);
return connection.getURL().toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.embrace.android.embracesdk.internal.network.http;

import static io.embrace.android.embracesdk.config.behavior.NetworkSpanForwardingBehavior.TRACEPARENT_HEADER_NAME;
import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger;

import android.annotation.TargetApi;
import android.os.Build;
Expand Down Expand Up @@ -34,7 +35,6 @@

import io.embrace.android.embracesdk.Embrace;
import io.embrace.android.embracesdk.annotation.InternalApi;
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger;
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest;
import io.embrace.android.embracesdk.network.http.HttpMethod;
import io.embrace.android.embracesdk.utils.exceptions.function.CheckedSupplier;
Expand Down Expand Up @@ -621,7 +621,7 @@ synchronized void internalLogNetworkCall(long startTime, long endTime, boolean o
);
}
} catch (Exception e) {
InternalStaticEmbraceLogger.logError("Error logging native network request", e);
logger.logError("Error logging native network request", e);
}
}
}
Expand Down Expand Up @@ -698,7 +698,7 @@ private void identifyTraceId() {
try {
traceId = getRequestProperty(embrace.getTraceIdHeader());
} catch (Exception e) {
InternalStaticEmbraceLogger.logDebug("Failed to retrieve actual trace id header. Current: " + traceId);
logger.logDebug("Failed to retrieve actual trace id header. Current: " + traceId);
}
}
}
Expand Down

0 comments on commit 8c35994

Please sign in to comment.