Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for logging ExceptionModel. Updated version code to 30 #43

Merged
merged 1 commit into from
Sep 24, 2023
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
buildscript {
ext {
kotlin_version = '1.8.22'
versionName = '0.0.29'
versionName = '0.0.30'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.evdayapps.madassistant.clientlib.permission

import com.evdayapps.madassistant.common.models.exceptions.ExceptionModel
import com.evdayapps.madassistant.common.models.networkcalls.NetworkCallLogModel

/**
Expand Down Expand Up @@ -60,7 +61,12 @@ interface PermissionManager {
/**
* Test if [throwable] should be logged to the repository
*/
fun shouldLogExceptions(throwable: Throwable): Boolean
fun shouldLogException(throwable: Throwable): Boolean

/**
* Test if [exception] should be logged to the repository
*/
fun shouldLogException(exception: ExceptionModel): Boolean
// endregion Crash Logs

// region Analytics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.util.Log
import com.evdayapps.madassistant.clientlib.utils.Logger
import com.evdayapps.madassistant.clientlib.utils.matches
import com.evdayapps.madassistant.common.cipher.MADAssistantCipher
import com.evdayapps.madassistant.common.models.exceptions.ExceptionModel
import com.evdayapps.madassistant.common.models.networkcalls.NetworkCallLogModel
import com.evdayapps.madassistant.common.models.permissions.MADAssistantPermissions
import org.json.JSONException
Expand Down Expand Up @@ -154,7 +155,7 @@ class PermissionManagerImpl(
/**
* Test if [throwable] should be logged to the repository
*/
override fun shouldLogExceptions(throwable: Throwable): Boolean {
override fun shouldLogException(throwable: Throwable): Boolean {
if (!isLoggingEnabled() || (permissions?.exceptions?.enabled != true)) {
return false
}
Expand All @@ -165,6 +166,19 @@ class PermissionManagerImpl(
return chkType && chkMessage
}

override fun shouldLogException(exception: ExceptionModel): Boolean {
if (!isLoggingEnabled() || (permissions?.exceptions?.enabled != true)) {
return false
}

val chkType =
patternExceptionsType?.matches(exception.stackTrace.firstOrNull()?.className) ?: true
val chkMessage = patternExceptionsMessage?.matches(exception.message) ?: true

return chkType && chkMessage
}


// region Analytics
override fun shouldLogAnalytics(
destination: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.evdayapps.madassistant.clientlib.transmission

import com.evdayapps.madassistant.common.models.exceptions.ExceptionModel
import com.evdayapps.madassistant.common.models.networkcalls.NetworkCallLogModel

/**
Expand Down Expand Up @@ -45,7 +46,9 @@ interface Transmitter {
fun hasActiveSession(): Boolean

/**
* Disconnect from the repository. This method should be called instead of [ConnectionManager.disconnect] because it ensures processing of the log queue
* Disconnect from the repository.
*
* This method should be called instead of [ConnectionManager.disconnect] because it ensures processing of the log queue
*/
fun disconnect(code: Int, message: String)

Expand All @@ -72,6 +75,15 @@ interface Transmitter {
data: Map<String, Any>? = null
)

/**
* Log an exception model (non-fatal) in the repository
*
* Required when dealing with exceptions that come from incompatible sources, like Flutter
*/
fun logException(
exception: ExceptionModel
)

/**
* Log a generic log in the repository
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ class TransmitterImpl(
sessionId = sessionId,
first = data
)
} else {
logger?.w(
tag = TAG,
message = "No session initiated, unable to log network call.\n" +
"Please initiate a session first using startSession()",
)
}
}

Expand Down Expand Up @@ -315,6 +321,12 @@ class TransmitterImpl(
fourth = data
)
)
} else {
logger?.w(
tag = TAG,
message = "No session initiated, unable to log crash.\n" +
"Please initiate a session first using startSession()",
)
}
}

Expand All @@ -335,6 +347,32 @@ class TransmitterImpl(
third = message,
fourth = data
)
} else {
logger?.w(
tag = TAG,
message = "No session initiated, unable to log exception.\n" +
"Please initiate a session first using startSession()",
)
}
}

/**
* Log an exception model (non-fatal) in the repository
* Required when dealing with exceptions that come from incompatible sources, like Flutter
*/
override fun logException(exception: ExceptionModel) {
if (sessionId != -1L) {
queueManager.addMessageToQueue(
type = MADAssistantTransmissionType.Exception,
sessionId = sessionId,
first = exception,
)
} else {
logger?.w(
tag = TAG,
message = "No session initiated, unable to log exception.\n" +
"Please initiate a session first using startSession()",
)
}
}

Expand All @@ -343,23 +381,41 @@ class TransmitterImpl(
*/
private fun _processException(messageData: MessageData) {
try {
val throwable: Throwable = messageData.first as Throwable
if (permissionManager.shouldLogExceptions(throwable)) {
val json = ExceptionModel(
threadName = messageData.threadName,
throwable = throwable,
isCrash = messageData.second as Boolean,
message = messageData.third?.run { this as String },
data = messageData.fourth?.run { JSONObject(this as Map<String, Any?>) }
).toJsonObject().toString(0)
val value = messageData.first
when (val input = messageData.first) {
is ExceptionModel -> {
if (permissionManager.shouldLogException(input)) {
val json = input.toJsonObject().toString(0)

transmit(
json = json,
type = MADAssistantTransmissionType.Exception,
sessionId = messageData.sessionId,
timestamp = messageData.timestamp,
encrypt = permissionManager.shouldEncryptLogs()
)
}
}

transmit(
json = json,
type = MADAssistantTransmissionType.Exception,
sessionId = messageData.sessionId,
timestamp = messageData.timestamp,
encrypt = permissionManager.shouldEncryptLogs()
)
is Throwable -> {
if (permissionManager.shouldLogException(input)) {
val json = ExceptionModel(
threadName = messageData.threadName,
throwable = input,
isCrash = messageData.second as Boolean,
message = messageData.third?.run { this as String },
data = messageData.fourth?.run { JSONObject(this as Map<String, Any?>) }
).toJsonObject().toString(0)

transmit(
json = json,
type = MADAssistantTransmissionType.Exception,
sessionId = messageData.sessionId,
timestamp = messageData.timestamp,
encrypt = permissionManager.shouldEncryptLogs()
)
}
}
}
} catch (ex: Exception) {
logger?.e(ex)
Expand Down
Loading