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

chore: add structured logs to location and fix permission handling (WPB-6358) #2736

Merged
merged 2 commits into from
Feb 23, 2024
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 app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ android {
// For all other flavors use the "nonfree" sourceSets
} else {
getByName("main") {
java.srcDirs("src/nonfree/kotlin")
java.srcDirs("src/main/kotlin", "src/nonfree/kotlin")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix, otherwise we don't have autocompletion at all for classes coming from this nonfree

println("Building with non-free sourceSets")
}
}
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/kotlin/com/wire/android/AppLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,53 @@

import com.wire.kalium.logger.KaliumLogLevel
import com.wire.kalium.logger.KaliumLogger
import com.wire.kalium.util.serialization.toJsonElement

private var appLoggerConfig = KaliumLogger.Config.disabled()

// App wide global logger, carefully initialized when our application is "onCreate"
internal var appLogger = KaliumLogger.disabled()

object AppLogger {
fun init(config: KaliumLogger.Config) {
appLoggerConfig = config
appLogger = KaliumLogger(config = config, tag = "WireAppLogger")
}

fun setLogLevel(level: KaliumLogLevel) {
appLoggerConfig.setLogLevel(level)
}
}

object AppJsonStyledLogger {
/**
* Log a structured JSON message, in the following format:
*
* Example:
* ```
* leadingMessage: {map of key-value pairs represented as JSON}
* ```
* @param level the severity of the log message
* @param error optional - the throwable error to be logged
* @param leadingMessage the leading message useful for later grok parsing
* @param jsonStringKeyValues the map of key-value pairs to be logged in a valid JSON format
*/
fun log(
level: KaliumLogLevel,
error: Throwable? = null,
leadingMessage: String,
jsonStringKeyValues: Map<String, Any?>
) = with(appLogger) {
val logJson = jsonStringKeyValues.toJsonElement()
val sanitizedLeadingMessage = if (leadingMessage.endsWith(":")) leadingMessage else "$leadingMessage:"
val logMessage = "$sanitizedLeadingMessage $logJson"
when (level) {
KaliumLogLevel.DEBUG -> d(logMessage)

Check warning on line 63 in app/src/main/kotlin/com/wire/android/AppLogger.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/AppLogger.kt#L63

Added line #L63 was not covered by tests
KaliumLogLevel.INFO -> i(logMessage)
KaliumLogLevel.WARN -> w(logMessage)
KaliumLogLevel.ERROR -> e(logMessage, throwable = error)
KaliumLogLevel.VERBOSE -> v(logMessage)
KaliumLogLevel.DISABLED -> Unit

Check warning on line 68 in app/src/main/kotlin/com/wire/android/AppLogger.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/AppLogger.kt#L66-L68

Added lines #L66 - L68 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import androidx.core.location.LocationManagerCompat
import com.wire.android.AppJsonStyledLogger
import com.wire.kalium.logger.KaliumLogLevel
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

Expand All @@ -32,6 +34,11 @@ open class LocationPickerHelper @Inject constructor(@ApplicationContext val cont
@SuppressLint("MissingPermission")
protected fun getLocationWithoutGms(onSuccess: (GeoLocatedAddress) -> Unit, onError: () -> Unit) {
if (isLocationServicesEnabled()) {
AppJsonStyledLogger.log(
level = KaliumLogLevel.INFO,
leadingMessage = "GetLocation",
jsonStringKeyValues = mapOf("isUsingGms" to false)
)
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val networkLocationListener: LocationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
Expand All @@ -42,6 +49,14 @@ open class LocationPickerHelper @Inject constructor(@ApplicationContext val cont
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, networkLocationListener)
} else {
AppJsonStyledLogger.log(
level = KaliumLogLevel.WARN,
leadingMessage = "GetLocation",
jsonStringKeyValues = mapOf(
"isUsingGms" to false,
"error" to "Location services are not enabled"
)
)
onError()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import android.location.Geocoder
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority
import com.google.android.gms.tasks.CancellationTokenSource
import com.wire.android.AppJsonStyledLogger
import com.wire.android.util.extension.isGoogleServicesAvailable
import com.wire.kalium.logger.KaliumLogLevel
import kotlinx.coroutines.tasks.await
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.tasks.await

@Singleton
class LocationPickerHelperFlavor @Inject constructor(context: Context) : LocationPickerHelper(context) {
Expand All @@ -52,12 +54,25 @@ class LocationPickerHelperFlavor @Inject constructor(context: Context) : Locatio
@SuppressLint("MissingPermission")
private suspend fun getLocationWithGms(onSuccess: (GeoLocatedAddress) -> Unit, onError: () -> Unit) {
if (isLocationServicesEnabled()) {
AppJsonStyledLogger.log(
level = KaliumLogLevel.INFO,
leadingMessage = "GetLocation",
jsonStringKeyValues = mapOf("isUsingGms" to true)
)
val locationProvider = LocationServices.getFusedLocationProviderClient(context)
val currentLocation =
locationProvider.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, CancellationTokenSource().token).await()
val address = Geocoder(context).getFromLocation(currentLocation.latitude, currentLocation.longitude, 1).orEmpty()
onSuccess(GeoLocatedAddress(address.firstOrNull(), currentLocation))
} else {
AppJsonStyledLogger.log(
level = KaliumLogLevel.WARN,
leadingMessage = "GetLocation",
jsonStringKeyValues = mapOf(
"isUsingGms" to true,
"error" to "Location services are not enabled"
)
)
onError()
}
}
Expand Down
Loading