Skip to content

Commit 023df4e

Browse files
committed
improve logger & update kotlin
1 parent 6f5feae commit 023df4e

File tree

8 files changed

+57
-40
lines changed

8 files changed

+57
-40
lines changed

build.gradle.kts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
kotlin("jvm") version "1.5.31"
2+
kotlin("jvm") version "1.6.0"
33
id("com.github.johnrengelman.shadow") version "7.1.0"
44
`maven-publish`
55
}
@@ -13,13 +13,17 @@ repositories {
1313

1414
dependencies {
1515
val ktorVersion: String by project
16+
val kotlinVersion: String by project
1617

17-
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.5.31")
18-
implementation("org.jetbrains.kotlin:kotlin-reflect:1.5.31")
18+
implementation(kotlin("stdlib-jdk8", kotlinVersion))
19+
implementation(kotlin("reflect", kotlinVersion))
1920

2021
implementation("io.ktor:ktor-client-core:$ktorVersion")
2122
implementation("io.ktor:ktor-client-apache:$ktorVersion")
2223
implementation("io.ktor:ktor-client-gson:$ktorVersion")
24+
25+
implementation("io.github.microutils:kotlin-logging-jvm:2.1.0")
26+
implementation("ch.qos.logback:logback-classic:1.2.7")
2327
}
2428

2529
tasks {

gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
kotlin.code.style=official
22

3-
ktorVersion=1.6.5
3+
ktorVersion=1.6.5
4+
kotlinVersion=1.6.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

src/main/kotlin/org/bundleproject/launchwrapper/Launcher.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.bundleproject.launchwrapper
22

3-
import org.bundleproject.launchwrapper.utils.bundleClassName
4-
import org.bundleproject.launchwrapper.utils.err
5-
import org.bundleproject.launchwrapper.utils.get
6-
import org.bundleproject.launchwrapper.utils.info
3+
import org.bundleproject.launchwrapper.utils.*
74
import java.io.File
85
import kotlin.reflect.KClass
96
import kotlin.reflect.full.callSuspend
@@ -28,22 +25,22 @@ suspend fun launch(args: Array<String>, gameDir: File, classLoader: ClassLoader)
2825

2926
val bundleClass = runCatching { Class.forName(bundleClassName, true, classLoader).kotlin }
3027
.onFailure {
31-
err("Bundle is not in the classpath! Cannot launch bundle!")
28+
logger.error(it) { "Bundle is not in the classpath! Cannot launch bundle!" }
3229
return@launch
3330
}
3431
.getOrThrow()
3532

36-
info("Invoking constructor...")
33+
logger.info("Invoking constructor...")
3734
bundleClass.constructors.find { it.typeParameters[1] == String::class.createType() }
3835
?.call(gameDir, version, modFolderName)
3936

40-
info("Starting Bundle... Goodbye, Launchwrapper...")
37+
logger.info("Starting Bundle... Goodbye, Launchwrapper...")
4138
bundleClass.functions.find { it.name == "start" }?.callSuspend()
4239
}
4340
}
4441

