From 789dd2494e12c2c78db92e38e4333c0aff651797 Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Mon, 29 Jan 2024 23:35:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8Alog=E6=8D=A2=E6=88=90tracing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 11 ++--------- src/cluster.rs | 4 +++- src/config.rs | 2 +- src/log.rs | 19 ++++--------------- src/utils.rs | 6 +++--- 5 files changed, 13 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7fc16d4..1c41b11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,12 +27,5 @@ sha1 = "0.10.6" zstd = "0.13.0" -log = "0.4.20" -[dependencies.simple_logger] -version = "4.3.3" -features = ["colors", "threads", "timestamps"] -# [dependencies.db_logger] -# version = "0.1" -# optional = true -# default-features = false -# features = ["postgres"] +tracing = "0.1.40" +tracing-subscriber = { version = "0.3.18", features = ["time"] } diff --git a/src/cluster.rs b/src/cluster.rs index 527219d..1beae8d 100644 --- a/src/cluster.rs +++ b/src/cluster.rs @@ -2,7 +2,7 @@ use crate::config::Config; use crate::utils::avro_data_to_file_list; use crate::PROTOCOL_VERSION; -use log::{info, warn}; +use tracing::{info, instrument, warn}; use reqwest::{Client, StatusCode}; use serde::Deserialize; use zstd::stream::decode_all; @@ -14,6 +14,7 @@ pub struct SyncFile { pub size: i64, } +#[derive(Debug, Clone)] pub struct Cluster { pub config: Config, pub ua: String, @@ -60,6 +61,7 @@ impl Cluster { pub async fn get_file_list(&self) -> Option> { // server: https://openbmclapi.bangbang93.com // path: /openbmclapi/files + info!("initing"); let url = self.config.join_center_url("/openbmclapi/files"); let password = self.config.cluster_secret.clone(); let username = self.config.cluster_id.clone(); diff --git a/src/config.rs b/src/config.rs index 9b7e951..6d1aa3c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ use { crate::fatal, - log::{info, warn}, + tracing::{info, warn}, serde::{Deserialize, Serialize}, std::{ env, fs, diff --git a/src/log.rs b/src/log.rs index 739ebb2..9d02be1 100644 --- a/src/log.rs +++ b/src/log.rs @@ -1,22 +1,11 @@ +use tracing_subscriber::FmtSubscriber; + pub fn init_log_with_cli() { // 命令行参数 // --warn // --debug // --trace // 从低级开始判断 - let log_level; - if std::env::args().any(|x| x == "--trace") { - log_level = log::LevelFilter::Trace; - } else if std::env::args().any(|x| x == "--debug") { - log_level = log::LevelFilter::Debug; - } else if std::env::args().any(|x| x == "--warn") { - log_level = log::LevelFilter::Warn; - } else { - log_level = log::LevelFilter::Info; - } - simple_logger::SimpleLogger::new() - .with_level(log_level) - .env() - .init() - .unwrap(); + + tracing_subscriber::fmt::init(); } diff --git a/src/utils.rs b/src/utils.rs index fc98158..e771d4e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,7 +3,7 @@ use crate::cluster::SyncFile; use std::io::Cursor; use apache_avro::{from_avro_datum, from_value, types::Value}; -use log::{info, warn}; +use tracing::{info, warn}; use md5::{Digest, Md5}; use sha1::Sha1; use std::path::PathBuf; @@ -116,14 +116,14 @@ pub fn avro_data_to_file_list(data: Vec) -> Option> { macro_rules! fatal { // 正常输入两组信息 (($($arg1:tt)+), ($($arg2:tt)+)) => { - use log::error; + use tracing::error; // error!() + panic!() error!($($arg1)+); panic!($($arg2)+); }; // 如果只输入了一组 ($($arg:tt)+) => { - use log::error; + use tracing::error; // error!() + panic!() error!($($arg)+); panic!($($arg)+);