Improve debug logging mechanisms

This commit is contained in:
Mygod
2018-12-30 15:49:43 +08:00
parent f59ddb5616
commit fe33c88047
16 changed files with 99 additions and 71 deletions

View File

@@ -58,7 +58,7 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: String,
if (iface == null) Timber.w("Failed to find network interface #$index")
else return listOf(IpNeighbour(ip, iface.name, lladdr, state), result)
} catch (e: SocketException) {
Timber.w(e)
Timber.d(e)
}
listOf(result)
} catch (e: Exception) {

View File

@@ -1,8 +1,10 @@
package be.mygod.vpnhotspot.net.monitor
import android.system.ErrnoException
import android.system.OsConstants
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.DebugHelper
import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.debugLog
import be.mygod.vpnhotspot.util.thread
import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber
@@ -21,7 +23,6 @@ abstract class IpMonitor : Runnable {
Monitor, MonitorRoot, Poll
}
private class MonitorFailure : RuntimeException("Failed to set up monitor, switching to polling")
private class FlushFailure : RuntimeException()
protected abstract val monitoredObject: String
protected abstract fun processLine(line: String)
@@ -44,17 +45,17 @@ abstract class IpMonitor : Runnable {
try {
process.errorStream.bufferedReader().forEachLine { Timber.e(it) }
} catch (_: InterruptedIOException) { } catch (e: IOException) {
Timber.w(e)
if ((e.cause as? ErrnoException)?.errno != OsConstants.EBADF) Timber.w(e)
}
}
try {
process.inputStream.bufferedReader().forEachLine(this::processLine)
} catch (_: InterruptedIOException) { } catch (e: IOException) {
Timber.w(e)
if ((e.cause as? ErrnoException)?.errno != OsConstants.EBADF) Timber.w(e)
}
err.join()
process.waitFor()
debugLog("IpMonitor", "Monitor process exited with ${process.exitValue()}")
DebugHelper.log("IpMonitor", "Monitor process exited with ${process.exitValue()}")
}
init {
@@ -68,7 +69,7 @@ abstract class IpMonitor : Runnable {
}
handleProcess(ProcessBuilder("su", "-c", "exec ip monitor $monitoredObject"))
if (destroyed) return@thread
Timber.i(MonitorFailure())
DebugHelper.logEvent("ip_monitor_failure")
}
val pool = Executors.newScheduledThreadPool(1)
pool.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS)

View File

