From 5c0f83810e016fcc7acfa5d39284b0c0b42d2f1f Mon Sep 17 00:00:00 2001 From: itsusinn Date: Tue, 18 Jan 2022 23:12:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(log):=20=E6=94=B9=E8=BF=9B=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 输出时间, 中文化等 --- src/bot.rs | 2 +- src/despatch.rs | 1 + src/main.rs | 12 ++++++------ src/message/handlers/receive.rs | 11 +++++++---- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 308aad5..781b6b3 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -19,7 +19,7 @@ impl TgBot { let TgFile { file_path, .. } = self .get_file(String::from_utf8_lossy(id)) .await - .expect("failed to get file"); + .expect("获取文件失败"); let tmp_path = RES.tmp_path(&id_str); let url = self.get_url_by_path(file_path); NET.download(&url, &tmp_path).await?; diff --git a/src/despatch.rs b/src/despatch.rs index aaba8e8..2a7abeb 100644 --- a/src/despatch.rs +++ b/src/despatch.rs @@ -63,6 +63,7 @@ pub async fn cmd_or_msg_repl_with_listener, Message>| { UnboundedReceiverStream::new(rx).for_each_concurrent(None, move |cx| { diff --git a/src/main.rs b/src/main.rs index d00eb87..ffbe8f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![allow(incomplete_features)] #![feature(backtrace, capture_disjoint_fields)] +use env_logger::TimestampPrecision; use futures::FutureExt; use mesagisto_client::MesagistoConfig; use std::sync::Arc; @@ -26,14 +27,14 @@ mod net; mod webhook; fn main() { - std::env::set_var("RUST_BACKTRACE", "1"); + std::backtrace::Backtrace::force_capture(); env_logger::builder() .write_style(env_logger::WriteStyle::Auto) - .filter(None, log::LevelFilter::Error) - .format_timestamp(None) - .filter(Some("telegram_message_source"), log::LevelFilter::Trace) - .filter(Some("mesagisto_client"), log::LevelFilter::Trace) + .filter(None, log::LevelFilter::Warn) + .format_timestamp(Some(TimestampPrecision::Seconds)) + .filter(Some("telegram_message_source"), log::LevelFilter::Info) + .filter(Some("mesagisto_client"), log::LevelFilter::Info) .filter(Some("teloxide"), log::LevelFilter::Info) .init(); tokio::runtime::Builder::new_multi_thread() @@ -78,7 +79,6 @@ async fn run() -> Result<(), anyhow::Error> { .apply() .await; - log::info!("Mesagisto-Bot is starting up"); log::info!("Mesagisto-Bot正在启动"); let bot = Bot::with_client(CONFIG.telegram.token.clone(), net::client_from_config()).auto_send(); diff --git a/src/message/handlers/receive.rs b/src/message/handlers/receive.rs index eae08e2..04792ab 100644 --- a/src/message/handlers/receive.rs +++ b/src/message/handlers/receive.rs @@ -1,3 +1,5 @@ +use std::convert::TryInto; + use crate::ext::db::DbExt; use crate::CONFIG; use crate::TG_BOT; @@ -11,8 +13,9 @@ use teloxide::prelude::Requester; use teloxide::types::InputFile; -pub async fn receive_from_server(message: nats::asynk::Message, target: i64) -> anyhow::Result<()> { - log::trace!("Receive from {}", target); +pub async fn receive_from_server(message: nats::asynk::Message, target: Vec) -> anyhow::Result<()> { + let target = i64::from_be_bytes(target.try_into().unwrap()); + log::trace!("接收到来自目标{}的消息", target); let packet = Packet::from_cbor(&message.data)?; match packet { either::Left(msg) => { @@ -25,7 +28,7 @@ pub async fn receive_from_server(message: nats::asynk::Message, target: i64) -> pub async fn handle_receive_message(mut message: Message, target: i64) -> anyhow::Result<()> { for single in message.chain { - log::trace!("handling element in chain"); + log::trace!("正在处理消息链中的元素"); let sender_name = if message.profile.nick.is_some() { message.profile.nick.take().unwrap() } else if message.profile.username.is_some() { @@ -53,7 +56,7 @@ pub async fn handle_receive_message(mut message: Message, target: i64) -> anyhow DB.put_msg_id_1(&target, &message.id, &receipt.id)?; } MessageType::Image { id, url } => { - let channel = CONFIG.mapper(&target).expect("Channel don't exist"); + let channel = CONFIG.mapper(&target).expect("频道不存在"); let path = CACHE.file(&id, &url, &channel).await?; let receipt = TG_BOT .send_message(target, format!("{} :", sender_name))