refactor: simplify PlatformLog actuals and convert remaining interpolated log calls to lambdas
- Extract private log() helper in JVM and iOS PlatformLog to reduce copy-paste branching - Fix JVM formatter to be private and non-nullable - Convert 60+ interpolated Log.w/e/i calls to lambda overloads, including hot-path Filter.kt toJson() and LocalCache event processing Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -23,51 +23,40 @@ package com.vitorpamplona.quartz.utils
|
||||
import platform.Foundation.NSLog
|
||||
|
||||
actual object PlatformLog {
|
||||
actual fun w(
|
||||
private fun log(
|
||||
level: String,
|
||||
tag: String,
|
||||
message: String,
|
||||
throwable: Throwable?,
|
||||
) {
|
||||
if (throwable != null) {
|
||||
NSLog("WARN: [$tag] $message. Throwable: $throwable CAUSE ${throwable.cause}")
|
||||
NSLog("$level: [$tag] $message. Throwable: $throwable CAUSE ${throwable.cause}")
|
||||
} else {
|
||||
NSLog("WARN: [$tag] $message")
|
||||
NSLog("$level: [$tag] $message")
|
||||
}
|
||||
}
|
||||
|
||||
actual fun w(
|
||||
tag: String,
|
||||
message: String,
|
||||
throwable: Throwable?,
|
||||
) = log("WARN", tag, message, throwable)
|
||||
|
||||
actual fun e(
|
||||
tag: String,
|
||||
message: String,
|
||||
throwable: Throwable?,
|
||||
) {
|
||||
if (throwable != null) {
|
||||
NSLog("ERROR: [$tag] $message. Throwable: $throwable CAUSE ${throwable.cause}")
|
||||
} else {
|
||||
NSLog("ERROR: [$tag] $message")
|
||||
}
|
||||
}
|
||||
) = log("ERROR", tag, message, throwable)
|
||||
|
||||
actual fun d(
|
||||
tag: String,
|
||||
message: String,
|
||||
throwable: Throwable?,
|
||||
) {
|
||||
if (throwable != null) {
|
||||
NSLog("DEBUG: [$tag] $message. Throwable: $throwable CAUSE ${throwable.cause}")
|
||||
} else {
|
||||
NSLog("DEBUG: [$tag] $message")
|
||||
}
|
||||
}
|
||||
) = log("DEBUG", tag, message, throwable)
|
||||
|
||||
actual fun i(
|
||||
tag: String,
|
||||
message: String,
|
||||
throwable: Throwable?,
|
||||
) {
|
||||
if (throwable != null) {
|
||||
NSLog("INFO: [$tag] $message. Throwable: $throwable CAUSE ${throwable.cause}")
|
||||
} else {
|
||||
NSLog("INFO: [$tag] $message")
|
||||
}
|
||||
}
|
||||
) = log("INFO", tag, message, throwable)
|
||||
}
|
||||
|
||||
+3
-3
@@ -46,7 +46,7 @@ class AddressSerializer {
|
||||
if (parts.size > 2 && parts[1].length == 64 && Hex.isHex(parts[1])) {
|
||||
if (parts[0].length > 5) {
|
||||
// invalid kind
|
||||
Log.w("AddressableId", "Error parsing. invalid kind $addressId")
|
||||
Log.w("AddressableId") { "Error parsing. invalid kind $addressId" }
|
||||
null
|
||||
} else {
|
||||
Address(parts[0].toInt(), parts[1], parts.getOrNull(2) ?: "")
|
||||
@@ -57,11 +57,11 @@ class AddressSerializer {
|
||||
if (addr is NAddress) {
|
||||
addr.address()
|
||||
} else {
|
||||
Log.w("AddressableId", "Error parsing. naddr1 seems invalid: $addressId")
|
||||
Log.w("AddressableId") { "Error parsing. naddr1 seems invalid: $addressId" }
|
||||
null
|
||||
}
|
||||
} else {
|
||||
Log.w("AddressableId", "Error parsing. Not a valid address: $addressId")
|
||||
Log.w("AddressableId") { "Error parsing. Not a valid address: $addressId" }
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -71,7 +71,7 @@ class MetadataEvent(
|
||||
Json.parseToJsonElement(content) as JsonObject
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Log.w("MetadataEvent", "Content Parse Error: ${toNostrUri()} ${e.message}")
|
||||
Log.w("MetadataEvent") { "Content Parse Error: ${toNostrUri()} ${e.message}" }
|
||||
null
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ class MetadataEvent(
|
||||
JsonMapper.fromJson<UserMetadata>(content)
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Log.w("MetadataEvent", "Content Parse Error: ${toNostrUri()} ${e.message}")
|
||||
Log.w("MetadataEvent") { "Content Parse Error: ${toNostrUri()} ${e.message}" }
|
||||
null
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ fun List<RelayBasedFilter>.groupByRelay(): Map<NormalizedRelayUrl, List<Filter>>
|
||||
val result = mutableMapOf<NormalizedRelayUrl, MutableList<Filter>>()
|
||||
for (relayBasedFilter in this) {
|
||||
if (relayBasedFilter.filter.isEmpty()) {
|
||||
Log.e("FilterError", "Ignoring empty filter for ${relayBasedFilter.relay}")
|
||||
Log.e("FilterError") { "Ignoring empty filter for ${relayBasedFilter.relay}" }
|
||||
} else {
|
||||
result.getOrPut(relayBasedFilter.relay) { mutableListOf() }.add(relayBasedFilter.filter)
|
||||
}
|
||||
|
||||
+8
-8
@@ -91,32 +91,32 @@ class Filter(
|
||||
|
||||
init {
|
||||
ids?.forEach {
|
||||
if (it.length != 64) Log.e("FilterError", "Invalid id length $it on ${toJson()}")
|
||||
if (it.length != 64) Log.e("FilterError") { "Invalid id length $it on ${toJson()}" }
|
||||
}
|
||||
authors?.forEach {
|
||||
if (it.length != 64) Log.e("FilterError", "Invalid author length $it on ${toJson()}")
|
||||
if (it.length != 64) Log.e("FilterError") { "Invalid author length $it on ${toJson()}" }
|
||||
}
|
||||
// tests common tags.
|
||||
if (tags != null) {
|
||||
tags["p"]?.forEach {
|
||||
if (it.length != 64) Log.e("FilterError", "Invalid p-tag length $it on ${toJson()}")
|
||||
if (it.length != 64) Log.e("FilterError") { "Invalid p-tag length $it on ${toJson()}" }
|
||||
}
|
||||
tags["e"]?.forEach {
|
||||
if (it.length != 64) Log.e("FilterError", "Invalid e-tag length $it on ${toJson()}")
|
||||
if (it.length != 64) Log.e("FilterError") { "Invalid e-tag length $it on ${toJson()}" }
|
||||
}
|
||||
tags["a"]?.forEach {
|
||||
if (Address.parse(it) == null) Log.e("FilterError", "Invalid a-tag $it on ${toJson()}")
|
||||
if (Address.parse(it) == null) Log.e("FilterError") { "Invalid a-tag $it on ${toJson()}" }
|
||||
}
|
||||
}
|
||||
if (tagsAll != null) {
|
||||
tagsAll["p"]?.forEach {
|
||||
if (it.length != 64) Log.e("FilterError", "Invalid p-tag length $it on ${toJson()}")
|
||||
if (it.length != 64) Log.e("FilterError") { "Invalid p-tag length $it on ${toJson()}" }
|
||||
}
|
||||
tagsAll["e"]?.forEach {
|
||||
if (it.length != 64) Log.e("FilterError", "Invalid e-tag length $it on ${toJson()}")
|
||||
if (it.length != 64) Log.e("FilterError") { "Invalid e-tag length $it on ${toJson()}" }
|
||||
}
|
||||
tagsAll["a"]?.forEach {
|
||||
if (Address.parse(it) == null) Log.e("FilterError", "Invalid a-tag $it on ${toJson()}")
|
||||
if (Address.parse(it) == null) Log.e("FilterError") { "Invalid a-tag $it on ${toJson()}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -145,7 +145,7 @@ class RelayUrlNormalizer {
|
||||
|
||||
if (trimmed.contains("://")) {
|
||||
// some other scheme we cannot connect to.
|
||||
Log.w("RelayUrlNormalizer", "Rejected $url")
|
||||
Log.w("RelayUrlNormalizer") { "Rejected $url" }
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -178,14 +178,14 @@ class RelayUrlNormalizer {
|
||||
normalizedUrls.put(url, NormalizationResult.Success(normalized))
|
||||
normalized
|
||||
} else {
|
||||
Log.w("NormalizedRelayUrl", "Rejected $url")
|
||||
Log.w("NormalizedRelayUrl") { "Rejected $url" }
|
||||
normalizedUrls.put(url, NormalizationResult.Error)
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
normalizedUrls.put(url, NormalizationResult.Error)
|
||||
Log.w("NormalizedRelayUrl", "Rejected $url")
|
||||
Log.w("NormalizedRelayUrl") { "Rejected $url" }
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ class RelaySession(
|
||||
try {
|
||||
onSend(OptimizedJsonMapper.toJson(message))
|
||||
} catch (e: Exception) {
|
||||
Log.w("ClientSession", "Failed to send to ${e.message}")
|
||||
Log.w("ClientSession") { "Failed to send to ${e.message}" }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -156,7 +156,7 @@ class OpenTimestamps(
|
||||
): Timestamp {
|
||||
val responses =
|
||||
mapNotNullAsync(calendarUrls) { calendarUrl ->
|
||||
Log.i("OpenTimestamps", "Submitting to remote calendar $calendarUrl")
|
||||
Log.i("OpenTimestamps") { "Submitting to remote calendar $calendarUrl" }
|
||||
calendar.submit(calendarUrl, timestamp.digest)
|
||||
}
|
||||
|
||||
@@ -361,7 +361,7 @@ class OpenTimestamps(
|
||||
val attsFromRemote: MutableSet<TimeAttestation> = upgradedStamp.getAttestations()
|
||||
|
||||
if (attsFromRemote.isNotEmpty()) {
|
||||
Log.i("OpenTimestamps", "Got 1 attestation(s) from $calendarUrl")
|
||||
Log.i("OpenTimestamps") { "Got 1 attestation(s) from $calendarUrl" }
|
||||
}
|
||||
|
||||
// Set difference from remote attestations & existing attestations
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ abstract class OpUnary : Op() {
|
||||
}
|
||||
|
||||
else -> {
|
||||
Log.e("OpenTimestamp", "Unknown operation tag: $tag")
|
||||
Log.e("OpenTimestamp") { "Unknown operation tag: $tag" }
|
||||
|
||||
null // TODO: Is this OK? Won't it blow up later? Better to throw?
|
||||
}
|
||||
|
||||
+1
-1
@@ -62,7 +62,7 @@ class EncryptedInfo(
|
||||
nonce = Base64.decode(parts[1]),
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.w("NIP04", "Unable to Parse encrypted payload: $payload")
|
||||
Log.w("NIP04") { "Unable to Parse encrypted payload: $payload" }
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ fun ATag.Companion.parseAtag(
|
||||
|
||||
ATag(parts[0].toInt(), parts[1], parts[2], relayHint)
|
||||
} catch (t: Throwable) {
|
||||
Log.w("ATag", "Error parsing A Tag: $atag: ${t.message}")
|
||||
Log.w("ATag") { "Error parsing A Tag: $atag: ${t.message}" }
|
||||
null
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ object Nip19Parser {
|
||||
|
||||
return type!! + key
|
||||
} catch (e: Throwable) {
|
||||
Log.e("NIP19 Parser", "Issue trying to Decode NIP19 $uri: ${e.message}")
|
||||
Log.e("NIP19 Parser") { "Issue trying to Decode NIP19 $uri: ${e.message}" }
|
||||
}
|
||||
|
||||
return null
|
||||
@@ -98,7 +98,7 @@ object Nip19Parser {
|
||||
|
||||
return parseComponents(type, key, additionalChars?.ifEmpty { null })
|
||||
} catch (e: Throwable) {
|
||||
Log.e("NIP19 Parser", "Issue trying to Decode NIP19 $uri: ${e.message}")
|
||||
Log.e("NIP19 Parser") { "Issue trying to Decode NIP19 $uri: ${e.message}" }
|
||||
}
|
||||
|
||||
return null
|
||||
@@ -127,7 +127,7 @@ object Nip19Parser {
|
||||
ParseReturn(it, nip19, additionalChars)
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
Log.w("NIP19 Parser", "Issue trying to Decode NIP19 $key: ${e.message}")
|
||||
Log.w("NIP19 Parser") { "Issue trying to Decode NIP19 $key: ${e.message}" }
|
||||
null
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ data class NAddress(
|
||||
return parse(key.bechToBytes())
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
Log.w("NAddress", "Issue trying to Decode NIP19 $this: ${e.message}")
|
||||
Log.w("NAddress") { "Issue trying to Decode NIP19 $this: ${e.message}" }
|
||||
}
|
||||
|
||||
return null
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ class ChannelCreateEvent(
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Log.w("ChannelCreateEvent", "Failure to parse ${this.toJson()}")
|
||||
Log.w("ChannelCreateEvent") { "Failure to parse ${this.toJson()}" }
|
||||
ChannelDataNorm()
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -74,7 +74,7 @@ class ChannelMetadataEvent(
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Log.w("ChannelCreateEvent", "Failure to parse ${this.toJson()}")
|
||||
Log.w("ChannelCreateEvent") { "Failure to parse ${this.toJson()}" }
|
||||
ChannelDataNorm()
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ class DraftWrapEvent(
|
||||
fromJson(json)
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Log.w("DraftEvent", "Unable to parse inner event of a draft: $json")
|
||||
Log.w("DraftEvent") { "Unable to parse inner event of a draft: $json" }
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -43,7 +43,7 @@ class PrivateTagsInContent {
|
||||
decode(json)
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
Log.w("DraftEvent", "Unable to parse inner event of a draft: $json")
|
||||
Log.w("DraftEvent") { "Unable to parse inner event of a draft: $json" }
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ enum class ReportType(
|
||||
"スパム \uD83D\uDCE3" -> SPAM
|
||||
"Pourriel \uD83D\uDCE3" -> SPAM
|
||||
"violence" -> VIOLENCE
|
||||
else -> Log.w("ReportedEventTag", "Report type not supported: `$code` ${tag.joinToString(", ")}").let { OTHER }
|
||||
else -> Log.w("ReportedEventTag") { "Report type not supported: `$code` ${tag.joinToString(", ")}" }.let { OTHER }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -61,7 +61,7 @@ class NIP90ContentDiscoveryResponseEvent(
|
||||
}
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
Log.w("NIP90ContentDiscoveryResponseEvent", "Error parsing the JSON ${e.message}")
|
||||
Log.w("NIP90ContentDiscoveryResponseEvent") { "Error parsing the JSON ${e.message}" }
|
||||
}
|
||||
|
||||
return events ?: listOf()
|
||||
|
||||
+2
-2
@@ -62,7 +62,7 @@ class LargeDBTests {
|
||||
try {
|
||||
db.insert(event)
|
||||
} catch (e: SQLiteException) {
|
||||
Log.w("LargeDBTests", "Error inserting event: ${e.message} for event: ${event.toJson()}")
|
||||
Log.w("LargeDBTests") { "Error inserting event: ${e.message} for event: ${event.toJson()}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,7 @@ class LargeDBTests {
|
||||
try {
|
||||
db.insert(event)
|
||||
} catch (e: SQLiteException) {
|
||||
Log.w("LargeDBTests", "Error inserting event: ${e.message} for event: ${event.toJson()}")
|
||||
Log.w("LargeDBTests") { "Error inserting event: ${e.message} for event: ${event.toJson()}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,56 +24,44 @@ import java.time.LocalTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
actual object PlatformLog {
|
||||
// Define a formatter for the desired output format (e.g., HH:mm:ss)
|
||||
val formatter: DateTimeFormatter? = DateTimeFormatter.ofPattern("HH:mm:ss.SSS")
|
||||
private val formatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS")
|
||||
|
||||
fun time() = LocalTime.now().format(formatter)
|
||||
private fun time() = LocalTime.now().format(formatter)
|
||||
|
||||
private fun log(
|
||||
level: String,
|
||||
tag: String,
|
||||
message: String,
|
||||
throwable: Throwable?,
|
||||
) {
|
||||
if (throwable != null) {
|
||||
println("${time()} $level: [$tag] $message. Throwable: ${throwable.message}")
|
||||
} else {
|
||||
println("${time()} $level: [$tag] $message")
|
||||
}
|
||||
}
|
||||
|
||||
actual fun w(
|
||||
tag: String,
|
||||
message: String,
|
||||
throwable: Throwable?,
|
||||
) {
|
||||
if (throwable != null) {
|
||||
println("${time()} WARN : [$tag] $message. Throwable: ${throwable.message}")
|
||||
} else {
|
||||
println("${time()} WARN : [$tag] $message")
|
||||
}
|
||||
}
|
||||
) = log("WARN ", tag, message, throwable)
|
||||
|
||||
actual fun e(
|
||||
tag: String,
|
||||
message: String,
|
||||
throwable: Throwable?,
|
||||
) {
|
||||
if (throwable != null) {
|
||||
println("${time()} ERROR: [$tag] $message. Throwable: ${throwable.message}")
|
||||
} else {
|
||||
println("${time()} ERROR: [$tag] $message")
|
||||
}
|
||||
}
|
||||
) = log("ERROR", tag, message, throwable)
|
||||
|
||||
actual fun d(
|
||||
tag: String,
|
||||
message: String,
|
||||
throwable: Throwable?,
|
||||
) {
|
||||
if (throwable != null) {
|
||||
println("${time()} DEBUG: [$tag] $message. Throwable: ${throwable.message}")
|
||||
} else {
|
||||
println("${time()} DEBUG: [$tag] $message")
|
||||
}
|
||||
}
|
||||
) = log("DEBUG", tag, message, throwable)
|
||||
|
||||
actual fun i(
|
||||
tag: String,
|
||||
message: String,
|
||||
throwable: Throwable?,
|
||||
) {
|
||||
if (throwable != null) {
|
||||
println("${time()} INFO : [$tag] $message. Throwable: ${throwable.message}")
|
||||
} else {
|
||||
println("${time()} INFO : [$tag] $message")
|
||||
}
|
||||
}
|
||||
) = log("INFO ", tag, message, throwable)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user