Skip to content

Commit

Permalink
Make sure that base folder exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
airxnoor committed May 27, 2024
1 parent ca1fed1 commit 9d3974c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -200,7 +196,10 @@ class FileLoggerFacility(
*
* Note: The returned list contains absolute (canonical) paths to the files.
*/
suspend fun files(): Collection<String> = io.of(baseFolder)
suspend fun files(): Collection<String> {
ensureBaseFolder()
return io.of(baseFolder)
}

/**
* Scans the [baseFolder] and returns the list of log files residing in it that
Expand All @@ -210,14 +209,18 @@ class FileLoggerFacility(
*
* @param date The date from which log files should be considered.
*/
suspend fun files(date: LogDate): Collection<String> = io.of(baseFolder, date)
suspend fun files(date: LogDate): Collection<String> {
ensureBaseFolder()
return io.of(baseFolder, date)
}

/**
* Deletes a file residing in [baseFolder].
*
* @param name The file name to delete.
*/
suspend fun delete(name: String) {
ensureBaseFolder()
io.delete("$baseFolder${io.pathSeparator}$name")
}

Expand All @@ -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,
Expand All @@ -239,6 +250,8 @@ class FileLoggerFacility(
}

coroutineScope.launch {
ensureBaseFolder()

val logFile = "$baseFolder${io.pathSeparator}${dateStamp(null)}.log"
val currentLogFileLocked = currentLogFile.value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -221,14 +217,28 @@ class JsonLoggerFacility(
/**
* Scans the [baseFolder] and returns the list of log files residing in it.
*/
suspend fun files(): Collection<String> = io.of(baseFolder)
suspend fun files(): Collection<String> {
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<String> = io.of(baseFolder, date)
suspend fun files(date: LogDate): Collection<String> {
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,
Expand All @@ -239,6 +249,8 @@ class JsonLoggerFacility(
}

coroutineScope.launch {
ensureBaseFolder()

val logFile = "$baseFolder${io.pathSeparator}${dateStamp(null)}.json"
val currentLogFileLocked = currentLogFile.value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}

Expand Down

0 comments on commit 9d3974c

Please sign in to comment.