4542
private fun findEntrypoint(): KClass<*>? {
46-
err("Relying on fallback entrypoint! Please re-install bundle!")
43+
logger.warn("Relying on fallback entrypoint! Please re-install bundle!")
4744
fallbackEntrypoints.forEach {
4845
runCatching { Class.forName(it) }
4946
.onSuccess { return it.kotlin }

src/main/kotlin/org/bundleproject/launchwrapper/Main.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ package org.bundleproject.launchwrapper
22

33
import java.io.File
44
import org.bundleproject.launchwrapper.utils.get
5-
import org.bundleproject.launchwrapper.utils.info
5+
import org.bundleproject.launchwrapper.utils.logger
66

77
suspend fun main(args: Array<String>) {
8-
val gameDir = File(args["gameDir"] ?: ".")
8+
try {
9+
val gameDir = File(args["gameDir"] ?: ".")
910

10-
info("Updating...")
11-
val version = update(gameDir)
12-
info("Loading...")
13-
val classLoader = load(gameDir, version)
14-
info("Launching...")
15-
launch(args, gameDir, classLoader)
11+
logger.info("Updating...")
12+
val version = update(gameDir)
13+
logger.info("Loading...")
14+
val classLoader = load(gameDir, version)
15+
logger.info("Launching...")
16+
launch(args, gameDir, classLoader)
17+
} catch (e: Throwable) {
18+
logger.error(e) { "Failed to launch Bundle!" }
19+
}
1620
}

src/main/kotlin/org/bundleproject/launchwrapper/Updater.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ suspend fun update(gameDir: File): String {
1212
val jarsFolder = File(bundleFolder, "jars")
1313
jarsFolder.mkdirs()
1414

15-
info("Fetching latest version from github...")
15+
logger.info("Fetching latest version from github...")
1616
val latestRelease = http.get<GithubReleases>("$githubApi/repos/BundleProject/Bundle/releases")
1717
.firstOrNull()
1818
val latestTagName = latestRelease?.tagName
@@ -23,27 +23,27 @@ suspend fun update(gameDir: File): String {
2323
val versionFile = File(bundleFolder, "version.json")
2424

2525
if (latestDownloadUrl != null && latestTagName != null) {
26-
info("Found latest version: $latestTagName")
26+
logger.info("Found latest version: $latestTagName")
2727

2828
val latestJar = File(jarsFolder, "bundle-$latestTagName.jar")
2929

3030
if (!latestJar.exists()) {
31-
important("Downloading Bundle update...")
31+
logger.info("Downloading Bundle update...")
3232
http.downloadFile(latestJar, latestDownloadUrl)
3333

34-
info("Writing latest version to json...")
34+
logger.info("Writing latest version to json...")
3535
val json = JsonObject()
3636
json.addProperty("latest_version", latestTagName)
3737

3838
versionFile.writeText(gson.toJson(json))
3939
} else {
40-
important("Bundle is already up to date!")
40+
logger.info("Bundle is already up to date!")
4141
}
4242

4343
return latestTagName
4444
}
4545

46-
err("Failed to fetch latest release! Returning current latest version to continue safely.")
46+
logger.error("Failed to fetch latest release! Returning current latest version to continue safely.")
4747
return versionFile.readText().let {
4848
gson.fromJson(it, JsonObject::class.java).get("latest_version").asString
4949
}
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
package org.bundleproject.launchwrapper.utils
22

3-
const val RESET = "\u001b[0m" // Text Reset
3+
import mu.KotlinLogging
44

5-
const val BLACK = "\u001b[0;30m"
6-
const val RED = "\u001b[0;31m"
7-
const val GREEN = "\u001b[0;32m"
8-
const val YELLOW = "\u001b[0;33m"
9-
const val BLUE = "\u001b[0;34m"
10-
const val PURPLE = "\u001b[0;35m"
11-
const val CYAN = "\u001b[0;36m"
12-
const val WHITE = "\u001b[0;37m"
13-
14-
fun info(any: Any?, newLine: Boolean = true) = print(RESET + "Bundle: ${any?.toString()}" + if (newLine) "\n" else "" + RESET)
15-
fun err(any: Any?, newLine: Boolean = true) = print(RED + "Bundle: ${any?.toString()}" + if (newLine) "\n" else "" + RESET)
16-
fun important(any: Any?, newLine: Boolean = true) = print(PURPLE + "Bundle: ${any?.toString()}" + if (newLine) "\n" else "" + RESET)
5+
val logger = KotlinLogging.logger("Bundle LaunchWrapper")

src/main/resources/logback.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<configuration>
2+
<timestamp key="byDay" datePattern="YYYY.MM.dd-HH-mm-ss"/>
3+
4+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5+
<encoder>
6+
<pattern>%highlight(%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level (%logger{36}\) - %msg%n)</pattern>
7+
</encoder>
8+
</appender>
9+
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
10+
<file>logs/bundle-${byDay}.log</file>
11+
<append>false</append>
12+
<encoder>
13+
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
14+
</encoder>
15+
</appender>
16+
<root level="trace">
17+
<appender-ref ref="STDOUT"/>
18+
<appender-ref ref="FILE"/>
19+
</root>
20+
<logger name="org.eclipse.jetty" level="INFO"/>
21+
<logger name="io.netty" level="INFO"/>
22+
</configuration>

0 commit comments

Comments
 (0)