From daf632061405c72ecfbbe0449ff13fc6e6797ce1 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Tue, 20 Aug 2024 11:55:30 +0800 Subject: [PATCH] add target feature --- Cargo.toml | 28 ++++++++++++++------------ simple-log-derive/src/lib.rs | 38 ++++++++++++++++++------------------ src/lib.rs | 18 ++++++++++++++--- src/macros.rs | 3 +-- 4 files changed, 51 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 56e1dae..a6fdb00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,15 @@ +[workspace.package] +version = "1.7.0" +edition = "2021" +authors = ["baoyachi "] +description = "A simple log. It's really simple use" +keywords = ["log", "simple-log", "logger", "log4j", "log4rs"] +readme = "README.md" +categories = ["development-tools::debugging"] +documentation = "https://docs.rs/simple-log" +repository = "https://github.com/baoyachi/simple-log" +license = "MIT Or Apache-2.0" + [package] name = "simple-log" version.workspace = true @@ -20,7 +32,7 @@ once_cell = "1.15.0" serde = { version = "1.0.145", features = ["derive"] } is_debug = "1.0.1" convert_case = "0.6.0" -simple-log-derive = { path = "simple-log-derive" } +simple-log-derive = { path = "simple-log-derive", optional = true } [dev-dependencies] serde_json = "1" @@ -30,14 +42,6 @@ serde_yaml = "0.9.13" [workspace] members = ["./", "simple-log-derive"] -[workspace.package] -version = "1.7.0" -edition = "2021" -authors = ["baoyachi "] -description = "A simple log. It's really simple use" -keywords = ["log", "simple-log", "logger", "log4j", "log4rs"] -readme = "README.md" -categories = ["development-tools::debugging"] -documentation = "https://docs.rs/simple-log" -repository = "https://github.com/baoyachi/simple-log" -license = "MIT Or Apache-2.0" + +[features] +target = ["simple-log-derive"] \ No newline at end of file diff --git a/simple-log-derive/src/lib.rs b/simple-log-derive/src/lib.rs index 5b48001..0c694f6 100644 --- a/simple-log-derive/src/lib.rs +++ b/simple-log-derive/src/lib.rs @@ -4,35 +4,35 @@ use syn::{parse_macro_input, Ident}; #[proc_macro] pub fn log_target_derive(input: TokenStream) -> TokenStream { - let input = parse_macro_input!(input as Ident); + let input = parse_macro_input!(input as Ident); - const LOG_LEVELS: [&str; 5] = ["trace", "debug", "info", "warn", "error"]; + const LOG_LEVELS: [&str; 5] = ["trace", "debug", "info", "warn", "error"]; - let mut parts = Vec::new(); + let mut parts = Vec::new(); - for level in LOG_LEVELS.iter() { - let log_target = Ident::new(&format!("{}_{}", level, input), input.span()); - let log_level = match *level { - "trace" => quote!($crate::log::Level::Trace), - "debug" => quote!($crate::log::Level::Debug), - "info" => quote!($crate::log::Level::Info), - "warn" => quote!($crate::log::Level::Warn), - "error" => quote!($crate::log::Level::Error), - _ => unreachable!(), - }; + for level in LOG_LEVELS.iter() { + let log_target = Ident::new(&format!("{}_{}", level, input), input.span()); + let log_level = match *level { + "trace" => quote!($crate::log::Level::Trace), + "debug" => quote!($crate::log::Level::Debug), + "info" => quote!($crate::log::Level::Info), + "warn" => quote!($crate::log::Level::Warn), + "error" => quote!($crate::log::Level::Error), + _ => unreachable!(), + }; - let expand = quote! { + let expand = quote! { #[macro_export] macro_rules! #log_target { ($($arg:tt)*) => ($crate::log::log!(target: stringify!(#input), #log_level, $($arg),*)); } }; - parts.push(expand); - } + parts.push(expand); + } - let combined = quote! { + let combined = quote! { #(#parts)* }; - TokenStream::from(combined) -} \ No newline at end of file + TokenStream::from(combined) +} diff --git a/src/lib.rs b/src/lib.rs index efbb9a6..b7ce986 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,9 +105,9 @@ use serde::{Deserialize, Serialize}; use std::sync::Mutex; pub use is_debug::{is_debug, is_release}; +#[cfg(feature = "target")] pub use simple_log_derive::*; - pub type SimpleResult = Result; const SIMPLE_LOG_FILE: &str = "simple_log_file"; @@ -615,13 +615,25 @@ fn encoder(time_format: Option<&String>, color: bool) -> PatternEncoder { false => "l", }; let mut pattern = format!("{{d({})}} [{{{}}}] ", time_format, color_level); - pattern += "<{M}:{L}>:{m}{n}"; + + #[cfg(feature = "target")] + { + pattern += "[{t}] <{f}:{L}>:{m}{n}"; + } + #[cfg(not(feature = "target"))] + { + pattern += "<{f}:{L}>:{m}{n}"; + } + PatternEncoder::new(pattern.as_str()) } fn file_appender(log: &LogConfig) -> SimpleResult> { // If the log is written to a file, the path parameter is required - let path = log.path.as_ref().expect("Expected the path to write the log file, but it is empty"); + let path = log + .path + .as_ref() + .expect("Expected the path to write the log file, but it is empty"); let roll = FixedWindowRoller::builder() .base(0) .build(format!("{}.{{}}.gz", path).as_str(), log.roll_count) diff --git a/src/macros.rs b/src/macros.rs index 466a2e9..3bf7ad6 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -53,7 +53,7 @@ macro_rules! trace { ) } - +#[cfg(feature = "target")] #[macro_export(local_inner_macros)] macro_rules! log_target { ($($x:expr),+ $(,)?) => { @@ -66,7 +66,6 @@ macro_rules! log_target { }; } - #[macro_export] macro_rules! quick { () => {