-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
composeApp/src/commonMain/kotlin/org/ooni/probe/shared/monitoring/CrashMonitoring.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.ooni.probe.shared.monitoring | ||
|
||
import co.touchlab.kermit.LogWriter | ||
import co.touchlab.kermit.Severity | ||
import io.sentry.kotlin.multiplatform.Sentry | ||
import io.sentry.kotlin.multiplatform.SentryLevel | ||
import io.sentry.kotlin.multiplatform.protocol.Breadcrumb | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.onEach | ||
import org.ooni.probe.data.models.SettingsKey | ||
import org.ooni.probe.data.repositories.PreferenceRepository | ||
|
||
class CrashMonitoring( | ||
private val preferencesRepository: PreferenceRepository, | ||
) { | ||
private var isEnabled = false | ||
|
||
suspend fun setup() { | ||
preferencesRepository.getValueByKey(SettingsKey.SEND_CRASH) | ||
.onEach { sendCrash -> | ||
if (sendCrash == true) { | ||
Sentry.init { | ||
it.dsn = SENTRY_DSN | ||
} | ||
isEnabled = true | ||
} else { | ||
isEnabled = false | ||
Sentry.close() | ||
} | ||
} | ||
.collect() | ||
} | ||
|
||
val logWriter = object : LogWriter() { | ||
override fun isLoggable( | ||
tag: String, | ||
severity: Severity, | ||
): Boolean = isEnabled && severity != Severity.Verbose | ||
|
||
override fun log( | ||
severity: Severity, | ||
message: String, | ||
tag: String, | ||
throwable: Throwable?, | ||
) { | ||
if (!isEnabled) return | ||
|
||
if (severity == Severity.Error) { | ||
if (throwable != null) { | ||
addBreadcrumb(severity, message, tag) | ||
Sentry.captureException(throwable) | ||
} else { | ||
Sentry.captureMessage(message) | ||
} | ||
} else { | ||
addBreadcrumb(severity, message, tag) | ||
} | ||
} | ||
|
||
private fun addBreadcrumb( | ||
severity: Severity, | ||
message: String, | ||
tag: String, | ||
) { | ||
Sentry.addBreadcrumb( | ||
Breadcrumb( | ||
level = when (severity) { | ||
Severity.Verbose, | ||
Severity.Debug, | ||
-> SentryLevel.DEBUG | ||
|
||
Severity.Info -> SentryLevel.INFO | ||
Severity.Warn -> SentryLevel.WARNING | ||
Severity.Error -> SentryLevel.ERROR | ||
Severity.Assert -> SentryLevel.ERROR | ||
}, | ||
type = when (severity) { | ||
Severity.Debug -> "debug" | ||
Severity.Error -> "error" | ||
else -> "default" | ||
}, | ||
message = message, | ||
category = tag, | ||
), | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val SENTRY_DSN = | ||
"https://9dcd83d9519844188803aa817cdcd416@o155150.ingest.sentry.io/5619989" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters