From 9d3974c32b7a904d2b7b16e3e0e7ded19e7fd633 Mon Sep 17 00:00:00 2001 From: Noor Dawod Date: Mon, 27 May 2024 14:37:32 +0200 Subject: [PATCH] Make sure that base folder exists. --- .../logging/facility/FileLoggerFacility.kt | 27 ++++++++++++++----- .../logging/facility/JsonLoggerFacility.kt | 26 +++++++++++++----- .../platform/PlatformFileInputOutputImpl.kt | 8 ++++-- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/commonMain/kotlin/com/airthings/lib/logging/facility/FileLoggerFacility.kt b/src/commonMain/kotlin/com/airthings/lib/logging/facility/FileLoggerFacility.kt index 8f93aaa..053a67c 100644 --- a/src/commonMain/kotlin/com/airthings/lib/logging/facility/FileLoggerFacility.kt +++ b/src/commonMain/kotlin/com/airthings/lib/logging/facility/FileLoggerFacility.kt @@ -133,11 +133,7 @@ class FileLoggerFacility( init { coroutineScope.launch { - // Please note: The call to `io.mkdirs()` returns true if the directory exists, which may be - // different from the platform's implementation. - if (!io.mkdirs(baseFolder)) { - throw IllegalArgumentException("Base log folder is invalid: $baseFolder") - } + ensureBaseFolder() } } @@ -200,7 +196,10 @@ class FileLoggerFacility( * * Note: The returned list contains absolute (canonical) paths to the files. */ - suspend fun files(): Collection = io.of(baseFolder) + suspend fun files(): Collection { + ensureBaseFolder() + return io.of(baseFolder) + } /** * Scans the [baseFolder] and returns the list of log files residing in it that @@ -210,7 +209,10 @@ class FileLoggerFacility( * * @param date The date from which log files should be considered. */ - suspend fun files(date: LogDate): Collection = io.of(baseFolder, date) + suspend fun files(date: LogDate): Collection { + ensureBaseFolder() + return io.of(baseFolder, date) + } /** * Deletes a file residing in [baseFolder]. @@ -218,6 +220,7 @@ class FileLoggerFacility( * @param name The file name to delete. */ suspend fun delete(name: String) { + ensureBaseFolder() io.delete("$baseFolder${io.pathSeparator}$name") } @@ -230,6 +233,14 @@ class FileLoggerFacility( io.delete(path) } + private suspend fun ensureBaseFolder() { + // Please note: The call to `io.mkdirs()` returns true if the directory exists, which may be + // different from the platform's implementation. + if (!io.mkdirs(baseFolder)) { + throw IllegalArgumentException("Base log folder is invalid: $baseFolder") + } + } + private fun withLogLevel( level: LogLevel, action: suspend (String) -> Unit, @@ -239,6 +250,8 @@ class FileLoggerFacility( } coroutineScope.launch { + ensureBaseFolder() + val logFile = "$baseFolder${io.pathSeparator}${dateStamp(null)}.log" val currentLogFileLocked = currentLogFile.value diff --git a/src/commonMain/kotlin/com/airthings/lib/logging/facility/JsonLoggerFacility.kt b/src/commonMain/kotlin/com/airthings/lib/logging/facility/JsonLoggerFacility.kt index 8d702d8..61ee584 100644 --- a/src/commonMain/kotlin/com/airthings/lib/logging/facility/JsonLoggerFacility.kt +++ b/src/commonMain/kotlin/com/airthings/lib/logging/facility/JsonLoggerFacility.kt @@ -133,11 +133,7 @@ class JsonLoggerFacility( init { coroutineScope.launch { - // Please note: The call to `io.mkdirs()` returns true if the directory exists, which may be - // different from the platform's implementation. - if (!io.mkdirs(baseFolder)) { - throw IllegalArgumentException("Base log folder is invalid: $baseFolder") - } + ensureBaseFolder() } } @@ -221,14 +217,28 @@ class JsonLoggerFacility( /** * Scans the [baseFolder] and returns the list of log files residing in it. */ - suspend fun files(): Collection = io.of(baseFolder) + suspend fun files(): Collection { + ensureBaseFolder() + return io.of(baseFolder) + } /** * Scans the [baseFolder] and returns the list of log files residing in it that were created after a certain date. * * @param date The date from which log files should be considered. */ - suspend fun files(date: LogDate): Collection = io.of(baseFolder, date) + suspend fun files(date: LogDate): Collection { + ensureBaseFolder() + return io.of(baseFolder, date) + } + + private suspend fun ensureBaseFolder() { + // Please note: The call to `io.mkdirs()` returns true if the directory exists, which may be + // different from the platform's implementation. + if (!io.mkdirs(baseFolder)) { + throw IllegalArgumentException("Base log folder is invalid: $baseFolder") + } + } private fun withLogLevel( level: LogLevel, @@ -239,6 +249,8 @@ class JsonLoggerFacility( } coroutineScope.launch { + ensureBaseFolder() + val logFile = "$baseFolder${io.pathSeparator}${dateStamp(null)}.json" val currentLogFileLocked = currentLogFile.value diff --git a/src/jvmMain/kotlin/com/airthings/lib/logging/platform/PlatformFileInputOutputImpl.kt b/src/jvmMain/kotlin/com/airthings/lib/logging/platform/PlatformFileInputOutputImpl.kt index ca72cf7..8074044 100644 --- a/src/jvmMain/kotlin/com/airthings/lib/logging/platform/PlatformFileInputOutputImpl.kt +++ b/src/jvmMain/kotlin/com/airthings/lib/logging/platform/PlatformFileInputOutputImpl.kt @@ -63,8 +63,12 @@ internal actual class PlatformFileInputOutputImpl : PlatformFileInputOutput { } override suspend fun ensure(path: String) { - synchronized(writeLock) { - File(path).createNewFile() + val parentPath = File(path).parentFile.canonicalPath + + if (mkdirs(parentPath)) { + synchronized(writeLock) { + File(path).createNewFile() + } } }