-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
NdkIntegration.java
100 lines (85 loc) · 3.46 KB
/
NdkIntegration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package io.sentry.android.core;
import io.sentry.IHub;
import io.sentry.Integration;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Method;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
/** Enables the NDK error reporting for Android */
public final class NdkIntegration implements Integration, Closeable {
public static final String SENTRY_NDK_CLASS_NAME = "io.sentry.android.ndk.SentryNdk";
private final @Nullable Class<?> sentryNdkClass;
private @Nullable SentryAndroidOptions options;
public NdkIntegration(final @Nullable Class<?> sentryNdkClass) {
this.sentryNdkClass = sentryNdkClass;
}
@Override
public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) {
Objects.requireNonNull(hub, "Hub is required");
this.options =
Objects.requireNonNull(
(options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null,
"SentryAndroidOptions is required");
final boolean enabled = this.options.isEnableNdk();
this.options.getLogger().log(SentryLevel.DEBUG, "NdkIntegration enabled: %s", enabled);
// Note: `hub` isn't used here because the NDK integration writes files to disk which are picked
// up by another integration (EnvelopeFileObserverIntegration).
if (enabled && sentryNdkClass != null) {
final String cachedDir = this.options.getCacheDirPath();
if (cachedDir == null || cachedDir.isEmpty()) {
this.options.getLogger().log(SentryLevel.ERROR, "No cache dir path is defined in options.");
disableNdkIntegration(this.options);
return;
}
try {
final Method method = sentryNdkClass.getMethod("init", SentryAndroidOptions.class);
final Object[] args = new Object[1];
args[0] = this.options;
method.invoke(null, args);
this.options.getLogger().log(SentryLevel.DEBUG, "NdkIntegration installed.");
} catch (NoSuchMethodException e) {
disableNdkIntegration(this.options);
this.options
.getLogger()
.log(SentryLevel.ERROR, "Failed to invoke the SentryNdk.init method.", e);
} catch (Throwable e) {
disableNdkIntegration(this.options);
this.options.getLogger().log(SentryLevel.ERROR, "Failed to initialize SentryNdk.", e);
}
} else {
disableNdkIntegration(this.options);
}
}
private void disableNdkIntegration(final @NotNull SentryOptions options) {
options.setEnableNdk(false);
options.setEnableScopeSync(false);
}
@TestOnly
@Nullable
Class<?> getSentryNdkClass() {
return sentryNdkClass;
}
@Override
public void close() throws IOException {
if (options != null && options.isEnableNdk() && sentryNdkClass != null) {
try {
final Method method = sentryNdkClass.getMethod("close");
method.invoke(null, new Object[0]);
options.getLogger().log(SentryLevel.DEBUG, "NdkIntegration removed.");
} catch (NoSuchMethodException e) {
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to invoke the SentryNdk.close method.", e);
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, "Failed to close SentryNdk.", e);
} finally {
disableNdkIntegration(this.options);
}
}
}
}