Skip to content

Commit

Permalink
add target feature
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed Aug 20, 2024
1 parent b670af3 commit daf6320
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
28 changes: 16 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
[workspace.package]
version = "1.7.0"
edition = "2021"
authors = ["baoyachi <liaoymxsdl@gmail.com>"]
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
Expand All @@ -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"
Expand All @@ -30,14 +42,6 @@ serde_yaml = "0.9.13"
[workspace]
members = ["./", "simple-log-derive"]

[workspace.package]
version = "1.7.0"
edition = "2021"
authors = ["baoyachi <liaoymxsdl@gmail.com>"]
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"]
38 changes: 19 additions & 19 deletions simple-log-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
TokenStream::from(combined)
}
18 changes: 15 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = Result<T, String>;

const SIMPLE_LOG_FILE: &str = "simple_log_file";
Expand Down Expand Up @@ -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<Box<RollingFileAppender>> {
// 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)
Expand Down
3 changes: 1 addition & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ macro_rules! trace {
)
}


#[cfg(feature = "target")]
#[macro_export(local_inner_macros)]
macro_rules! log_target {
($($x:expr),+ $(,)?) => {
Expand All @@ -66,7 +66,6 @@ macro_rules! log_target {
};
}


#[macro_export]
macro_rules! quick {
() => {
Expand Down

0 comments on commit daf6320

Please sign in to comment.