Skip to content

Commit 1ee92e7

Browse files
authored
Merge pull request #450 from synonymdev/feat/logcat-ldk-logs
feat: stream ldk-node logs to android logcat
2 parents 82282b5 + b7d2941 commit 1ee92e7

File tree

8 files changed

+217
-120
lines changed

8 files changed

+217
-120
lines changed

app/src/main/java/to/bitkit/async/ServiceQueue.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import java.util.concurrent.ThreadFactory
1515
import kotlin.coroutines.CoroutineContext
1616

1717
enum class ServiceQueue {
18-
LDK, CORE, FOREX, MIGRATION;
18+
LDK, CORE, FOREX, LOG, MIGRATION;
1919

2020
private val scope by lazy { CoroutineScope(newSingleThreadDispatcher(name) + SupervisorJob()) }
2121

app/src/main/java/to/bitkit/env/Env.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ internal object Env {
2222
val platform = "Android ${Build.VERSION.RELEASE} (API ${Build.VERSION.SDK_INT})"
2323
const val version = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})"
2424

25+
val ldkLogLevel = LogLevel.TRACE
26+
2527
// TODO: remove this to load from BT API instead
2628
val trustedLnPeers
2729
get() = when (network) {
@@ -110,8 +112,8 @@ internal object Env {
110112

111113
fun initAppStoragePath(path: String) {
112114
require(path.isNotBlank()) { "App storage path cannot be empty." }
113-
Logger.info("App storage path: $path")
114115
appStoragePath = path
116+
Logger.info("App storage path: $path")
115117
}
116118

117119
val logDir: String
@@ -120,8 +122,6 @@ internal object Env {
120122
return File(appStoragePath).resolve("logs").ensureDir().path
121123
}
122124

123-
val ldkLogLevel = LogLevel.TRACE
124-
125125
fun ldkStoragePath(walletIndex: Int) = storagePathOf(walletIndex, network.name.lowercase(), "ldk")
126126

127127
fun bitkitCoreStoragePath(walletIndex: Int): String {

app/src/main/java/to/bitkit/ext/DateTime.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,20 @@ fun LocalDate.endOfDay(): Long {
122122
.toEpochMilliseconds()
123123
}
124124

125+
fun utcDateFormatterOf(pattern: String) = SimpleDateFormat(pattern, Locale.US).apply {
126+
timeZone = java.util.TimeZone.getTimeZone("UTC")
127+
}
128+
125129
object DatePattern {
126130
const val DATE_TIME = "dd/MM/yyyy, HH:mm"
127131
const val INVOICE_EXPIRY = "MMM dd, h:mm a"
128132
const val ACTIVITY_DATE = "MMMM d"
129133
const val ACTIVITY_ROW_DATE = "MMMM d, HH:mm"
130134
const val ACTIVITY_ROW_DATE_YEAR = "MMMM d yyyy, HH:mm"
131135
const val ACTIVITY_TIME = "h:mm"
132-
const val LOG_FILE = "yyyy-MM-dd_HH-mm-ss"
133136
const val CHANNEL_DETAILS = "MMM d, yyyy, HH:mm"
137+
const val LOG_FILE = "yyyy-MM-dd_HH-mm-ss"
138+
const val LOG_LINE = "yyyy-MM-dd HH:mm:ss.SSS"
134139

135140
const val MONTH_YEAR_FORMAT = "MMMM yyyy"
136141
const val DATE_FORMAT = "MMM d, yyyy"
@@ -159,4 +164,3 @@ object CalendarConstants {
159164
// Preview
160165
const val PREVIEW_DAYS_AGO = 7
161166
}
162-

app/src/main/java/to/bitkit/repositories/LogsRepo.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import to.bitkit.ext.fromBase64
1414
import to.bitkit.ext.getEnumValueOf
1515
import to.bitkit.ext.toBase64
1616
import to.bitkit.models.ChatwootMessage
17+
import to.bitkit.utils.LogSource
1718
import to.bitkit.utils.Logger
1819
import java.io.BufferedReader
1920
import java.io.ByteArrayOutputStream
@@ -57,10 +58,8 @@ class LogsRepo @Inject constructor(
5758
/** Lists log files sorted by newest first */
5859
suspend fun getLogs(): Result<List<LogFile>> = withContext(bgDispatcher) {
5960
try {
60-
val logDir = File(Env.logDir)
61-
if (!logDir.exists()) {
62-
return@withContext Result.failure(Exception("Logs dir not found"))
63-
}
61+
val logDir = runCatching { File(Env.logDir) }.getOrElse { return@withContext Result.failure(it) }
62+
if (!logDir.exists()) return@withContext Result.failure(Exception("Logs dir not found"))
6463

6564
val logFiles = logDir
6665
.listFiles { file -> file.extension == "log" }
@@ -205,5 +204,3 @@ data class LogFile(
205204
) {
206205
val fileName: String get() = file.name
207206
}
208-
209-
enum class LogSource { Ldk, Bitkit, Unknown }

app/src/main/java/to/bitkit/services/LightningService.kt

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,14 @@ import to.bitkit.data.backup.VssStoreIdProvider
4141
import to.bitkit.data.keychain.Keychain
4242
import to.bitkit.di.BgDispatcher
4343
import to.bitkit.env.Env
44-
import to.bitkit.ext.DatePattern
4544
import to.bitkit.ext.totalNextOutboundHtlcLimitSats
4645
import to.bitkit.ext.uByteList
4746
import to.bitkit.ext.uri
4847
import to.bitkit.models.OpenChannelResult
4948
import to.bitkit.utils.LdkError
49+
import to.bitkit.utils.LdkLogWriter
5050
import to.bitkit.utils.Logger
5151
import to.bitkit.utils.ServiceError
52-
import java.io.File
53-
import java.text.SimpleDateFormat
54-
import java.util.Date
55-
import java.util.Locale
56-
import java.util.TimeZone
5752
import javax.inject.Inject
5853
import javax.inject.Singleton
5954
import kotlin.io.path.Path
@@ -88,19 +83,18 @@ class LightningService @Inject constructor(
8883
val dirPath = Env.ldkStoragePath(walletIndex)
8984

9085
val trustedPeerNodeIds = trustedPeers.map { it.nodeId }
91-
9286
val config = defaultConfig().copy(
9387
storageDirPath = dirPath,
9488
network = Env.network,
9589
trustedPeers0conf = trustedPeerNodeIds,
9690
anchorChannelsConfig = AnchorChannelsConfig(
9791
trustedPeersNoReserve = trustedPeerNodeIds,
9892
perChannelReserveSats = 1u,
99-
)
93+
),
10094
)
10195

10296
val builder = Builder.fromConfig(config).apply {
103-
setFilesystemLogger(generateLogFilePath(), Env.ldkLogLevel)
97+
setCustomLogger(LdkLogWriter())
10498

10599
configureChainSource(customServerUrl)
106100
configureGossipSource(customRgsServerUrl)
@@ -346,14 +340,15 @@ class LightningService @Inject constructor(
346340

347341
return ServiceQueue.LDK.background {
348342
try {
343+
val pushToCounterpartyMsat = pushToCounterpartySats?.let { it * 1000u }
349344
Logger.debug("Initiating channel open (sats: $channelAmountSats) with peer: ${peer.uri}")
350345

351346
val userChannelId = node.openChannel(
352-
nodeId = peer.nodeId,
353-
address = peer.address,
354-
channelAmountSats = channelAmountSats,
355-
pushToCounterpartyMsat = pushToCounterpartySats?.let { it * 1000u },
356-
channelConfig = channelConfig,
347+
peer.nodeId,
348+
peer.address,
349+
channelAmountSats,
350+
pushToCounterpartyMsat,
351+
channelConfig,
357352
)
358353

359354
val result = OpenChannelResult(
@@ -790,19 +785,9 @@ class LightningService @Inject constructor(
790785
}
791786

792787
// region helpers
793-
794-
private fun generateLogFilePath(): String {
795-
val dateFormatter = SimpleDateFormat(DatePattern.LOG_FILE, Locale.US).apply {
796-
timeZone = TimeZone.getTimeZone("UTC")
797-
}
798-
val timestamp = dateFormatter.format(Date())
799-
800-
val sessionLogFilePath = File(Env.logDir).resolve("ldk_$timestamp.log").path
801-
802-
Logger.debug("Generated LDK log file path: $sessionLogFilePath")
803-
return sessionLogFilePath
804-
}
805-
788+
/**
789+
* TODO remove, replace all usages with [FeeRate.fromSatPerVbUnchecked]
790+
* */
806791
private fun convertVByteToKwu(satsPerVByte: UInt): FeeRate {
807792
// 1 vbyte = 4 weight units, so 1 sats/vbyte = 250 sats/kwu
808793
val satPerKwu = satsPerVByte.toULong() * 250u

app/src/main/java/to/bitkit/ui/screens/wallets/activity/components/ActivityListSimple.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import androidx.compose.foundation.layout.Column
44
import androidx.compose.foundation.layout.fillMaxWidth
55
import androidx.compose.foundation.layout.padding
66
import androidx.compose.foundation.layout.wrapContentWidth
7-
import androidx.compose.material3.HorizontalDivider
87
import androidx.compose.runtime.Composable
98
import androidx.compose.ui.Alignment
109
import androidx.compose.ui.Modifier

0 commit comments

Comments
 (0)