Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that base folder exists #10

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
jstarczewski marked this conversation as resolved.
Show resolved Hide resolved
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
Loading