-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
synchronized
extension function for Loggers (#40)
- Loading branch information
1 parent
c3c0401
commit fd1b470
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
khronicle-core/src/commonMain/kotlin/SynchronizedLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.juul.khronicle | ||
|
||
import kotlinx.atomicfu.locks.reentrantLock | ||
import kotlinx.atomicfu.locks.withLock | ||
|
||
public fun Logger.synchronized(): Logger = SynchronizedLogger(this) | ||
|
||
private class SynchronizedLogger( | ||
private val inner: Logger, | ||
) : Logger { | ||
|
||
private val guard = reentrantLock() | ||
|
||
override fun verbose(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) { | ||
guard.withLock { inner.verbose(tag, message, metadata, throwable) } | ||
} | ||
|
||
override fun debug(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) { | ||
guard.withLock { inner.debug(tag, message, metadata, throwable) } | ||
} | ||
|
||
override fun info(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) { | ||
guard.withLock { inner.info(tag, message, metadata, throwable) } | ||
} | ||
|
||
override fun warn(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) { | ||
guard.withLock { inner.warn(tag, message, metadata, throwable) } | ||
} | ||
|
||
override fun error(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) { | ||
guard.withLock { inner.error(tag, message, metadata, throwable) } | ||
} | ||
|
||
override fun assert(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) { | ||
guard.withLock { inner.assert(tag, message, metadata, throwable) } | ||
} | ||
} |