Skip to content

Commit

Permalink
Merge branch 'main' into kari/searchflag
Browse files Browse the repository at this point in the history
Signed-off-by: kari-ts <[email protected]>
  • Loading branch information
kari-ts authored Dec 10, 2024
2 parents aeedaa4 + ebbc1b2 commit a6adff7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
14 changes: 10 additions & 4 deletions android/src/main/java/com/tailscale/ipn/ui/localapi/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.tailscale.ipn.ui.localapi

import android.content.Context
import com.tailscale.ipn.App
import com.tailscale.ipn.ui.model.BugReportID
import com.tailscale.ipn.ui.model.Errors
import com.tailscale.ipn.ui.model.Ipn
Expand All @@ -13,7 +14,6 @@ import com.tailscale.ipn.ui.model.StableNodeID
import com.tailscale.ipn.ui.model.Tailcfg
import com.tailscale.ipn.ui.util.InputStreamAdapter
import com.tailscale.ipn.util.TSLog
import com.tailscale.ipn.App
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -69,9 +69,7 @@ class Client(private val scope: CoroutineScope) {
private val TAG = Client::class.simpleName

// Access libtailscale.Application lazily
private val app: libtailscale.Application by lazy {
App.get().getLibtailscaleApp()
}
private val app: libtailscale.Application by lazy { App.get().getLibtailscaleApp() }

fun start(options: Ipn.Options, responseHandler: (Result<Unit>) -> Unit) {
val body = Json.encodeToString(options).toByteArray()
Expand Down Expand Up @@ -102,6 +100,14 @@ class Client(private val scope: CoroutineScope) {
}

fun editPrefs(prefs: Ipn.MaskedPrefs, responseHandler: (Result<Ipn.Prefs>) -> Unit) {
// Log the stack trace for debugging purposes for
// https://github.com/tailscale/tailscale/issues/14125
val stackTrace =
Thread.currentThread().stackTrace.joinToString("\n") { element ->
"at ${element.className}.${element.methodName}(${element.fileName}:${element.lineNumber})"
}
TSLog.v("editPrefs", "Called editPrefs with prefs: $prefs\nStack trace:\n$stackTrace")

val body = Json.encodeToString(prefs).toByteArray()
return patch(Endpoint.PREFS, body, responseHandler = responseHandler)
}
Expand Down
22 changes: 22 additions & 0 deletions android/src/main/java/com/tailscale/ipn/util/TSLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
// SPDX-License-Identifier: BSD-3-Clause
package com.tailscale.ipn.util

import android.content.Context
import android.util.Log
import libtailscale.Libtailscale

object TSLog {
private lateinit var appContext: Context
var libtailscaleWrapper = LibtailscaleWrapper()

fun init(context: Context) {
appContext = context.applicationContext
}

fun d(tag: String?, message: String) {
Log.d(tag, message)
libtailscaleWrapper.sendLog(tag, message)
Expand All @@ -18,6 +24,13 @@ object TSLog {
libtailscaleWrapper.sendLog(tag, message)
}

fun v(tag: String?, message: String) {
if (isUnstableRelease()) {
Log.v(tag, message)
libtailscaleWrapper.sendLog(tag, message)
}
}

// Overloaded function without Throwable because Java does not support default parameters
@JvmStatic
fun e(tag: String?, message: String) {
Expand All @@ -35,6 +48,15 @@ object TSLog {
}
}

private fun isUnstableRelease(): Boolean {
val versionName =
appContext.packageManager.getPackageInfo(appContext.packageName, 0).versionName

// Extract the middle number and check if it's odd
val middleNumber = versionName.split(".").getOrNull(1)?.toIntOrNull()
return middleNumber?.let { it % 2 == 1 } ?: false
}

class LibtailscaleWrapper {
public fun sendLog(tag: String?, message: String) {
val logTag = tag ?: ""
Expand Down

0 comments on commit a6adff7

Please sign in to comment.