Implement SingleFileLogger separately for different platforms, ensure binary compatibility

This commit is contained in:
Him188 2021-02-24 09:55:33 +08:00
parent 0b971b2117
commit 1ce26c393c
6 changed files with 113 additions and 28 deletions

View File

@ -5902,7 +5902,7 @@ public final class net/mamoe/mirai/utils/SimpleLogger$LogPriority : java/lang/En
public static fun values ()[Lnet/mamoe/mirai/utils/SimpleLogger$LogPriority; public static fun values ()[Lnet/mamoe/mirai/utils/SimpleLogger$LogPriority;
} }
public final class net/mamoe/mirai/utils/SingleFileLogger : net/mamoe/mirai/utils/PlatformLogger { public final class net/mamoe/mirai/utils/SingleFileLogger : net/mamoe/mirai/utils/PlatformLogger, net/mamoe/mirai/utils/MiraiLogger {
public fun <init> (Ljava/lang/String;)V public fun <init> (Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ljava/io/File;)V public fun <init> (Ljava/lang/String;Ljava/io/File;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/io/File;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public synthetic fun <init> (Ljava/lang/String;Ljava/io/File;ILkotlin/jvm/internal/DefaultConstructorMarker;)V

View File

@ -0,0 +1,33 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.utils
import net.mamoe.mirai.internal.utils.StdoutLogger
import java.io.File
/**
* 将日志写入('append')到特定文件.
*
* @see PlatformLogger 查看格式信息
*/
public actual class SingleFileLogger actual constructor(
identity: String,
file: File
) : MiraiLogger by StdoutLogger(identity, { file.appendText(it + "\n") }) {
public actual constructor(identity: String) : this(identity, File("$identity-${getCurrentDate()}.log"))
init {
file.createNewFile()
require(file.isFile) { "Log file must be a file: $file" }
require(file.canWrite()) { "Log file must be write: $file" }
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -9,30 +9,12 @@
package net.mamoe.mirai.utils package net.mamoe.mirai.utils
import net.mamoe.mirai.internal.utils.StdoutLogger
import java.io.File import java.io.File
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
private val currentDay get() = Calendar.getInstance()[Calendar.DAY_OF_MONTH] internal fun getCurrentDay() = Calendar.getInstance()[Calendar.DAY_OF_MONTH]
private val currentDate get() = SimpleDateFormat("yyyy-MM-dd").format(Date()) internal fun getCurrentDate() = SimpleDateFormat("yyyy-MM-dd").format(Date())
/**
* 将日志写入('append')到特定文件.
*
* @see PlatformLogger 查看格式信息
*/
public class SingleFileLogger @JvmOverloads constructor(
identity: String,
file: File = File("$identity-$currentDate.log")
) : MiraiLogger by StdoutLogger(identity, { file.appendText(it + "\n") }) {
init {
file.createNewFile()
require(file.isFile) { "Log file must be a file: $file" }
require(file.canWrite()) { "Log file must be write: $file" }
}
}
private val STUB: (priority: SimpleLogger.LogPriority, message: String?, e: Throwable?) -> Unit = private val STUB: (priority: SimpleLogger.LogPriority, message: String?, e: Throwable?) -> Unit =
{ _: SimpleLogger.LogPriority, _: String?, _: Throwable? -> error("stub") } { _: SimpleLogger.LogPriority, _: String?, _: Throwable? -> error("stub") }
@ -61,15 +43,15 @@ public class DirectoryLogger @JvmOverloads constructor(
} }
} }
private var day = currentDay private var day = getCurrentDay()
private var delegate: SingleFileLogger = SingleFileLogger(identity, File(directory, "$currentDate.log")) private var delegate: SingleFileLogger = SingleFileLogger(identity, File(directory, "${getCurrentDate()}.log"))
get() { get() {
val currentDay = currentDay val currentDay = getCurrentDay()
if (day != currentDay) { if (day != currentDay) {
day = currentDay day = currentDay
checkOutdated() checkOutdated()
field = SingleFileLogger(identity!!, File(directory, "$currentDate.log")) field = SingleFileLogger(identity!!, File(directory, "${getCurrentDate()}.log"))
} }
return field return field
} }

View File

@ -0,0 +1,28 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:JvmName("FileLoggerKt") // bin-comp
package net.mamoe.mirai.utils
import java.io.File
/**
* 将日志写入('append')到特定文件.
*
* @see PlatformLogger 查看格式信息
*/
public expect class SingleFileLogger : MiraiLogger {
public constructor(identity: String)
public constructor(identity: String, file: File = File("$identity-${getCurrentDate()}.log"))
// Implementation notes v2.5.0:
// default argument `file` to produce synthetic constructor with `DefaultConstructorMarker` for binary compatibility
// dedicated constructor with single parameter `identity` for the same reason.
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -49,8 +49,11 @@ public actual open class PlatformLogger constructor( // same as StdoutLogger bu
public open val output: (String) -> Unit, public open val output: (String) -> Unit,
public val isColored: Boolean = true public val isColored: Boolean = true
) : MiraiLoggerPlatformBase() { ) : MiraiLoggerPlatformBase() {
// PlatformLogger("") resolves to this one.
public actual constructor(identity: String?) : this(identity, ::println) public actual constructor(identity: String?) : this(identity, ::println)
public constructor(identity: String?, output: (String) -> Unit) : this(identity, output, true)
public constructor(identity: String?, output: (String) -> Unit = ::println) : this(identity, output, true)
/** /**
* 输出一条日志. [message] 末尾可能不带换行符. * 输出一条日志. [message] 末尾可能不带换行符.
@ -102,6 +105,7 @@ public actual open class PlatformLogger constructor( // same as StdoutLogger bu
if (e != null) debug((message ?: e.toString()) + "\n${e.stackTraceToString()}") if (e != null) debug((message ?: e.toString()) + "\n${e.stackTraceToString()}")
else debug(message.toString()) else debug(message.toString())
} }
protected open val timeFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE) protected open val timeFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE)
private val currentTimeFormatted get() = timeFormat.format(Date()) private val currentTimeFormatted get() = timeFormat.format(Date())

View File

@ -0,0 +1,38 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("unused")
package net.mamoe.mirai.utils
import java.io.File
/**
* 将日志写入('append')到特定文件.
*
* @see PlatformLogger 查看格式信息
*/
public actual class SingleFileLogger actual constructor(
identity: String,
file: File
) : MiraiLogger, PlatformLogger(identity, { file.appendText(it + "\n") }) {
// Implementation notes v2.5.0:
// Extending `PlatformLogger` for binary compatibility for JVM target only.
// See actual declaration in androidMain for a better impl (implements `MiraiLogger` only)
public actual constructor(identity: String) : this(identity, File("$identity-${getCurrentDate()}.log"))
init {
file.createNewFile()
require(file.isFile) { "Log file must be a file: $file" }
require(file.canWrite()) { "Log file must be write: $file" }
}
}