@@ -2,7 +2,7 @@ package be.mygod.vpnhotspot.net.monitor
import android.util.LongSparseArray
import androidx.core.os.postDelayed
import be.mygod.vpnhotspot.debugLog
import be.mygod.vpnhotspot.DebugHelper
import be.mygod.vpnhotspot.net.Routing.Companion.IPTABLES
import be.mygod.vpnhotspot.room.AppDatabase
import be.mygod.vpnhotspot.room.TrafficRecord
@@ -33,14 +33,14 @@ object TrafficRecorder {
downstream = downstream)
AppDatabase.instance.trafficRecordDao.insert(record)
synchronized(this) {
debugLog(TAG, "Registering ($ip, $upstream, $downstream)")
DebugHelper.log(TAG, "Registering ($ip, $upstream, $downstream)")
check(records.put(Triple(ip, upstream, downstream), record) == null)
scheduleUpdateLocked()
}
}
fun unregister(ip: InetAddress, upstream: String?, downstream: String) = synchronized(this) {
update() // flush stats before removing
debugLog(TAG, "Unregistering ($ip, $upstream, $downstream)")
DebugHelper.log(TAG, "Unregistering ($ip, $upstream, $downstream)")
if (records.remove(Triple(ip, upstream, downstream)) == null) Timber.w(
"Failed to find traffic record for ($ip, $downstream, $upstream).")
}
@@ -145,7 +145,7 @@ object TrafficRecorder {
fun clean() = synchronized(this) {
update()
unscheduleUpdateLocked()
debugLog(TAG, "Cleaning records")
DebugHelper.log(TAG, "Cleaning records")
records.clear()
}

View File

@@ -2,7 +2,7 @@ package be.mygod.vpnhotspot.net.monitor
import android.net.*
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.debugLog
import be.mygod.vpnhotspot.DebugHelper
import timber.log.Timber
object VpnMonitor : UpstreamMonitor() {
@@ -29,7 +29,7 @@ object VpnMonitor : UpstreamMonitor() {
val oldProperties = available.put(network, properties)
if (old != network) {
if (old != null) {
debugLog(TAG, "Assuming old VPN interface ${available[old]} is dying")
DebugHelper.log(TAG, "Assuming old VPN interface ${available[old]} is dying")
callbacks.forEach { it.onLost() }
}
currentNetwork = network
@@ -75,7 +75,7 @@ object VpnMonitor : UpstreamMonitor() {
if (available.isNotEmpty()) {
val next = available.entries.first()
currentNetwork = next.key
debugLog(TAG, "Switching to ${next.value.interfaceName} as VPN interface")
DebugHelper.log(TAG, "Switching to ${next.value.interfaceName} as VPN interface")
callbacks.forEach { it.onAvailable(next.value.interfaceName!!, next.value.dnsServers) }
} else currentNetwork = null
}

View File

@@ -3,7 +3,7 @@ package be.mygod.vpnhotspot.net.wifi
import android.net.wifi.p2p.WifiP2pGroup
import android.os.Build
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.timberSetString
import be.mygod.vpnhotspot.DebugHelper
import be.mygod.vpnhotspot.util.RootSession
import java.io.File
@@ -79,9 +79,9 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, ownerAddress:
}
Pair(result, target!!)
} catch (e: RuntimeException) {
timberSetString(TAG, parser.lines.joinToString("\n"))
timberSetString("$TAG.ownerAddress", ownerAddress)
timberSetString("$TAG.p2pGroup", group.toString())
DebugHelper.setString(TAG, parser.lines.joinToString("\n"))
DebugHelper.setString("$TAG.ownerAddress", ownerAddress)
DebugHelper.setString("$TAG.p2pGroup", group.toString())
throw e
}
}

View File

@@ -4,8 +4,10 @@ import android.annotation.SuppressLint
import android.net.wifi.WpsInfo
import android.net.wifi.p2p.WifiP2pGroup
import android.net.wifi.p2p.WifiP2pManager
import be.mygod.vpnhotspot.DebugHelper
import com.android.dx.stock.ProxyBuilder
import timber.log.Timber
import java.lang.IllegalArgumentException
import java.lang.reflect.Proxy
object WifiP2pManagerHelper {
@@ -27,7 +29,7 @@ object WifiP2pManagerHelper {
try {
setWifiP2pChannels.invoke(this, c, lc, oc, listener)
} catch (e: NoSuchMethodException) {
Timber.w(e)
DebugHelper.logEvent("NoSuchMethod_setWifiP2pChannels")
listener.onFailure(UNSUPPORTED)
}
}
@@ -45,7 +47,7 @@ object WifiP2pManagerHelper {
try {
startWps.invoke(this, c, wps, listener)
} catch (e: NoSuchMethodException) {
Timber.w(e)
DebugHelper.logEvent("NoSuchMethod_startWps")
listener.onFailure(UNSUPPORTED)
}
}
@@ -64,7 +66,7 @@ object WifiP2pManagerHelper {
try {
deletePersistentGroup.invoke(this, c, netId, listener)
} catch (e: NoSuchMethodException) {
Timber.w(e)
DebugHelper.logEvent("NoSuchMethod_deletePersistentGroup")
listener.onFailure(UNSUPPORTED)
}
}
@@ -90,11 +92,11 @@ object WifiP2pManagerHelper {
val proxy = Proxy.newProxyInstance(interfacePersistentGroupInfoListener.classLoader,
arrayOf(interfacePersistentGroupInfoListener)) { proxy, method, args ->
if (method.name == "onPersistentGroupInfoAvailable") {
if (args.size != 1) Timber.w("Unexpected args: $args")
if (args.size != 1) Timber.w(IllegalArgumentException("Unexpected args: $args"))
listener(getGroupList.invoke(args[0]) as Collection<WifiP2pGroup>)
null
} else {
Timber.w("Unexpected method, calling super: $method")
Timber.w(IllegalArgumentException("Unexpected method, calling super: $method"))
ProxyBuilder.callSuper(proxy, method, args)
}
}