mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-24 22:45:20 +00:00
visual logger refactor (#583)
This commit is contained in:
@@ -158,9 +158,11 @@ class BluetoothMeshService(private val context: Context) {
|
|||||||
*/
|
*/
|
||||||
private fun setupDelegates() {
|
private fun setupDelegates() {
|
||||||
Log.d(TAG, "Setting up component delegates")
|
Log.d(TAG, "Setting up component delegates")
|
||||||
// Provide nickname resolver to BLE broadcaster for detailed logs
|
// Provide nickname resolver to BLE broadcaster and debug manager
|
||||||
try {
|
try {
|
||||||
connectionManager.setNicknameResolver { pid -> peerManager.getPeerNickname(pid) }
|
val resolver: (String) -> String? = { pid -> peerManager.getPeerNickname(pid) }
|
||||||
|
connectionManager.setNicknameResolver(resolver)
|
||||||
|
debugManager?.setNicknameResolver(resolver)
|
||||||
} catch (_: Exception) { }
|
} catch (_: Exception) { }
|
||||||
// PeerManager delegates to main mesh service delegate
|
// PeerManager delegates to main mesh service delegate
|
||||||
peerManager.delegate = object : PeerManagerDelegate {
|
peerManager.delegate = object : PeerManagerDelegate {
|
||||||
@@ -546,21 +548,12 @@ class BluetoothMeshService(private val context: Context) {
|
|||||||
override fun onPacketReceived(packet: BitchatPacket, peerID: String, device: android.bluetooth.BluetoothDevice?) {
|
override fun onPacketReceived(packet: BitchatPacket, peerID: String, device: android.bluetooth.BluetoothDevice?) {
|
||||||
// Log incoming for debug graphs (do not double-count anywhere else)
|
// Log incoming for debug graphs (do not double-count anywhere else)
|
||||||
try {
|
try {
|
||||||
val nick = getPeerNicknames()[peerID]
|
|
||||||
val route = packet.route
|
|
||||||
val routeInfo = if (!route.isNullOrEmpty()) "routed: ${route.size} hops" else null
|
|
||||||
|
|
||||||
// Convert route to hex strings for visualization
|
|
||||||
val routeStrings = route?.map { it.toHexString() }
|
|
||||||
|
|
||||||
com.bitchat.android.ui.debug.DebugSettingsManager.getInstance().logIncoming(
|
com.bitchat.android.ui.debug.DebugSettingsManager.getInstance().logIncoming(
|
||||||
packetType = packet.type.toString(),
|
packet = packet,
|
||||||
fromPeerID = peerID,
|
fromPeerID = peerID,
|
||||||
fromNickname = nick,
|
fromNickname = null,
|
||||||
fromDeviceAddress = device?.address,
|
fromDeviceAddress = device?.address,
|
||||||
packetVersion = packet.version,
|
myPeerID = myPeerID
|
||||||
routeInfo = routeInfo,
|
|
||||||
route = routeStrings
|
|
||||||
)
|
)
|
||||||
} catch (_: Exception) { }
|
} catch (_: Exception) { }
|
||||||
packetProcessor.processPacket(RoutedPacket(packet, peerID, device?.address))
|
packetProcessor.processPacket(RoutedPacket(packet, peerID, device?.address))
|
||||||
|
|||||||
@@ -78,8 +78,6 @@ class PacketProcessor(private val myPeerID: String) {
|
|||||||
Log.w(TAG, "Received packet with no peer ID, skipping")
|
Log.w(TAG, "Received packet with no peer ID, skipping")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Get or create actor for this peer
|
// Get or create actor for this peer
|
||||||
val actor = actors.getOrPut(peerID) { getOrCreateActorForPeer(peerID) }
|
val actor = actors.getOrPut(peerID) { getOrCreateActorForPeer(peerID) }
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import kotlinx.coroutines.flow.SharedFlow
|
|||||||
import kotlinx.coroutines.flow.asSharedFlow
|
import kotlinx.coroutines.flow.asSharedFlow
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue
|
import java.util.concurrent.ConcurrentLinkedQueue
|
||||||
|
import com.bitchat.android.protocol.BitchatPacket
|
||||||
|
import com.bitchat.android.util.toHexString
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Debug settings manager for controlling debug features and collecting debug data
|
* Debug settings manager for controlling debug features and collecting debug data
|
||||||
@@ -485,20 +487,32 @@ class DebugSettingsManager private constructor() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Peer nickname resolver
|
||||||
|
private var nicknameResolver: ((String) -> String?)? = null
|
||||||
|
fun setNicknameResolver(resolver: (String) -> String?) { nicknameResolver = resolver }
|
||||||
|
|
||||||
// Explicit incoming/outgoing logging to avoid double counting
|
// Explicit incoming/outgoing logging to avoid double counting
|
||||||
fun logIncoming(packetType: String, fromPeerID: String?, fromNickname: String?, fromDeviceAddress: String?, packetVersion: UByte = 1u, routeInfo: String? = null, route: List<String>? = null) {
|
fun logIncoming(packet: BitchatPacket, fromPeerID: String, fromNickname: String?, fromDeviceAddress: String?, myPeerID: String) {
|
||||||
|
val packetType = packet.type.toString()
|
||||||
|
val packetVersion = packet.version
|
||||||
|
val route = packet.route
|
||||||
|
val routeInfo = if (!route.isNullOrEmpty()) "routed: ${route.size} hops" else null
|
||||||
|
|
||||||
if (verboseLoggingEnabled.value) {
|
if (verboseLoggingEnabled.value) {
|
||||||
val who = fromNickname ?: fromPeerID ?: "unknown"
|
val resolvedNick = fromNickname ?: nicknameResolver?.invoke(fromPeerID) ?: "unknown"
|
||||||
|
val who = if (resolvedNick != "unknown") "$resolvedNick ($fromPeerID)" else fromPeerID
|
||||||
val routeStr = if (routeInfo != null) " $routeInfo" else ""
|
val routeStr = if (routeInfo != null) " $routeInfo" else ""
|
||||||
addDebugMessage(DebugMessage.PacketEvent("📥 Incoming v$packetVersion $packetType from $who (${fromPeerID ?: "?"}, ${fromDeviceAddress ?: "?"})$routeStr"))
|
addDebugMessage(DebugMessage.PacketEvent("📥 Incoming v$packetVersion $packetType from $who (${fromDeviceAddress ?: "?"})$routeStr"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Emit visual events
|
emitVisualEvent(MeshVisualEvent.PacketActivity(fromPeerID))
|
||||||
if (fromPeerID != null) {
|
|
||||||
emitVisualEvent(MeshVisualEvent.PacketActivity(fromPeerID))
|
|
||||||
}
|
|
||||||
if (!route.isNullOrEmpty()) {
|
if (!route.isNullOrEmpty()) {
|
||||||
emitVisualEvent(MeshVisualEvent.RouteActivity(route))
|
val fullRoute = mutableListOf<String>()
|
||||||
|
fullRoute.add(packet.senderID.toHexString())
|
||||||
|
route.forEach { fullRoute.add(it.toHexString()) }
|
||||||
|
packet.recipientID?.let { fullRoute.add(it.toHexString()) }
|
||||||
|
emitVisualEvent(MeshVisualEvent.RouteActivity(fullRoute))
|
||||||
}
|
}
|
||||||
|
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
@@ -509,11 +523,11 @@ class DebugSettingsManager private constructor() {
|
|||||||
deviceIncomingTotalsMap[it] = (deviceIncomingTotalsMap[it] ?: 0L) + 1L
|
deviceIncomingTotalsMap[it] = (deviceIncomingTotalsMap[it] ?: 0L) + 1L
|
||||||
_perDeviceIncomingTotalsFlow.value = deviceIncomingTotalsMap.toMap()
|
_perDeviceIncomingTotalsFlow.value = deviceIncomingTotalsMap.toMap()
|
||||||
}
|
}
|
||||||
fromPeerID?.let {
|
|
||||||
perPeerIncoming.getOrPut(it) { ConcurrentLinkedQueue() }.offer(now)
|
perPeerIncoming.getOrPut(fromPeerID) { ConcurrentLinkedQueue() }.offer(now)
|
||||||
peerIncomingTotalsMap[it] = (peerIncomingTotalsMap[it] ?: 0L) + 1L
|
peerIncomingTotalsMap[fromPeerID] = (peerIncomingTotalsMap[fromPeerID] ?: 0L) + 1L
|
||||||
_perPeerIncomingTotalsFlow.value = peerIncomingTotalsMap.toMap()
|
_perPeerIncomingTotalsFlow.value = peerIncomingTotalsMap.toMap()
|
||||||
}
|
|
||||||
// bump totals
|
// bump totals
|
||||||
val cur = _relayStats.value
|
val cur = _relayStats.value
|
||||||
_relayStats.value = cur.copy(
|
_relayStats.value = cur.copy(
|
||||||
|
|||||||
Reference in New Issue
Block a user