mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-07 05:49:14 +08:00
Improved logger
This commit is contained in:
parent
312fb43776
commit
900040f963
@ -6,10 +6,7 @@ import net.mamoe.mirai.event.events.server.ServerDisabledEvent;
|
||||
import net.mamoe.mirai.event.events.server.ServerEnabledEvent;
|
||||
import net.mamoe.mirai.network.packet.login.LoginState;
|
||||
import net.mamoe.mirai.task.MiraiTaskManager;
|
||||
import net.mamoe.mirai.utils.BotAccount;
|
||||
import net.mamoe.mirai.utils.LoggerTextFormat;
|
||||
import net.mamoe.mirai.utils.MiraiLogger;
|
||||
import net.mamoe.mirai.utils.MiraiLoggerKt;
|
||||
import net.mamoe.mirai.utils.*;
|
||||
import net.mamoe.mirai.utils.config.MiraiConfig;
|
||||
import net.mamoe.mirai.utils.setting.MiraiSettingListSection;
|
||||
import net.mamoe.mirai.utils.setting.MiraiSettingMapSection;
|
||||
@ -78,7 +75,7 @@ public final class MiraiServer {
|
||||
this.parentFolder = new File(System.getProperty("user.dir"));
|
||||
this.unix = !System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS");
|
||||
|
||||
this.logger = MiraiLogger.INSTANCE;
|
||||
this.logger = MiraiLogger.Companion;
|
||||
this.eventManager = MiraiEventManager.getInstance();
|
||||
this.taskManager = MiraiTaskManager.getInstance();
|
||||
|
||||
@ -201,7 +198,7 @@ public final class MiraiServer {
|
||||
private Bot getAvailableBot() throws ExecutionException, InterruptedException {
|
||||
for (String it : qqList.split("\n")) {
|
||||
var strings = it.split("----");
|
||||
var bot = new Bot(new BotAccount(Long.parseLong(strings[0]), strings[1]));
|
||||
var bot = new Bot(new BotAccount(Long.parseLong(strings[0]), strings[1]), new Console());
|
||||
|
||||
if (bot.network.tryLogin(200).get() == LoginState.SUCCESS) {
|
||||
MiraiLoggerKt.success(bot, "Login succeed");
|
||||
|
@ -6,6 +6,7 @@ import net.mamoe.mirai.network.BotNetworkHandler;
|
||||
import net.mamoe.mirai.network.BotNetworkHandlerImpl;
|
||||
import net.mamoe.mirai.utils.BotAccount;
|
||||
import net.mamoe.mirai.utils.ContactList;
|
||||
import net.mamoe.mirai.utils.MiraiLogger;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.Closeable;
|
||||
@ -57,6 +58,8 @@ public final class Bot implements Closeable {
|
||||
|
||||
public final BotNetworkHandler network;
|
||||
|
||||
public final MiraiLogger logger;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Bot{id=%d,qq=%d}", id, this.account.getQqNumber());
|
||||
@ -94,9 +97,14 @@ public final class Bot implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
public Bot(@NotNull BotAccount account) {
|
||||
public Bot(@NotNull BotAccount account, @NotNull MiraiLogger logger) {
|
||||
Objects.requireNonNull(account);
|
||||
|
||||
this.account = account;
|
||||
|
||||
this.logger = Objects.requireNonNull(logger);
|
||||
this.logger.setIdentity("Bot" + this.id + "(" + this.account.getQqNumber() + ")");
|
||||
|
||||
this.network = new BotNetworkHandlerImpl(this);
|
||||
}
|
||||
|
||||
|
21
mirai-core/src/main/java/net/mamoe/mirai/Mirai.kt
Normal file
21
mirai-core/src/main/java/net/mamoe/mirai/Mirai.kt
Normal file
@ -0,0 +1,21 @@
|
||||
package net.mamoe.mirai
|
||||
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* @author Him188moe
|
||||
*/
|
||||
object Mirai {
|
||||
val VERSION: String get() = internal.version
|
||||
|
||||
val WORKING_DIRECTORY: File get() = internal.workingDirectory
|
||||
|
||||
|
||||
internal lateinit var internal: MiraiInternal
|
||||
|
||||
internal abstract class MiraiInternal {
|
||||
abstract val workingDirectory: File
|
||||
abstract val version: String
|
||||
}
|
||||
}
|
||||
|
@ -223,7 +223,7 @@ internal class BotNetworkHandlerImpl(private val bot: Bot) : BotNetworkHandler {
|
||||
withContext(Dispatchers.IO) {
|
||||
socket!!.send(DatagramPacket(data, data.size))
|
||||
}
|
||||
bot.cyanL("Packet sent: $packet")
|
||||
bot.cyan("Packet sent: $packet")
|
||||
|
||||
PacketSentEvent(bot, packet).broadcast()
|
||||
} catch (e: Throwable) {
|
||||
|
@ -6,38 +6,56 @@ import net.mamoe.mirai.network.packet.goto
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
|
||||
/**
|
||||
* used to replace old logger
|
||||
*
|
||||
* @author Him188moe
|
||||
* @author NaturalHG
|
||||
*/
|
||||
object MiraiLogger {
|
||||
fun log(o: Any?) = info(o)
|
||||
fun println(o: Any?) = info(o)
|
||||
fun info(o: Any?) = this.print(o.toString(), LoggerTextFormat.RESET)
|
||||
interface MiraiLogger {
|
||||
companion object : Console("[TOP Level]")
|
||||
|
||||
var identity: String
|
||||
|
||||
fun error(o: Any?) = this.print(o.toString(), LoggerTextFormat.RED)
|
||||
fun info(any: Any?) = log(any)
|
||||
fun log(any: Any?)
|
||||
|
||||
fun notice(o: Any?) = this.print(o.toString(), LoggerTextFormat.LIGHT_BLUE)
|
||||
fun error(any: Any?)
|
||||
|
||||
fun success(o: Any?) = this.print(o.toString(), LoggerTextFormat.GREEN)
|
||||
fun debug(any: Any?)
|
||||
|
||||
fun debug(o: Any?) = this.print(o.toString(), LoggerTextFormat.YELLOW)
|
||||
fun cyan(any: Any?)
|
||||
|
||||
fun catching(e: Throwable) {
|
||||
e.printStackTrace()
|
||||
/*
|
||||
this.print(e.message)
|
||||
this.print(e.localizedMessage)
|
||||
this.print(e.cause.toString())*/
|
||||
fun purple(any: Any?)
|
||||
|
||||
fun green(any: Any?)
|
||||
|
||||
fun blue(any: Any?)
|
||||
}
|
||||
|
||||
val DEBUGGING: Boolean by lazy {
|
||||
//avoid inspections
|
||||
true
|
||||
}
|
||||
|
||||
open class Console(
|
||||
override var identity: String = "[Unknown]"
|
||||
) : MiraiLogger {
|
||||
override fun green(any: Any?) = print(any.toString(), LoggerTextFormat.GREEN)
|
||||
override fun purple(any: Any?) = print(any.toString(), LoggerTextFormat.LIGHT_PURPLE)
|
||||
override fun blue(any: Any?) = print(any.toString(), LoggerTextFormat.BLUE)
|
||||
override fun cyan(any: Any?) = print(any.toString(), LoggerTextFormat.LIGHT_CYAN)
|
||||
override fun error(any: Any?) = print(any.toString(), LoggerTextFormat.RED)
|
||||
override fun log(any: Any?) = print(any.toString(), LoggerTextFormat.LIGHT_GRAY)
|
||||
override fun debug(any: Any?) {
|
||||
if (DEBUGGING) {
|
||||
print(any.toString(), LoggerTextFormat.YELLOW)
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun print(value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) {
|
||||
fun print(value: String?, color: LoggerTextFormat = LoggerTextFormat.YELLOW) {
|
||||
val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date())
|
||||
kotlin.io.println("$color[Mirai] $s : $value")
|
||||
|
||||
println("$color$identity $s : $value")
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,7 +69,7 @@ fun Bot.notice(o: Any?) = print(this, o.toString(), LoggerTextFormat.LIGHT_BLUE)
|
||||
|
||||
fun Bot.purple(o: Any?) = print(this, o.toString(), LoggerTextFormat.PURPLE)
|
||||
|
||||
fun Bot.cyanL(o: Any?) = print(this, o.toString(), LoggerTextFormat.LIGHT_CYAN)
|
||||
fun Bot.cyan(o: Any?) = print(this, o.toString(), LoggerTextFormat.LIGHT_CYAN)
|
||||
fun Bot.success(o: Any?) = print(this, o.toString(), LoggerTextFormat.GREEN)
|
||||
|
||||
fun Bot.debug(o: Any?) = print(this, o.toString(), LoggerTextFormat.YELLOW)
|
||||
@ -69,17 +87,8 @@ private fun print(bot: Bot, value: String?, color: LoggerTextFormat = LoggerText
|
||||
kotlin.io.println("$color[Mirai] $s #R${bot.id}: $value")
|
||||
}
|
||||
|
||||
|
||||
@Synchronized
|
||||
private fun print(value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) {
|
||||
val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date())
|
||||
kotlin.io.println("$color[Mirai] $s : $value")
|
||||
}
|
||||
|
||||
fun Any.logInfo() = MiraiLogger.info(this)
|
||||
|
||||
fun Any.logDebug() = MiraiLogger.debug(this)
|
||||
|
||||
fun Any.logError() = MiraiLogger.error(this)
|
||||
|
||||
fun Any.logNotice() = MiraiLogger.notice(this)
|
@ -3,6 +3,7 @@ import kotlinx.coroutines.launch
|
||||
import net.mamoe.mirai.Bot
|
||||
import net.mamoe.mirai.network.packet.login.LoginState
|
||||
import net.mamoe.mirai.utils.BotAccount
|
||||
import net.mamoe.mirai.utils.Console
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
@ -74,7 +75,7 @@ fun main() {
|
||||
return@let password.substring(0, password.length - 1)
|
||||
}
|
||||
return@let password
|
||||
}))
|
||||
}), Console())
|
||||
|
||||
bot.network.tryLogin().whenComplete { state, _ ->
|
||||
if (!(state == LoginState.BLOCKED || state == LoginState.DEVICE_LOCK || state == LoginState.WRONG_PASSWORD)) {
|
||||
|
@ -9,6 +9,7 @@ import net.mamoe.mirai.message.defaults.Image
|
||||
import net.mamoe.mirai.message.defaults.PlainText
|
||||
import net.mamoe.mirai.network.packet.login.LoginState
|
||||
import net.mamoe.mirai.utils.BotAccount
|
||||
import net.mamoe.mirai.utils.Console
|
||||
|
||||
/**
|
||||
* @author Him188moe
|
||||
@ -17,7 +18,7 @@ fun main() {
|
||||
val bot = Bot(BotAccount(
|
||||
qqNumber = 1683921395,
|
||||
password = "bb22222"
|
||||
))
|
||||
), Console())
|
||||
|
||||
bot.network.tryLogin().get().let {
|
||||
check(it == LoginState.SUCCESS) { "Login failed: " + it.name }
|
||||
|
Loading…
Reference in New Issue
Block a user