Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

implemented diagnostic logger #218

Merged
merged 1 commit into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class ANRWatchDog extends Thread {
private final ANRListener anrListener;
private final IHandler uiHandler;
private final long timeoutIntervalMills;
private final ILogger logger;
private final @NotNull ILogger logger;
private AtomicLong tick = new AtomicLong(0);
private volatile boolean reported = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package io.sentry.android.core;

import static io.sentry.core.ILogger.logIfNotNull;

import android.content.Context;
import io.sentry.core.ILogger;
import io.sentry.core.SentryLevel;
import io.sentry.core.SentryOptions;
import java.io.File;
import org.jetbrains.annotations.NotNull;

final class AndroidOptionsInitializer {
private AndroidOptionsInitializer() {}
Expand All @@ -15,7 +14,7 @@ static void init(SentryAndroidOptions options, Context context) {
init(options, context, new AndroidLogger());
}

static void init(SentryAndroidOptions options, Context context, ILogger logger) {
static void init(SentryAndroidOptions options, Context context, final @NotNull ILogger logger) {
// Firstly set the logger, if `debug=true` configured, logging can start asap.
options.setLogger(logger);

Expand All @@ -36,7 +35,7 @@ static void init(SentryAndroidOptions options, Context context, ILogger logger)
options.setTransportGate(new AndroidTransportGate(context, options.getLogger()));
}

private static void setDefaultInApp(Context context, SentryOptions options) {
private static void setDefaultInApp(Context context, final @NotNull SentryOptions options) {
String packageName = context.getPackageName();
if (packageName != null && !packageName.startsWith("android.")) {
options.addInAppInclude(packageName);
Expand All @@ -55,8 +54,7 @@ private static void initializeCacheDirs(Context context, SentryOptions options)
if (options.getOutboxPath() != null) {
new File(options.getOutboxPath()).mkdirs();
} else {
logIfNotNull(
options.getLogger(), SentryLevel.WARNING, "No outbox dir path is defined in options.");
options.getLogger().log(SentryLevel.WARNING, "No outbox dir path is defined in options.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
import java.io.Writer;
import java.util.Date;
import java.util.TimeZone;
import org.jetbrains.annotations.NotNull;

final class AndroidSerializer implements ISerializer {

private final ILogger logger;
private final @NotNull ILogger logger;
private final Gson gson;

public AndroidSerializer(ILogger logger) {
public AndroidSerializer(final @NotNull ILogger logger) {
this.logger = logger;

gson = provideGson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
final class AndroidTransportGate implements ITransportGate {

private final Context context;
private final ILogger logger;
private final @NotNull ILogger logger;

AndroidTransportGate(@NotNull Context context, @NotNull ILogger logger) {
this.context = context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.sentry.android.core;

import static io.sentry.core.ILogger.logIfNotNull;

import io.sentry.core.IHub;
import io.sentry.core.ILogger;
import io.sentry.core.Integration;
Expand All @@ -11,6 +9,7 @@
import io.sentry.core.protocol.Mechanism;
import java.io.Closeable;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;

final class AnrIntegration implements Integration, Closeable {
Expand All @@ -23,14 +22,15 @@ public void register(IHub hub, SentryOptions options) {
}

private void register(IHub hub, SentryAndroidOptions options) {
logIfNotNull(options.getLogger(), SentryLevel.DEBUG, "ANR enabled: %s", options.isAnrEnabled());
options.getLogger().log(SentryLevel.DEBUG, "ANR enabled: %s", options.isAnrEnabled());

if (options.isAnrEnabled() && anrWatchDog == null) {
logIfNotNull(
options.getLogger(),
SentryLevel.DEBUG,
"ANR timeout in milliseconds: %d",
options.getAnrTimeoutIntervalMills());
options
.getLogger()
.log(
SentryLevel.DEBUG,
"ANR timeout in milliseconds: %d",
options.getAnrTimeoutIntervalMills());

anrWatchDog =
new ANRWatchDog(
Expand All @@ -43,8 +43,8 @@ private void register(IHub hub, SentryAndroidOptions options) {
}

@TestOnly
void reportANR(IHub hub, ILogger logger, ApplicationNotResponding error) {
logIfNotNull(logger, SentryLevel.INFO, "ANR triggered with message: %s", error.getMessage());
void reportANR(IHub hub, final @NotNull ILogger logger, ApplicationNotResponding error) {
logger.log(SentryLevel.INFO, "ANR triggered with message: %s", error.getMessage());

Mechanism mechanism = new Mechanism();
mechanism.setType("ANR");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.sentry.android.core;

import static android.content.Context.ACTIVITY_SERVICE;
import static io.sentry.core.ILogger.logIfNotNull;

import android.app.ActivityManager;
import android.content.Context;
Expand Down Expand Up @@ -81,6 +80,7 @@ public DefaultAndroidEventProcessor(Context context, SentryOptions options) {
this.options = Objects.requireNonNull(options, "The SentryOptions is required.");

ExecutorService executorService = Executors.newSingleThreadExecutor();
// dont ref. to method reference, theres a bug on it
contextData = executorService.submit(() -> loadContextData());

executorService.shutdown();
Expand Down Expand Up @@ -116,11 +116,12 @@ public SentryEvent process(SentryEvent event, @Nullable Object hint) {
if (!(hint instanceof Cached)) {
processNonCachedEvent(event);
} else {
logIfNotNull(
options.getLogger(),
SentryLevel.DEBUG,
"Event was cached so not applying data relevant to the current app execution/version: %s",
event.getEventId());
options
.getLogger()
.log(
SentryLevel.DEBUG,
"Event was cached so not applying data relevant to the current app execution/version: %s",
event.getEventId());
}

if (event.getContexts().getDevice() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
import io.sentry.core.SentryLevel;
import io.sentry.core.util.Objects;
import java.io.File;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

final class EnvelopeFileObserver extends FileObserver {

private final String rootPath;
private final IEnvelopeSender envelopeSender;
private final ILogger logger;
private @NotNull final ILogger logger;

// The preferred overload (Taking File instead of String) is only available from API 29
@SuppressWarnings("deprecation")
EnvelopeFileObserver(String path, IEnvelopeSender envelopeSender, ILogger logger) {
EnvelopeFileObserver(String path, IEnvelopeSender envelopeSender, @NotNull ILogger logger) {
super(path);
this.rootPath = Objects.requireNonNull(path, "File path is required.");
this.envelopeSender = Objects.requireNonNull(envelopeSender, "Envelope sender is required.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package io.sentry.android.core;

import static io.sentry.core.ILogger.logIfNotNull;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import io.sentry.core.ILogger;
import io.sentry.core.SentryLevel;
import java.util.Locale;
import org.jetbrains.annotations.NotNull;

final class ManifestMetadataReader {

static final String DSN_KEY = "io.sentry.dsn";
static final String DEBUG_KEY = "io.sentry.debug";
static final String DEBUG_LEVEL = "io.sentry.debug.level";
static final String ANR_ENABLE = "io.sentry.anr.enable";
static final String ANR_REPORT_DEBUG = "io.sentry.anr.report-debug";
static final String ANR_TIMEOUT_INTERVAL_MILLS = "io.sentry.anr.timeout-interval-mills";
Expand All @@ -29,76 +30,74 @@ static void applyMetadata(Context context, SentryAndroidOptions options) {

if (metadata != null) {
boolean debug = metadata.getBoolean(DEBUG_KEY, options.isDebug());
logIfNotNull(options.getLogger(), SentryLevel.DEBUG, "debug read: %s", debug);
options.setDebug(debug);
options.getLogger().log(SentryLevel.DEBUG, "debug read: %s", debug);

if (options.isDebug()) {
String level =
metadata.getString(
DEBUG_LEVEL, options.getDiagnosticLevel().name().toLowerCase(Locale.ROOT));
options.setDiagnosticLevel(SentryLevel.valueOf(level.toUpperCase(Locale.ROOT)));
}

boolean isAnrEnabled = metadata.getBoolean(ANR_ENABLE, options.isAnrEnabled());
logIfNotNull(options.getLogger(), SentryLevel.DEBUG, "isAnrEnabled read: %s", isAnrEnabled);
options.getLogger().log(SentryLevel.DEBUG, "isAnrEnabled read: %s", isAnrEnabled);
options.setAnrEnabled(isAnrEnabled);

boolean isAnrReportInDebug =
metadata.getBoolean(ANR_REPORT_DEBUG, options.isAnrReportInDebug());
logIfNotNull(
options.getLogger(),
SentryLevel.DEBUG,
"isAnrReportInDebug read: %s",
isAnrReportInDebug);
options
.getLogger()
.log(SentryLevel.DEBUG, "isAnrReportInDebug read: %s", isAnrReportInDebug);
options.setAnrReportInDebug(isAnrReportInDebug);

int anrTimeoutIntervalMills =
metadata.getInt(ANR_TIMEOUT_INTERVAL_MILLS, options.getAnrTimeoutIntervalMills());
logIfNotNull(
options.getLogger(),
SentryLevel.DEBUG,
"anrTimeoutIntervalMills read: %d",
anrTimeoutIntervalMills);
options
.getLogger()
.log(SentryLevel.DEBUG, "anrTimeoutIntervalMills read: %d", anrTimeoutIntervalMills);
options.setAnrTimeoutIntervalMills(anrTimeoutIntervalMills);

String dsn = metadata.getString(DSN_KEY, null);
if (dsn == null) {
logIfNotNull(
options.getLogger(),
SentryLevel.FATAL,
"DSN is required. Use empty string to disable SDK.");
options
.getLogger()
.log(SentryLevel.FATAL, "DSN is required. Use empty string to disable SDK.");
} else if (dsn.isEmpty()) {
logIfNotNull(
options.getLogger(), SentryLevel.DEBUG, "DSN is empty, disabling sentry-android");
options.getLogger().log(SentryLevel.DEBUG, "DSN is empty, disabling sentry-android");
} else {
logIfNotNull(options.getLogger(), SentryLevel.DEBUG, "DSN read: %s", dsn);
options.getLogger().log(SentryLevel.DEBUG, "DSN read: %s", dsn);
}
options.setDsn(dsn);

boolean ndk = metadata.getBoolean(ENABLE_NDK, options.isEnableNdk());
logIfNotNull(options.getLogger(), SentryLevel.DEBUG, "NDK read: %s", ndk);
options.getLogger().log(SentryLevel.DEBUG, "NDK read: %s", ndk);
options.setEnableNdk(ndk);
}
logIfNotNull(
options.getLogger(),
SentryLevel.INFO,
"Retrieving configuration from AndroidManifest.xml");
options
.getLogger()
.log(SentryLevel.INFO, "Retrieving configuration from AndroidManifest.xml");
} catch (Exception e) {
logIfNotNull(
options.getLogger(),
SentryLevel.ERROR,
"Failed to read configuration from android manifest metadata.",
e);
options
.getLogger()
.log(
SentryLevel.ERROR, "Failed to read configuration from android manifest metadata.", e);
}
}

static boolean isAutoInit(Context context, ILogger logger) {
static boolean isAutoInit(Context context, final @NotNull ILogger logger) {
if (context == null) throw new IllegalArgumentException("The application context is required.");

boolean autoInit = true;
try {
Bundle metadata = getMetadata(context);
if (metadata != null) {
autoInit = metadata.getBoolean(AUTO_INIT, true);
logIfNotNull(logger, SentryLevel.DEBUG, "Auto-init: %s", autoInit);
logger.log(SentryLevel.DEBUG, "Auto-init: %s", autoInit);
}
logIfNotNull(logger, SentryLevel.INFO, "Retrieving auto-init from AndroidManifest.xml");
logger.log(SentryLevel.INFO, "Retrieving auto-init from AndroidManifest.xml");
} catch (Exception e) {
logIfNotNull(
logger, SentryLevel.ERROR, "Failed to read auto-init from android manifest metadata.", e);
logger.log(SentryLevel.ERROR, "Failed to read auto-init from android manifest metadata.", e);
}
return autoInit;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.sentry.android.core.adapters;

import static io.sentry.core.ILogger.logIfNotNull;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
Expand All @@ -18,13 +16,14 @@
import io.sentry.core.protocol.SentryRuntime;
import java.lang.reflect.Type;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

@ApiStatus.Internal
public final class ContextsDeserializerAdapter implements JsonDeserializer<Contexts> {

private final ILogger logger;
private final @NotNull ILogger logger;

public ContextsDeserializerAdapter(ILogger logger) {
public ContextsDeserializerAdapter(@NotNull final ILogger logger) {
this.logger = logger;
}

Expand Down Expand Up @@ -78,7 +77,7 @@ public Contexts deserialize(JsonElement json, Type typeOfT, JsonDeserializationC
return contexts;
}
} catch (Exception e) {
logIfNotNull(logger, SentryLevel.ERROR, "Error when deserializing Contexts", e);
logger.log(SentryLevel.ERROR, "Error when deserializing Contexts", e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.sentry.android.core.adapters;

import static io.sentry.core.ILogger.logIfNotNull;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
Expand All @@ -12,13 +10,14 @@
import java.lang.reflect.Type;
import java.util.Date;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

@ApiStatus.Internal
public final class DateDeserializerAdapter implements JsonDeserializer<Date> {

private final ILogger logger;
private final @NotNull ILogger logger;

public DateDeserializerAdapter(ILogger logger) {
public DateDeserializerAdapter(final @NotNull ILogger logger) {
this.logger = logger;
}

Expand All @@ -28,7 +27,7 @@ public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationConte
try {
return json == null ? null : DateUtils.getDateTime(json.getAsString());
} catch (Exception e) {
logIfNotNull(logger, SentryLevel.ERROR, "Error when deserializing Date", e);
logger.log(SentryLevel.ERROR, "Error when deserializing Date", e);
}
return null;
}
Expand Down
Loading