diff --git a/app/src/main/java/app/klottr/MainActivity.kt b/app/src/main/java/app/klottr/MainActivity.kt index bb7a625..441add9 100644 --- a/app/src/main/java/app/klottr/MainActivity.kt +++ b/app/src/main/java/app/klottr/MainActivity.kt @@ -149,6 +149,7 @@ import app.klottr.util.FoundStore.LAYER_DM import app.klottr.util.FoundStore.LAYER_KLOTTER import app.klottr.util.FoundStore.LAYER_WIKI import app.klottr.util.ImageBitmapCache +import app.klottr.util.KLog import app.klottr.util.LastLocationStore import app.klottr.util.NoteDetailCache import app.klottr.util.OutboxStore @@ -630,8 +631,8 @@ private fun KlottrApp( lastKnownLat = gp.lat // Nytt lastKnownLon = gp.lon // Nytt - app.klottr.util.AppLog.d { "Närliggande-debug: toggles klotter=$showKlotter wiki=$showWiki dm=$showDm forn=$showFornsok"} - app.klottr.util.AppLog.d { "Närliggande-debug: loc=${lastKnownLat},${lastKnownLon}"} + KLog.d { "Närliggande-debug: toggles klotter=$showKlotter wiki=$showWiki dm=$showDm forn=$showFornsok"} + KLog.d { "Närliggande-debug: loc=${lastKnownLat},${lastKnownLon}"} // Re-arm: markera alla tidigare fynd som "borta" om vi nu står >200 m från dem // Hämta alla lager via delad KMP-funktion (DI med våra befintliga klientanrop) @@ -678,7 +679,7 @@ private fun KlottrApp( ) // [VALFRITT] Logg – nu summerar vi entries + per-lager - app.klottr.util.AppLog.d { + KLog.d { "Närliggande-resultat: entries=${nearbyEntries.size}, dm=${nearbyDm.size}, forn=${nearbyFornsok.size}" } @@ -1974,7 +1975,7 @@ private fun KlotterDialog( // Försök hämta detalj/bild om posten har besökts tidigare ELLER vi är inom radien val canAttempt = wasFoundBefore || withinRadius - app.klottr.util.AppLog.d { "KlotterDialog canAttempt=$canAttempt withinRadius=$withinRadius wasFoundBefore=$wasFoundBefore" } + KLog.d { "KlotterDialog canAttempt=$canAttempt withinRadius=$withinRadius wasFoundBefore=$wasFoundBefore" } if (canAttempt) { // get_note.php ska få: Besökt tidigare => NOTISENS coords, annars besökarens (fallback notisens) @@ -1987,7 +1988,7 @@ private fun KlotterDialog( fetchNoteDetail(id.toLong(), reqLat, reqLon) } catch (_: Throwable) { null } } - app.klottr.util.AppLog.d { "detail fetch id=$id ok=${detail != null}"} + KLog.d { "detail fetch id=$id ok=${detail != null}"} if (detail != null) { // Sätt titel + brödtext från servern @@ -1996,7 +1997,7 @@ private fun KlotterDialog( authorName = detail.username ownerUserId = detail.userId - app.klottr.util.AppLog.d { + KLog.d { "KD: ownerUserId=$ownerUserId currentUserId=$currentUserId showDelete=${currentUserId != null && ownerUserId != null && currentUserId == ownerUserId}" } @@ -2024,7 +2025,7 @@ private fun KlotterDialog( } // 2) Tillbaka på huvudtråden: uppdatera UI-tillstånd här - app.klottr.util.AppLog.d { "img POST ok=${bmp != null} id=${imgMeta.id} note=${detail.id}"} + KLog.d { "img POST ok=${bmp != null} id=${imgMeta.id} note=${detail.id}"} if (bmp != null) { imgBitmap = bmp if (imgMeta.id > 0) { diff --git a/app/src/main/java/app/klottr/net/Api.kt b/app/src/main/java/app/klottr/net/Api.kt index 6c90cc0..241cd26 100644 --- a/app/src/main/java/app/klottr/net/Api.kt +++ b/app/src/main/java/app/klottr/net/Api.kt @@ -5,6 +5,7 @@ import android.net.Uri import app.klottr.BuildConfig import app.klottr.net.KlottrApiShared import app.klottr.util.Auth +import app.klottr.util.KLog import java.io.File /** @@ -108,12 +109,12 @@ suspend fun uploadImageFromUri( lon = lon ) } catch (t: Throwable) { - app.klottr.util.AppLog.d { "uploadImage exception: ${t.message}" } + KLog.d { "uploadImage exception: ${t.message}" } Api.lastUploadError = "Uppladdningen misslyckades." return null } - app.klottr.util.AppLog.d { "uploadImage code=200 ok=${res.ok} imageId=${res.imageId} width=${res.width} height=${res.height}" } + KLog.d { "uploadImage code=200 ok=${res.ok} imageId=${res.imageId} width=${res.width} height=${res.height}" } if (!res.ok || res.imageId == null) { Api.lastUploadError = res.errorMessage ?: "Uppladdningen misslyckades." @@ -166,12 +167,12 @@ suspend fun uploadImageFromFile( lon = lon ) } catch (t: Throwable) { - app.klottr.util.AppLog.d { "uploadImage(file) exception: ${t.message}" } + KLog.d { "uploadImage(file) exception: ${t.message}" } Api.lastUploadError = "Uppladdningen misslyckades." return null } - app.klottr.util.AppLog.d { "uploadImage(file) ok=${res.ok} imageId=${res.imageId}" } + KLog.d { "uploadImage(file) ok=${res.ok} imageId=${res.imageId}" } if (!res.ok || res.imageId == null) { Api.lastUploadError = res.errorMessage ?: "Uppladdningen misslyckades." @@ -262,12 +263,12 @@ suspend fun fetchNoteDetail(id: Long, lat: Double, lon: Double): NoteDetailShare lon = lon ) } catch (t: Throwable) { - app.klottr.util.AppLog.d { "get_note exception: ${t.message}" } + KLog.d { "get_note exception: ${t.message}" } null } if (sh == null) { - app.klottr.util.AppLog.d { "get_note FAIL (null)" } + KLog.d { "get_note FAIL (null)" } return null } @@ -301,7 +302,7 @@ suspend fun checkUsernameRemote(input: String): UsernameCheckResp? { username = disp ) } catch (t: Throwable) { - app.klottr.util.AppLog.d { "username_check: EXC ${t.message}" } + KLog.d { "username_check: EXC ${t.message}" } null } return sh @@ -344,7 +345,7 @@ suspend fun fetchImageStreamPost( lon = lon ) } catch (t: Throwable) { - app.klottr.util.AppLog.d { "get_image exception: ${t.message}" } + KLog.d { "get_image exception: ${t.message}" } null } } diff --git a/app/src/main/java/app/klottr/net/Fornsok.kt b/app/src/main/java/app/klottr/net/Fornsok.kt index 78583e4..1736b57 100644 --- a/app/src/main/java/app/klottr/net/Fornsok.kt +++ b/app/src/main/java/app/klottr/net/Fornsok.kt @@ -1,6 +1,6 @@ package app.klottr.net -import android.util.Log +import app.klottr.util.KLog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.json.JSONArray @@ -35,7 +35,7 @@ object Fornsok { suspend fun getByEntityUri(entityUri: String): FornsokItem? = withContext(Dispatchers.IO) { val cql = """entityUri="$entityUri"""" val url = buildUrl(cql, startRecord = 1, usePresentation = true) - app.klottr.util.AppLog.d(TAG) { "Detalj: GET $url"} + KLog.d(TAG) { "Detalj: GET $url"} val root = httpGetJson(url) ?: return@withContext null val result = root.optJSONObject("result") ?: return@withContext null @@ -121,7 +121,7 @@ object Fornsok { return try { val code = conn.responseCode val ctype = conn.getHeaderField("Content-Type") - app.klottr.util.AppLog.d(TAG) { "HTTP $code Content-Type=$ctype"} + KLog.d(TAG) { "HTTP $code Content-Type=$ctype"} if (code !in 200..299) return null val body = conn.inputStream.use { it.readBytes().toString(Charsets.UTF_8) } if (body.isBlank()) return null @@ -141,13 +141,13 @@ object Fornsok { while (page <= maxPages) { val url = buildUrl(cql, start, usePresentation) - app.klottr.util.AppLog.d(TAG) { "Sida $page: GET $url"} + KLog.d(TAG) { "Sida $page: GET $url"} val root = httpGetJson(url) ?: break val result = root.optJSONObject("result") ?: break if (totalHitsFromServer < 0) { totalHitsFromServer = result.optInt("totalHits", -1) - app.klottr.util.AppLog.d(TAG) { "totalHits (server): $totalHitsFromServer"} + KLog.d(TAG) { "totalHits (server): $totalHitsFromServer"} } val recordsNode = result.opt("records") @@ -164,7 +164,7 @@ object Fornsok { } if (recordArr.length() == 0) { - app.klottr.util.AppLog.d(TAG) { "Ingen records.record på denna sida."} + KLog.d(TAG) { "Ingen records.record på denna sida."} break } @@ -242,7 +242,7 @@ object Fornsok { added++ } } - app.klottr.util.AppLog.d(TAG) {"Sida: genomgång visited=$visited, nonKmrFmi=$nonKmrFmi, noCoords=$noCoords, tillagda=$added, ack=${out.size}" } + KLog.d(TAG) {"Sida: genomgång visited=$visited, nonKmrFmi=$nonKmrFmi, noCoords=$noCoords, tillagda=$added, ack=${out.size}" } // Fler sidor? if (totalHitsFromServer > 0) { diff --git a/app/src/main/java/app/klottr/net/Geocache.kt b/app/src/main/java/app/klottr/net/Geocache.kt index b7b6636..c566df7 100644 --- a/app/src/main/java/app/klottr/net/Geocache.kt +++ b/app/src/main/java/app/klottr/net/Geocache.kt @@ -1,5 +1,6 @@ package app.klottr.net +import app.klottr.util.KLog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.util.concurrent.ConcurrentHashMap @@ -58,7 +59,7 @@ object Geocache { etag = prev?.etag ) if (first.code !in listOf(200, 304)) { - app.klottr.util.AppLog.d {"Geocache: first fetch failed code=${first.code} → fallback to cache size=${prev?.items?.size ?: 0}"} + KLog.d {"Geocache: first fetch failed code=${first.code} → fallback to cache size=${prev?.items?.size ?: 0}"} } // Hantera 304/not-modified och nätfel (kod 599 i fetchNearby) @@ -134,7 +135,7 @@ object Geocache { limit = 200 ) } catch (_: Throwable) { - app.klottr.util.AppLog.d { "nearby.php exception → code=599 lat=$lat lon=$lon r=$radiusMeters since=$sinceMs"} + KLog.d { "nearby.php exception → code=599 lat=$lat lon=$lon r=$radiusMeters since=$sinceMs"} NearbyResult(code = 599, items = emptyList(), etag = etag) } diff --git a/shared/src/androidMain/kotlin/app/klottr/util/KLog.android.kt b/shared/src/androidMain/kotlin/app/klottr/util/KLog.android.kt new file mode 100644 index 0000000..7c99928 --- /dev/null +++ b/shared/src/androidMain/kotlin/app/klottr/util/KLog.android.kt @@ -0,0 +1,36 @@ +package app.klottr.util + +import android.util.Log // Androids logg-API + +// Android-implementation av KLog: loggar till Logcat. +// Default-tag "Klottr" om ingen tag anges. Loggning kan stängas av. +actual object KLog { + @Volatile + private var enabled: Boolean = true // På som default; kan stängas via setEnabled + + actual fun setEnabled(enabled: Boolean) { + this.enabled = enabled // Uppdatera flagga + } + + private fun tagOrDefault(tag: String?): String = tag ?: "Klottr" // Standardtag + + actual fun d(tag: String?, msg: () -> String) { + if (!enabled) return // Avbryt om avstängd + Log.d(tagOrDefault(tag), msg()) // Skriv debug + } + + actual fun w(tag: String?, msg: () -> String) { + if (!enabled) return // Avbryt om avstängd + Log.w(tagOrDefault(tag), msg()) // Skriv varning + } + + actual fun e(tag: String?, t: Throwable?, msg: () -> String) { + if (!enabled) return // Avbryt om avstängd + val tg = tagOrDefault(tag) // Välj tag + if (t != null) { + Log.e(tg, msg(), t) // Fel med Throwable + } else { + Log.e(tg, msg()) // Fel utan Throwable + } + } +} diff --git a/shared/src/commonMain/kotlin/app/klottr/util/KLog.kt b/shared/src/commonMain/kotlin/app/klottr/util/KLog.kt new file mode 100644 index 0000000..8d51920 --- /dev/null +++ b/shared/src/commonMain/kotlin/app/klottr/util/KLog.kt @@ -0,0 +1,10 @@ +package app.klottr.util + +// Expect/Actual: Gemensam logger-API för KMP. +// Användning: KLog.d { "text" }, KLog.e(t = ex) { "fel" }, KLog.setEnabled(true/false) +expect object KLog { + fun d(tag: String? = null, msg: () -> String) // Debug-utskrift med lazy lambda + fun w(tag: String? = null, msg: () -> String) // Varning + fun e(tag: String? = null, t: Throwable? = null, msg: () -> String) // Fel med ev. Throwable + fun setEnabled(enabled: Boolean) // Slå på/av loggning +}