mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-20 19:09:12 +08:00
Add BotConfiguration.workingDir
This commit is contained in:
parent
767eb75d4e
commit
54add6df78
@ -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 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
@ -7,7 +7,7 @@
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("unused", "DEPRECATION_ERROR", "EXPOSED_SUPER_CLASS")
|
||||
@file:Suppress("unused", "DEPRECATION_ERROR", "EXPOSED_SUPER_CLASS", "MemberVisibilityCanBePrivate")
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
@ -25,7 +25,7 @@ import kotlin.coroutines.coroutineContext
|
||||
*
|
||||
* Kotlin 使用方法:
|
||||
* ```
|
||||
* val bot = Bot(...) {
|
||||
* val bot = BotFactory.newBot(...) {
|
||||
* // 在这里配置 Bot
|
||||
*
|
||||
* bogLoggerSupplier = { bot -> ... }
|
||||
@ -45,6 +45,11 @@ import kotlin.coroutines.coroutineContext
|
||||
*/
|
||||
@Suppress("PropertyName")
|
||||
public open class BotConfiguration { // open for Java
|
||||
/**
|
||||
* 工作目录. 默认为 "."
|
||||
*/
|
||||
public var workingDir: File = File(".")
|
||||
|
||||
/**
|
||||
* 日志记录器
|
||||
*
|
||||
@ -146,8 +151,23 @@ public open class BotConfiguration { // open for Java
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用文件存储设备信息.
|
||||
*
|
||||
* 此函数只在 JVM 和 Android 有效. 在其他平台将会抛出异常.
|
||||
* @param filepath 文件路径. 默认是相对于 [workingDir] 的文件 "device.json".
|
||||
* @see deviceInfo
|
||||
*/
|
||||
@JvmOverloads
|
||||
@ConfigurationDsl
|
||||
public fun fileBasedDeviceInfo(filepath: String = "device.json") {
|
||||
deviceInfo = getFileBasedDeviceInfoSupplier { workingDir.resolve(filepath) }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重定向 [网络日志][networkLoggerSupplier] 到指定目录. 若目录不存在将会自动创建 ([File.mkdirs])
|
||||
* 默认目录路径为 "$workingDir/logs/".
|
||||
* @see DirectoryLogger
|
||||
* @see redirectNetworkLogToDirectory
|
||||
*/
|
||||
@ -159,12 +179,11 @@ public open class BotConfiguration { // open for Java
|
||||
identity: (bot: Bot) -> String = { "Net ${it.id}" }
|
||||
) {
|
||||
require(!dir.isFile) { "dir must not be a file" }
|
||||
dir.mkdirs()
|
||||
networkLoggerSupplier = { DirectoryLogger(identity(it), dir, retain) }
|
||||
networkLoggerSupplier = { DirectoryLogger(identity(it), workingDir.resolve(dir), retain) }
|
||||
}
|
||||
|
||||
/**
|
||||
* 重定向 [网络日志][networkLoggerSupplier] 到指定文件.
|
||||
* 重定向 [网络日志][networkLoggerSupplier] 到指定文件. 默认文件路径为 "$workingDir/mirai.log".
|
||||
* 日志将会逐行追加到此文件. 若文件不存在将会自动创建 ([File.createNewFile])
|
||||
* @see SingleFileLogger
|
||||
* @see redirectNetworkLogToDirectory
|
||||
@ -176,25 +195,7 @@ public open class BotConfiguration { // open for Java
|
||||
identity: (bot: Bot) -> String = { "Net ${it.id}" }
|
||||
) {
|
||||
require(!file.isDirectory) { "file must not be a dir" }
|
||||
file.createNewFile()
|
||||
networkLoggerSupplier = { SingleFileLogger(identity(it), file) }
|
||||
}
|
||||
|
||||
/**
|
||||
* 重定向 [Bot 日志][botLoggerSupplier] 到指定目录. 若目录不存在将会自动创建 ([File.mkdirs])
|
||||
* @see DirectoryLogger
|
||||
* @see redirectBotLogToFile
|
||||
*/
|
||||
@JvmOverloads
|
||||
@ConfigurationDsl
|
||||
public fun redirectBotLogToDirectory(
|
||||
dir: File = File("logs"),
|
||||
retain: Long = 1.weeksToMillis,
|
||||
identity: (bot: Bot) -> String = { "Net ${it.id}" }
|
||||
) {
|
||||
require(!dir.isFile) { "dir must not be a file" }
|
||||
dir.mkdirs()
|
||||
botLoggerSupplier = { DirectoryLogger(identity(it), dir, retain) }
|
||||
networkLoggerSupplier = { SingleFileLogger(identity(it), workingDir.resolve(file)) }
|
||||
}
|
||||
|
||||
/**
|
||||
@ -210,8 +211,24 @@ public open class BotConfiguration { // open for Java
|
||||
identity: (bot: Bot) -> String = { "Net ${it.id}" }
|
||||
) {
|
||||
require(!file.isDirectory) { "file must not be a dir" }
|
||||
file.createNewFile()
|
||||
botLoggerSupplier = { SingleFileLogger(identity(it), file) }
|
||||
botLoggerSupplier = { SingleFileLogger(identity(it), workingDir.resolve(file)) }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重定向 [Bot 日志][botLoggerSupplier] 到指定目录. 若目录不存在将会自动创建 ([File.mkdirs])
|
||||
* @see DirectoryLogger
|
||||
* @see redirectBotLogToFile
|
||||
*/
|
||||
@JvmOverloads
|
||||
@ConfigurationDsl
|
||||
public fun redirectBotLogToDirectory(
|
||||
dir: File = File("logs"),
|
||||
retain: Long = 1.weeksToMillis,
|
||||
identity: (bot: Bot) -> String = { "Net ${it.id}" }
|
||||
) {
|
||||
require(!dir.isFile) { "dir must not be a file" }
|
||||
botLoggerSupplier = { DirectoryLogger(identity(it), workingDir.resolve(dir), retain) }
|
||||
}
|
||||
|
||||
public enum class MiraiProtocol {
|
||||
@ -240,35 +257,6 @@ public open class BotConfiguration { // open for Java
|
||||
public val Default: BotConfiguration = BotConfiguration()
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用文件存储设备信息.
|
||||
*
|
||||
* 此函数只在 JVM 和 Android 有效. 在其他平台将会抛出异常.
|
||||
* @param filepath 文件路径. 可相对于程序运行路径 (`user.dir`), 也可以是绝对路径.
|
||||
* @see deviceInfo
|
||||
*/
|
||||
@JvmOverloads
|
||||
@ConfigurationDsl
|
||||
public fun fileBasedDeviceInfo(filepath: String = "device.json") {
|
||||
deviceInfo = getFileBasedDeviceInfoSupplier(filepath)
|
||||
}
|
||||
|
||||
public fun copy(): BotConfiguration {
|
||||
return BotConfiguration().also { new ->
|
||||
new.botLoggerSupplier = botLoggerSupplier
|
||||
new.networkLoggerSupplier = networkLoggerSupplier
|
||||
new.deviceInfo = deviceInfo
|
||||
new.parentCoroutineContext = parentCoroutineContext
|
||||
new.heartbeatPeriodMillis = heartbeatPeriodMillis
|
||||
new.heartbeatTimeoutMillis = heartbeatTimeoutMillis
|
||||
new.firstReconnectDelayMillis = firstReconnectDelayMillis
|
||||
new.reconnectPeriodMillis = reconnectPeriodMillis
|
||||
new.reconnectionRetryTimes = reconnectionRetryTimes
|
||||
new.loginSolver = loginSolver
|
||||
new.protocol = protocol
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 不显示网络日志. 不推荐.
|
||||
@ -346,6 +334,24 @@ public open class BotConfiguration { // open for Java
|
||||
parentCoroutineContext = coroutineContext
|
||||
}
|
||||
|
||||
|
||||
public fun copy(): BotConfiguration {
|
||||
return BotConfiguration().also { new ->
|
||||
new.botLoggerSupplier = botLoggerSupplier
|
||||
new.networkLoggerSupplier = networkLoggerSupplier
|
||||
new.deviceInfo = deviceInfo
|
||||
new.parentCoroutineContext = parentCoroutineContext
|
||||
new.heartbeatPeriodMillis = heartbeatPeriodMillis
|
||||
new.heartbeatTimeoutMillis = heartbeatTimeoutMillis
|
||||
new.firstReconnectDelayMillis = firstReconnectDelayMillis
|
||||
new.reconnectPeriodMillis = reconnectPeriodMillis
|
||||
new.reconnectionRetryTimes = reconnectionRetryTimes
|
||||
new.loginSolver = loginSolver
|
||||
new.protocol = protocol
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** 标注一个配置 DSL 函数 */
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
@DslMarker
|
||||
|
@ -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 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
@ -221,9 +221,9 @@ public class StandardCharImageLoginSolver @JvmOverloads constructor(
|
||||
//////////////// internal
|
||||
///////////////////////////////
|
||||
|
||||
internal fun BotConfiguration.getFileBasedDeviceInfoSupplier(filename: String): (Bot) -> DeviceInfo {
|
||||
internal fun BotConfiguration.getFileBasedDeviceInfoSupplier(file: () -> File): (Bot) -> DeviceInfo {
|
||||
return {
|
||||
File(filename).loadAsDeviceInfo(json)
|
||||
file().loadAsDeviceInfo(json)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user