Skip to content

Commit

Permalink
Merge pull request #113 from Infomaniak/sentry-module
Browse files Browse the repository at this point in the history
feat: Add Core2 & SentryLog modules
  • Loading branch information
KevinBoulongne authored Oct 23, 2024
2 parents 4df093a + ef29a8f commit 108d59a
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 11 deletions.
34 changes: 34 additions & 0 deletions Core2/Sentry/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
}

android {
namespace = "com.infomaniak.sentry"
compileSdk = 34

defaultConfig {
minSdk = 24

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
api(core2.sentry.android)
}
Empty file added Core2/Sentry/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions Core2/Sentry/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
103 changes: 103 additions & 0 deletions Core2/Sentry/src/main/java/com/infomaniak/sentry/SentryLog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Infomaniak Core - Android
* Copyright (C) 2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.sentry

import android.util.Log
import io.sentry.Breadcrumb
import io.sentry.Sentry
import io.sentry.SentryEvent
import io.sentry.SentryLevel
import io.sentry.protocol.Message

object SentryLog {

private val TAG = SentryLog::class.java.simpleName

fun v(tag: String, msg: String, throwable: Throwable? = null) {
val formattedMessage = formatLogMessage(tag, msg)
Log.v(TAG, formattedMessage, throwable)
SentryLevel.DEBUG.addBreadcrumb(formattedMessage, throwable)
}

fun d(tag: String, msg: String, throwable: Throwable? = null) {
val formattedMessage = formatLogMessage(tag, msg)
Log.d(TAG, formattedMessage, throwable)
SentryLevel.DEBUG.addBreadcrumb(formattedMessage, throwable)
}

fun i(tag: String, msg: String, throwable: Throwable? = null) {
val formattedMessage = formatLogMessage(tag, msg)
Log.i(TAG, formattedMessage, throwable)
SentryLevel.INFO.addBreadcrumb(formattedMessage, throwable)
}

fun w(tag: String, msg: String, throwable: Throwable? = null) {
val formattedMessage = formatLogMessage(tag, msg)
Log.w(TAG, formattedMessage, throwable)
SentryLevel.WARNING.addBreadcrumb(formattedMessage, throwable)
}

fun e(tag: String, msg: String, throwable: Throwable? = null) {
val formattedMessage = formatLogMessage(tag, msg)
Log.e(TAG, formattedMessage, throwable)
SentryLevel.ERROR.apply {
addBreadcrumb(formattedMessage, throwable)
captureEvent(tag, msg, throwable)
}
}

fun wtf(tag: String, msg: String, throwable: Throwable? = null) {
val formattedMessage = formatLogMessage(tag, msg)
Log.wtf(TAG, formattedMessage, throwable)
SentryLevel.FATAL.apply {
addBreadcrumb(formattedMessage, throwable)
captureEvent(tag, msg, throwable)
}
}

private fun SentryLevel.captureEvent(tag: String, msg: String, throwable: Throwable?) {
val sentryEvent = SentryEvent().apply {
setTag("SentryLogTag", tag)
level = this@captureEvent
logger = TAG
message = Message().apply { this.message = msg }
throwable?.let(::setThrowable)
}

Sentry.captureEvent(sentryEvent)
}

private fun SentryLevel.addBreadcrumb(msg: String, throwable: Throwable?) {

val throwableMsg = throwable?.message
val breadCrumb = when {
throwableMsg != null -> Breadcrumb.error(throwableMsg).apply {
category = "exception"
}
else -> Breadcrumb().apply {
level = this@addBreadcrumb
category = TAG
message = msg
}
}

Sentry.addBreadcrumb(breadCrumb)
}

private fun formatLogMessage(tag: String, msg: String) = "($tag): $msg"
}
34 changes: 34 additions & 0 deletions Core2/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
}

android {
namespace = "com.infomaniak.core2"
compileSdk = 34

defaultConfig {
minSdk = 24

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {

}
Empty file added Core2/consumer-rules.pro
Empty file.
5 changes: 5 additions & 0 deletions Core2/gradle/core2.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[versions]
sentry-android = "7.15.0"

[libraries]
sentry-android = { module = "io.sentry:sentry-android", version.ref = "sentry-android" }
21 changes: 21 additions & 0 deletions Core2/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
1 change: 0 additions & 1 deletion FileTypes/.gitignore

This file was deleted.

8 changes: 4 additions & 4 deletions FileTypes/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ android {
dependencies {

implementation(libs.androidx.core.ktx)
implementation(filetype.androidx.ui.android)
implementation(filetype.androidx.foundation.android)
implementation(filetype.androidx.ui.tooling.preview.android)
implementation(filetypes.androidx.ui.android)
implementation(filetypes.androidx.foundation.android)
implementation(filetypes.androidx.ui.tooling.preview.android)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
debugImplementation(filetype.androidx.ui.tooling)
debugImplementation(filetypes.androidx.ui.tooling)
}
File renamed without changes.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ sentry {
}

dependencies {
implementation(project(":Core2:Sentry"))
implementation(project(":FileTypes"))
implementation(kotlin("reflect"))

Expand All @@ -96,7 +97,6 @@ dependencies {
implementation(libs.compose.material3.adaptative.navigation)
implementation(libs.navigation.compose)
implementation(libs.androidx.constraintlayout.compose)
implementation(libs.sentry.android)

implementation(libs.androidx.adaptive)
implementation(libs.androidx.adaptive.layout)
Expand Down
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ navigation = "2.8.1"
serialization = "1.7.1"
swisstransfer = "0.3.0"
sentry = "4.12.0"
sentry-android = "7.15.0"
recaptcha = "18.6.1"

[libraries]
Expand All @@ -45,7 +44,6 @@ hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", ve
kotlinx-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialization" }
navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigation" }
swisstransfer-core = { module = "com.github.Infomaniak.multiplatform-SwissTransfer:STCore", version.ref = "swisstransfer" }
sentry-android = { module = "io.sentry:sentry-android", version.ref = "sentry-android" }
recaptcha = { module = "com.google.android.recaptcha:recaptcha", version.ref = "recaptcha" }
# Tests
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
Expand Down
7 changes: 4 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ dependencyResolutionManagement {
maven { url = uri("https://jitpack.io") }
}
versionCatalogs {
create("filetype") {
from(files("FileTypes/gradle/filetype.versions.toml"))
}
create("core2") { from(files("Core2/gradle/core2.versions.toml")) }
create("filetypes") { from(files("FileTypes/gradle/filetypes.versions.toml")) }
}
}

rootProject.name = "android-SwissTransfer"
include(":app")
include(":Core2")
include(":Core2:Sentry")
include(":FileTypes")

0 comments on commit 108d59a

Please sign in to comment.