diff --git a/Cargo.toml b/Cargo.toml index 2895974f..cb6b22c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,12 +20,13 @@ include = [ [features] test = [] -default = ["termcolor", "local-offset"] -local-offset = ["time/local-offset"] +default = ["termcolor", "time", "local-offset"] +time = [] +local-offset = ["time", "time/local-offset"] [dependencies] log = { version = "0.4.*", features = ["std"] } termcolor = { version = "1.1.*", optional = true } paris = { version = "~1.5", optional = true } ansi_term = { version = "0.12", optional = true } -time = { version = "0.3.7", features = ["formatting", "macros"] } +time = { version = "0.3.7", optional = true, features = ["formatting", "macros"] } diff --git a/src/config.rs b/src/config.rs index e18ccb37..b8d25c80 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,6 +5,8 @@ use log::LevelFilter; use std::borrow::Cow; #[cfg(feature = "termcolor")] use termcolor::Color; + +#[cfg(feature = "time")] pub use time::{format_description::FormatItem, macros::format_description, UtcOffset}; #[derive(Debug, Clone, Copy)] @@ -52,6 +54,7 @@ pub enum ThreadLogMode { } #[derive(Debug, Clone)] +#[cfg(feature = "time")] pub(crate) enum TimeFormat { Rfc2822, Rfc3339, @@ -70,6 +73,7 @@ pub(crate) enum TimeFormat { /// Construct using [`Default`](Config::default) or using [`ConfigBuilder`] #[derive(Debug, Clone)] pub struct Config { + #[cfg(feature = "time")] pub(crate) time: LevelFilter, pub(crate) level: LevelFilter, pub(crate) level_padding: LevelPadding, @@ -79,7 +83,9 @@ pub struct Config { pub(crate) target: LevelFilter, pub(crate) target_padding: TargetPadding, pub(crate) location: LevelFilter, + #[cfg(feature = "time")] pub(crate) time_format: TimeFormat, + #[cfg(feature = "time")] pub(crate) time_offset: UtcOffset, pub(crate) filter_allow: Cow<'static, [Cow<'static, str>]>, pub(crate) filter_ignore: Cow<'static, [Cow<'static, str>]>, @@ -118,6 +124,7 @@ impl ConfigBuilder { } /// Set at which level and above (more verbose) the current time shall be logged (default is Error) + #[cfg(feature = "time")] pub fn set_time_level(&mut self, time: LevelFilter) -> &mut ConfigBuilder { self.0.time = time; self @@ -191,6 +198,7 @@ impl ConfigBuilder { /// .set_time_format_custom(format_description!("[hour]:[minute]:[second].[subsecond]")) /// .build(); /// ``` + #[cfg(feature = "time")] pub fn set_time_format_custom( &mut self, time_format: &'static [FormatItem<'static>], @@ -200,18 +208,21 @@ impl ConfigBuilder { } /// Set time format string to use rfc2822. + #[cfg(feature = "time")] pub fn set_time_format_rfc2822(&mut self) -> &mut ConfigBuilder { self.0.time_format = TimeFormat::Rfc2822; self } /// Set time format string to use rfc3339. + #[cfg(feature = "time")] pub fn set_time_format_rfc3339(&mut self) -> &mut ConfigBuilder { self.0.time_format = TimeFormat::Rfc3339; self } /// Set offset used for logging time (default is UTC) + #[cfg(feature = "time")] pub fn set_time_offset(&mut self, offset: UtcOffset) -> &mut ConfigBuilder { self.0.time_offset = offset; self @@ -224,7 +235,7 @@ impl ConfigBuilder { /// This may be the case, when the program is multi-threaded by the time of calling this function. /// One can opt-out of this behavior by setting `RUSTFLAGS="--cfg unsound_local_offset"`. /// Doing so is not recommended, completely untested and may cause unexpected segfaults. - #[cfg(feature = "local-offset")] + #[cfg(all(feature = "time", feature = "local-offset"))] pub fn set_time_offset_to_local(&mut self) -> Result<&mut ConfigBuilder, &mut ConfigBuilder> { match UtcOffset::current_local_offset() { Ok(offset) => { @@ -315,6 +326,7 @@ impl Default for ConfigBuilder { impl Default for Config { fn default() -> Config { Config { + #[cfg(feature = "time")] time: LevelFilter::Error, level: LevelFilter::Error, level_padding: LevelPadding::Off, @@ -324,7 +336,9 @@ impl Default for Config { target: LevelFilter::Debug, target_padding: TargetPadding::Off, location: LevelFilter::Trace, + #[cfg(feature = "time")] time_format: TimeFormat::Custom(format_description!("[hour]:[minute]:[second]")), + #[cfg(feature = "time")] time_offset: UtcOffset::UTC, filter_allow: Cow::Borrowed(&[]), filter_ignore: Cow::Borrowed(&[]), diff --git a/src/lib.rs b/src/lib.rs index 51c38787..c589e406 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,9 +25,11 @@ mod config; mod loggers; pub use self::config::{ - format_description, Config, ConfigBuilder, FormatItem, LevelPadding, TargetPadding, + Config, ConfigBuilder, LevelPadding, TargetPadding, ThreadLogMode, ThreadPadding, }; +#[cfg(feature = "time")] +pub use self::config::{format_description, FormatItem}; #[cfg(feature = "test")] pub use self::loggers::TestLogger; pub use self::loggers::{CombinedLogger, SimpleLogger, WriteLogger}; @@ -106,8 +108,13 @@ mod tests { let mut vec = Vec::new(); let mut conf_builder = ConfigBuilder::new(); - let conf_thread_name = ConfigBuilder::new() - .set_time_level(LevelFilter::Off) + let mut conf_thread_name = ConfigBuilder::new(); + + #[cfg(feature = "time")] + let mut conf_thread_name = conf_thread_name + .set_time_level(LevelFilter::Off); + + let conf_thread_name = conf_thread_name .set_thread_level(LevelFilter::Error) .set_thread_mode(ThreadLogMode::Names) .build(); @@ -129,9 +136,12 @@ mod tests { let conf = conf_builder .set_location_level(elem) .set_target_level(elem) - .set_max_level(elem) - .set_time_level(elem) - .build(); + .set_max_level(elem); + + #[cfg(feature = "time")] + let conf = conf.set_time_level(elem); + + let conf = conf.build(); i += 1; //error diff --git a/src/loggers/logging.rs b/src/loggers/logging.rs index a09bff3d..6e980dfd 100644 --- a/src/loggers/logging.rs +++ b/src/loggers/logging.rs @@ -1,4 +1,6 @@ -use crate::config::{TargetPadding, TimeFormat}; +use crate::config::TargetPadding; +#[cfg(feature = "time")] +use crate::config::TimeFormat; use crate::{Config, LevelPadding, ThreadLogMode, ThreadPadding}; use log::{LevelFilter, Record}; use std::io::{Error, Write}; @@ -30,6 +32,7 @@ where return Ok(()); } + #[cfg(feature = "time")] if config.time <= record.level() && config.time != LevelFilter::Off { write_time(write, config)?; } @@ -61,6 +64,7 @@ where } #[inline(always)] +#[cfg(feature = "time")] pub fn write_time(write: &mut W, config: &Config) -> Result<(), Error> where W: Write + Sized, diff --git a/src/loggers/termlog.rs b/src/loggers/termlog.rs index f2a17e44..474dff74 100644 --- a/src/loggers/termlog.rs +++ b/src/loggers/termlog.rs @@ -133,6 +133,7 @@ impl TermLogger { #[cfg(not(feature = "ansi_term"))] let color = self.config.level_color[record.level() as usize]; + #[cfg(feature = "time")] if self.config.time <= record.level() && self.config.time != LevelFilter::Off { write_time(term_lock, &self.config)?; }