Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
16 changes: 15 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -52,6 +54,7 @@ pub enum ThreadLogMode {
}

#[derive(Debug, Clone)]
#[cfg(feature = "time")]
pub(crate) enum TimeFormat {
Rfc2822,
Rfc3339,
Expand All @@ -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,
Expand All @@ -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>]>,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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>],
Expand All @@ -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
Expand All @@ -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) => {
Expand Down Expand Up @@ -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,
Expand All @@ -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(&[]),
Expand Down
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
mod config;
mod loggers;

#[cfg(feature = "time")]
pub use self::config::{format_description, FormatItem};
pub use self::config::{
format_description, Config, ConfigBuilder, FormatItem, LevelPadding, TargetPadding,
ThreadLogMode, ThreadPadding,
Config, ConfigBuilder, LevelPadding, TargetPadding, ThreadLogMode, ThreadPadding,
};
#[cfg(feature = "test")]
pub use self::loggers::TestLogger;
Expand Down Expand Up @@ -106,8 +107,12 @@ 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();
Expand All @@ -129,9 +134,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
Expand Down
6 changes: 5 additions & 1 deletion src/loggers/logging.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -30,6 +32,7 @@ where
return Ok(());
}

#[cfg(feature = "time")]
if config.time <= record.level() && config.time != LevelFilter::Off {
write_time(write, config)?;
}
Expand Down Expand Up @@ -61,6 +64,7 @@ where
}

#[inline(always)]
#[cfg(feature = "time")]
pub fn write_time<W>(write: &mut W, config: &Config) -> Result<(), Error>
where
W: Write + Sized,
Expand Down
1 change: 1 addition & 0 deletions src/loggers/termlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down