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

Refactor: Use PacketLogFile for logging sent and received packets #1545

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
62 changes: 55 additions & 7 deletions app/src/main/java/com/geeksville/mesh/android/DebugLogFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
package com.geeksville.mesh.android

import android.content.Context
import android.os.Build
import com.geeksville.mesh.MeshProtos
import com.geeksville.mesh.util.toOneLineString
import java.io.File
import java.io.FileOutputStream
import java.io.PrintWriter
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Date
import java.util.Locale

/**
* Create a debug log on the SD card (if needed and allowed and app is configured for debugging (FIXME)
Expand All @@ -41,13 +49,53 @@ class DebugLogFile(context: Context, name: String) {
}
}


/**
* Create a debug log on the SD card (if needed and allowed and app is configured for debugging (FIXME)
*
* write strings to that file
* Write received and sent packets to a file
*/
class BinaryLogFile(context: Context, name: String) :
FileOutputStream(File(context.getExternalFilesDir(null), name), true) {
class PacketLogFile(val context: Context, val type: PacketLogType) {
private val name = when (type) {
PacketLogType.SENT -> "sent_packets.log"
PacketLogType.RECEIVED -> "received_packets.log"
}
private val file = PrintWriter(
FileOutputStream(File(context.getExternalFilesDir(null), name), true)
)

init {
file.use {
it.println("${getTimeStamp()}: Log Start")
}
}

fun close() {
file.use {
it.println("${getTimeStamp()}: Log End")
}
}

fun log(p: ByteArray) {
val logString = if (type == PacketLogType.SENT) {
val toRadio = MeshProtos.ToRadio.parser().parseFrom(p)
toRadio.toOneLineString()
} else {
MeshProtos.FromRadio.parser().parseFrom(p).toOneLineString()
}
file.use {
it.println("${getTimeStamp()}: $logString")
}
}

}
private fun getTimeStamp(): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
} else {
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault())
dateFormat.format(Date())
}
}

enum class PacketLogType {
SENT,
RECEIVED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.coroutineScope
import com.geeksville.mesh.BuildConfig
import com.geeksville.mesh.CoroutineDispatchers
import com.geeksville.mesh.MeshProtos
import com.geeksville.mesh.android.BinaryLogFile
import com.geeksville.mesh.android.BuildUtils
import com.geeksville.mesh.android.GeeksvilleApplication
import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.android.PacketLogFile
import com.geeksville.mesh.android.PacketLogFile.PacketLogType
import com.geeksville.mesh.concurrent.handledLaunch
import com.geeksville.mesh.repository.bluetooth.BluetoothRepository
import com.geeksville.mesh.repository.network.NetworkRepository
Expand Down Expand Up @@ -74,10 +76,10 @@ class RadioInterfaceService @Inject constructor(
private val _receivedData = MutableSharedFlow<ByteArray>()
val receivedData: SharedFlow<ByteArray> = _receivedData

private val logSends = false
private val logReceives = false
private lateinit var sentPacketsLog: BinaryLogFile // inited in onCreate
private lateinit var receivedPacketsLog: BinaryLogFile
private val logSends = BuildConfig.DEBUG
private val logReceives = BuildConfig.DEBUG
private lateinit var sentPacketsLog: PacketLogFile // inited in onCreate
private lateinit var receivedPacketsLog: PacketLogFile

val mockInterfaceAddress: String by lazy {
toInterfaceAddress(InterfaceId.MOCK, "")
Expand Down Expand Up @@ -187,14 +189,16 @@ class RadioInterfaceService @Inject constructor(

// Send a packet/command out the radio link, this routine can block if it needs to
private fun handleSendToRadio(p: ByteArray) {
if (logSends) {
sentPacketsLog.log(p)
}
radioIf.handleSendToRadio(p)
}

// Handle an incoming packet from the radio, broadcasts it as an android intent
fun handleFromRadio(p: ByteArray) {
if (logReceives) {
receivedPacketsLog.write(p)
receivedPacketsLog.flush()
receivedPacketsLog.log(p)
}

if (radioIf is SerialInterface) {
Expand Down Expand Up @@ -235,10 +239,12 @@ class RadioInterfaceService @Inject constructor(
isStarted = true

if (logSends) {
sentPacketsLog = BinaryLogFile(context, "sent_log.pb")
sentPacketsLog =
PacketLogFile(context, PacketLogType.SENT)
}
if (logReceives) {
receivedPacketsLog = BinaryLogFile(context, "receive_log.pb")
receivedPacketsLog =
PacketLogFile(context, PacketLogType.RECEIVED)
}

radioIf = interfaceFactory.createInterface(address)
Expand Down
Loading