Skip to content

Commit

Permalink
[commonize] open-ani#496 Commonize :utils:slf4j-kt (renamed to :utils…
Browse files Browse the repository at this point in the history
…:logging)
  • Loading branch information
Him188 committed Jul 27, 2024
1 parent 9d83a21 commit bc35e9f
Show file tree
Hide file tree
Showing 23 changed files with 287 additions and 217 deletions.
4 changes: 2 additions & 2 deletions app/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ kotlin {
api(projects.torrent.anitorrent)

api(projects.client)
api(projects.utils.slf4jKt)
api(projects.utils.logging)
api(projects.utils.coroutines)
api(projects.utils.io)
api(projects.danmaku.ani.protocol)
Expand Down Expand Up @@ -184,7 +184,7 @@ kotlin {
}
api(compose.material3)
api("org.jetbrains.compose.ui:ui-graphics-desktop:${libs.versions.compose.multiplatform.get()}")
api(projects.utils.slf4jKt)
api(projects.utils.logging)
api(libs.kotlinx.coroutines.swing)
implementation(libs.vlcj)
implementation(libs.jna) // required and don't change version, otherwise vlcj might crash the VM
Expand Down
2 changes: 1 addition & 1 deletion danmaku/ani/server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

dependencies {
api(projects.utils.slf4jKt)
api(projects.utils.logging)

api(libs.kotlinx.coroutines.core)
api(libs.kotlinx.serialization.json)
Expand Down
2 changes: 1 addition & 1 deletion danmaku/api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
api(libs.kotlinx.serialization.core)

api(projects.utils.ktorClient)
api(projects.utils.slf4jKt)
api(projects.utils.logging)
api(projects.dataSources.api)

testImplementation(libs.kotlinx.coroutines.test)
Expand Down
2 changes: 1 addition & 1 deletion danmaku/dandanplay/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

dependencies {
api(projects.danmaku.api)
api(projects.utils.slf4jKt)
api(projects.utils.logging)

api(libs.kotlinx.coroutines.core)
api(libs.kotlinx.serialization.json)
Expand Down
4 changes: 2 additions & 2 deletions danmaku/ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ kotlin {
api(compose.material3)
api(compose.runtime)

api(projects.utils.slf4jKt)
api(projects.utils.logging)
api(projects.utils.coroutines)

// Ktor
Expand Down Expand Up @@ -86,7 +86,7 @@ kotlin {
api(compose.desktop.currentOs) {
exclude(compose.material) // We use material3
}
api(projects.utils.slf4jKt)
api(projects.utils.logging)
api(libs.kotlinx.coroutines.swing)
runtimeOnly(libs.kotlinx.coroutines.debug)

Expand Down
2 changes: 1 addition & 1 deletion data-sources/api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {
api(libs.ktor.client.logging)
api(libs.ktor.client.auth)
api(libs.jsoup)
implementation(projects.utils.slf4jKt)
implementation(projects.utils.logging)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(projects.utils.testing)

Expand Down
2 changes: 1 addition & 1 deletion data-sources/bt/acg.rip/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {
implementation(libs.ktor.client.logging)
implementation(libs.jsoup)
implementation(libs.slf4j.api)
implementation(projects.utils.slf4jKt)
implementation(projects.utils.logging)
}

tasks.withType<Jar> {
Expand Down
2 changes: 1 addition & 1 deletion data-sources/bt/dmhy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies {
implementation(libs.jsoup)
implementation(libs.slf4j.api)

implementation(projects.utils.slf4jKt)
implementation(projects.utils.logging)
}

tasks.withType<Jar> {
Expand Down
2 changes: 1 addition & 1 deletion data-sources/bt/mikan/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {
implementation(libs.kotlinx.serialization.json)
implementation(libs.jsoup)
implementation(libs.slf4j.api)
implementation(projects.utils.slf4jKt)
implementation(projects.utils.logging)
}

tasks.withType<Jar> {
Expand Down
2 changes: 1 addition & 1 deletion data-sources/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {
api(projects.utils.ktorClient)
// api(libs.kotlinx.io.core)
api(projects.utils.io)
implementation(projects.utils.slf4jKt)
implementation(projects.utils.logging)

implementation(compose.runtime) // required by the compose compiler
}
4 changes: 2 additions & 2 deletions data-sources/ikaros/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ idea {
dependencies {
api(projects.dataSources.api)
implementation(projects.utils.serialization)
implementation(projects.utils.slf4jKt)
implementation(projects.utils.logging)

api(libs.kotlinx.coroutines.core)
api(libs.ktor.client.core)
Expand All @@ -32,7 +32,7 @@ dependencies {
implementation(libs.kotlinx.serialization.json)
implementation(libs.jsoup)
implementation(libs.slf4j.api)
implementation(projects.utils.slf4jKt)
implementation(projects.utils.logging)

implementation("com.squareup.moshi:moshi-kotlin:1.14.0")
implementation("com.squareup.moshi:moshi-adapters:1.14.0")
Expand Down
2 changes: 1 addition & 1 deletion data-sources/jellyfin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
dependencies {
api(projects.dataSources.api)
implementation(projects.utils.serialization)
implementation(projects.utils.slf4jKt)
implementation(projects.utils.logging)

api(libs.kotlinx.coroutines.core)
implementation(libs.ktor.client.logging)
Expand Down
2 changes: 1 addition & 1 deletion data-sources/web/web-base/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
api(libs.jsoup)

api(projects.utils.ktorClient)
api(projects.utils.slf4jKt)
api(projects.utils.logging)

api(libs.ktor.client.content.negotiation)
api(libs.ktor.client.logging)
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ snakeyaml = "2.2"
sqlite = "2.5.0-alpha03"
constraintlayout-compose = "0.4.0"
antlr-kotlin = "1.0.0-RC4"
oshai-kotlin-logging = "7.0.0" # Only for native. On JVM we use slf4j directly.


compose-material3 = "1.2.1"
Expand All @@ -59,6 +60,7 @@ compose-multiplatfrom-gradle-plugin = { module = "org.jetbrains.compose:org.jetb
#compose-multiplatfrom-compiler-plugin = { module = "org.jetbrains.compose:org.jetbrains.compose.compiler", version.ref = "compose-multiplatform-compiler" }

# Kotlinx
oshai-kotlin-logging = { module = "io.github.oshai:kotlin-logging", version.ref = "oshai-kotlin-logging" }
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "datetime" }
kotlinx-io-core = { module = "org.jetbrains.kotlinx:kotlinx-io-core", version.ref = "kotlinx-io" }
kotlinx-io-bytestring = { module = "org.jetbrains.kotlinx:kotlinx-io-bytestring", version.ref = "kotlinx-io" }
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fun includeProject(projectPath: String, dir: String? = null) {

// Utilities shared by client and server (targeting JVM)
includeProject(":utils:platform") // 适配各个平台的基础 API
includeProject(":utils:slf4j-kt", "utils/slf4j-kt") // shared by client and server (targets JVM)
includeProject(":utils:logging") // shared by client and server (targets JVM)
includeProject(":utils:serialization", "utils/serialization")
includeProject(":utils:coroutines", "utils/coroutines")
includeProject(":utils:ktor-client", "utils/ktor-client")
Expand Down
2 changes: 1 addition & 1 deletion torrent/api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ plugins {

dependencies {
api(libs.kotlinx.coroutines.core)
implementation(projects.utils.slf4jKt)
implementation(projects.utils.logging)
implementation(projects.utils.coroutines)
api(projects.utils.io)

Expand Down
2 changes: 1 addition & 1 deletion torrent/impl/anitorrent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ kotlin {
//dependencies {
// api(libs.kotlinx.coroutines.core)
//// api(projects.torrent.api)
// implementation(projects.utils.slf4jKt)
// implementation(projects.utils.logging)
// implementation(projects.utils.coroutines)
// api(projects.utils.io)
//}
Expand Down
2 changes: 1 addition & 1 deletion utils/ktor-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ dependencies {
api(libs.ktor.client.core)
api(libs.ktor.client.content.negotiation)
api(libs.ktor.serialization.kotlinx.json)
api(projects.utils.slf4jKt)
api(projects.utils.logging)
}
19 changes: 16 additions & 3 deletions utils/slf4j-kt/build.gradle.kts → utils/logging/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,24 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi

plugins {
kotlin("jvm")
`flatten-source-sets`
kotlin("multiplatform")
`ani-mpp-lib-targets`
}

dependencies {
api(libs.slf4j.api)
jvmMainApi(libs.slf4j.api)
}

kotlin {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions.freeCompilerArgs.add("-Xexpect-actual-classes")

sourceSets.nativeMain {
dependencies {
implementation(libs.oshai.kotlin.logging)
}
}
}
68 changes: 68 additions & 0 deletions utils/logging/src/appleMain/kotlin/LoggerKt.native.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package me.him188.ani.utils.logging

import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlin.reflect.KClass

actual fun logger(name: String): Logger {
return IosLogger(KotlinLogging.logger(name))
}

actual fun logger(clazz: KClass<out Any>): Logger {
return IosLogger(KotlinLogging.logger(clazz.qualifiedName ?: clazz.simpleName ?: clazz.toString()))
}

actual interface Logger {
actual fun isTraceEnabled(): Boolean
actual fun trace(message: String?, throwable: Throwable?)
actual fun isDebugEnabled(): Boolean
actual fun debug(message: String?, throwable: Throwable?)
actual fun isInfoEnabled(): Boolean
actual fun info(message: String?, throwable: Throwable?)
actual fun isWarnEnabled(): Boolean
actual fun warn(message: String?, throwable: Throwable?)
actual fun isErrorEnabled(): Boolean
actual fun error(message: String?, throwable: Throwable?)
}

private class IosLogger(
private val delegate: KLogger,
) : Logger {
override fun isTraceEnabled(): Boolean = delegate.isTraceEnabled()

override fun trace(message: String?, throwable: Throwable?) {
return delegate.trace(throwable) { message }
}

override fun isDebugEnabled(): Boolean {
return delegate.isDebugEnabled()
}

override fun debug(message: String?, throwable: Throwable?) {
return delegate.debug(throwable) { message }
}

override fun isInfoEnabled(): Boolean {
return delegate.isInfoEnabled()
}

override fun info(message: String?, throwable: Throwable?) {
return delegate.info(throwable) { message }
}

override fun isWarnEnabled(): Boolean {
return delegate.isWarnEnabled()
}

override fun warn(message: String?, throwable: Throwable?) {
return delegate.warn(throwable) { message }
}

override fun isErrorEnabled(): Boolean {
return delegate.isErrorEnabled()
}

override fun error(message: String?, throwable: Throwable?) {
return delegate.error(throwable) { message }
}
}
Loading

0 comments on commit bc35e9f

Please sign in to comment.