Skip to content

Commit

Permalink
refactor(log): 改进日志
Browse files Browse the repository at this point in the history
输出时间, 中文化等
  • Loading branch information
itsusinn committed Jan 18, 2022
1 parent 29110cb commit 5c0f838
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
1 change: 1 addition & 0 deletions src/despatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub async fn cmd_or_msg_repl_with_listener<N, Cmd, CH, MH, FutC, FutM, L, Listen
let cmd_handler = Arc::new(cmd_handler);
let msg_handler = Arc::new(msg_handler);

log::info!("Mesagisto-Bot启动成功");
Dispatcher::new(bot.clone())
.messages_handler(move |rx: DispatcherHandlerRx<AutoSend<Bot>, Message>| {
UnboundedReceiverStream::new(rx).for_each_concurrent(None, move |cx| {
Expand Down
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 7 additions & 4 deletions src/message/handlers/receive.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryInto;

use crate::ext::db::DbExt;
use crate::CONFIG;
use crate::TG_BOT;
Expand All @@ -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<u8>) -> 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) => {
Expand All @@ -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() {
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 5c0f838

Please sign in to comment.