Skip to content

Commit

Permalink
make chrono optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jb-alvarado committed Jul 2, 2023
1 parent 5ee5a04 commit f7c7a36
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 178 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Unreleased
- The `new` method of loggers are now `#[must_use]` to prevent confusion when `new` is used called instead of `init`
## v0.12.0
- Replaces the semingly unmainted chrono library with the time crate.
- Replaces the semingly unmaintained chrono library with the time crate.
- Addresses through this update
- RUSTSEC-2020-0159 (chrono)
- RUSTSEC-2020-0071 (time)
Expand Down Expand Up @@ -45,7 +45,7 @@
## v0.8.0
- Switch from `term` to `termcolor` (PR #59. credits to @raybritton)
- Fix typo in docs (PR #58, credits to @anthonyjmartinez)
- Switch default padding to `Off`. Padding is annoyingly controversal, just set it to whatever you prefer, if you want it.
- Switch default padding to `Off`. Padding is annoyingly controversial, just set it to whatever you prefer, if you want it.

## v0.7.6
- Derive `Clone`,`Copy`,`PartialEq`,`Eq`,`Debug` and `Hash` for `TerminalMode`. (PR #56, credits to @panhania)
Expand Down Expand Up @@ -98,7 +98,7 @@
- Fixed building non-default feature sets

## v0.4.3
- Publically export TermLogger Error type
- Publicly export TermLogger Error type

## v0.4.2
- Removed a debug println! statement
Expand Down Expand Up @@ -131,7 +131,7 @@
- Removed some internal code duplication

## v0.2.0
- Local changes that (accidentially) made it to crates.io, but not git
- Local changes that (accidentally) made it to crates.io, but not git
- Basically a worse version of *Antoni Boucher* 0.3.0 changes
- Got noticed, when he made a Pull Request

Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local-offset = ["time/local-offset"]
[dependencies]
log = { version = "0.4.*", features = ["std"] }
termcolor = { version = "1.1.*", optional = true }
paris = { version = "~1.5", optional = true }
paris = { version = "~1.5.12", optional = true }
ansi_term = { version = "0.12", optional = true }
time = { version = "0.3.7", features = ["formatting", "macros"] }
time = { version = "0.3.11", optional = true, features = ["formatting", "macros"] }
chrono = { version = "0.4.26", optional = true }
2 changes: 1 addition & 1 deletion examples/custom_colors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(all(feature = "termcolor", not(feature = "paris")))]
#[cfg(feature = "termcolor")]
use log::*;
#[cfg(feature = "termcolor")]
use simplelog::*;
Expand Down
2 changes: 1 addition & 1 deletion examples/default_colors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(all(feature = "termcolor", not(feature = "paris")))]
#[cfg(feature = "termcolor")]
use log::*;
#[cfg(feature = "termcolor")]
use simplelog::*;
Expand Down
6 changes: 1 addition & 5 deletions examples/rgb_colors.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#[cfg(all(
not(target_family = "windows"),
feature = "termcolor",
not(feature = "paris")
))]
#[cfg(all(not(target_family = "windows"), feature = "termcolor"))]
use log::*;
#[cfg(all(not(target_family = "windows"), feature = "termcolor"))]
use simplelog::*;
Expand Down
1 change: 0 additions & 1 deletion examples/usage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(not(feature = "paris"))]
use log::*;
use simplelog::*;

Expand Down
122 changes: 105 additions & 17 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
use log::Level;
use log::LevelFilter;

#[cfg(feature = "chrono")]
pub use chrono::{
offset::{FixedOffset, Local, Offset, TimeZone, Utc},
DateTime,
};
use std::borrow::Cow;
#[cfg(feature = "termcolor")]
use termcolor::Color;
#[cfg(not(feature = "chrono"))]
pub use time::{format_description::FormatItem, macros::format_description, UtcOffset};

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -52,12 +58,23 @@ pub enum ThreadLogMode {
}

#[derive(Debug, Clone)]
pub(crate) enum TimeFormat {
pub enum TimeFormat {
Rfc2822,
Rfc3339,
#[cfg(feature = "chrono")]
Custom(Cow<'static, str>),
#[cfg(not(feature = "chrono"))]
Custom(&'static [time::format_description::FormatItem<'static>]),
}

#[derive(Debug, Clone)]
pub enum TimeOffset {
#[cfg(feature = "chrono")]
Chrono(chrono::offset::FixedOffset),
#[cfg(not(feature = "chrono"))]
Time(time::UtcOffset),
}

/// Configuration for the Loggers
///
/// All loggers print the message in the following form:
Expand All @@ -79,13 +96,18 @@ pub struct Config {
pub(crate) target: LevelFilter,
pub(crate) target_padding: TargetPadding,
pub(crate) location: LevelFilter,
pub(crate) module: LevelFilter,
#[cfg(feature = "chrono")]
pub(crate) time_local: bool,
pub(crate) time_format: TimeFormat,
pub(crate) time_offset: UtcOffset,
pub(crate) time_offset: TimeOffset,
pub(crate) filter_allow: Cow<'static, [Cow<'static, str>]>,
pub(crate) filter_ignore: Cow<'static, [Cow<'static, str>]>,
#[cfg(feature = "termcolor")]
pub(crate) level_color: [Option<Color>; 6],
pub(crate) write_log_enable_colors: bool,
#[cfg(feature = "paris")]
pub(crate) enable_paris_formatting: bool,
}

/// Builder for the Logger Configurations (`Config`)
Expand Down Expand Up @@ -147,6 +169,12 @@ impl ConfigBuilder {
self
}

/// Set at which level and above (more verbose) a module shall be logged (default is Off)
pub fn set_module_level(&mut self, module: LevelFilter) -> &mut ConfigBuilder {
self.0.module = module;
self
}

/// Set how the levels should be padded, when logging (default is Off)
pub fn set_level_padding(&mut self, padding: LevelPadding) -> &mut ConfigBuilder {
self.0.level_padding = padding;
Expand All @@ -173,6 +201,27 @@ impl ConfigBuilder {
self
}

/// Set time chrono [strftime] format string.
///
/// [strftime]: https://docs.rs/chrono/0.4.0/chrono/format/strftime/index.html#specifiers



// #[cfg(feature = "chrono")]
// pub fn set_time_format_str(&mut self, time_format: &'static str) -> &mut ConfigBuilder {
// self.0.time_format = TimeFormat::Custom(Cow::Borrowed(time_format));
// self
// }

// /// Set time chrono [strftime] format string.
// ///
// /// [strftime]: https://docs.rs/chrono/0.4.0/chrono/format/strftime/index.html#specifiers
// #[cfg(feature = "chrono")]
// pub fn set_time_format(&mut self, time_format: String) -> &mut ConfigBuilder {
// self.0.time_format = TimeFormat::Custom(Cow::Owned(time_format));
// self
// }

/// Sets the time format to a custom representation.
///
/// The easiest way to satisfy the static lifetime of the argument is to directly use the
Expand All @@ -191,6 +240,16 @@ impl ConfigBuilder {
/// .set_time_format_custom(format_description!("[hour]:[minute]:[second].[subsecond]"))
/// .build();
/// ```
#[cfg(feature = "chrono")]
pub fn set_time_format_custom(
&mut self,
time_format: Cow<'static, str>,
) -> &mut ConfigBuilder {
self.0.time_format = TimeFormat::Custom(time_format);
self
}

#[cfg(not(feature = "chrono"))]
pub fn set_time_format_custom(
&mut self,
time_format: &'static [FormatItem<'static>],
Expand All @@ -212,8 +271,15 @@ impl ConfigBuilder {
}

/// Set offset used for logging time (default is UTC)
pub fn set_time_offset(&mut self, offset: UtcOffset) -> &mut ConfigBuilder {
self.0.time_offset = offset;
pub fn set_time_offset(&mut self, time_offset: TimeOffset) -> &mut ConfigBuilder {
self.0.time_offset = time_offset;
self
}

/// set if you log in local timezone or UTC (default is UTC)
#[cfg(feature = "chrono")]
pub fn set_time_to_local(&mut self, local: bool) -> &mut ConfigBuilder {
self.0.time_local = local;
self
}

Expand All @@ -224,6 +290,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(not(feature = "chrono"))]
#[cfg(feature = "local-offset")]
pub fn set_time_offset_to_local(&mut self) -> Result<&mut ConfigBuilder, &mut ConfigBuilder> {
match UtcOffset::current_local_offset() {
Expand All @@ -242,8 +309,17 @@ impl ConfigBuilder {
self
}

/// Add allowed module filters.
/// If any are specified, only records from modules starting with one of these entries will be printed
/// set if you want paris formatting to be applied to this logger (default is On)
///
/// If disabled, paris markup and formatting will be stripped.
#[cfg(feature = "paris")]
pub fn set_enable_paris_formatting(&mut self, enable_formatting: bool) -> &mut ConfigBuilder {
self.0.enable_paris_formatting = enable_formatting;
self
}

/// Add allowed target filters.
/// If any are specified, only records from targets matching one of these entries will be printed
///
/// For example, `add_filter_allow_str("tokio::uds")` would allow only logging from the `tokio` crates `uds` module.
pub fn add_filter_allow_str(&mut self, filter_allow: &'static str) -> &mut ConfigBuilder {
Expand All @@ -253,26 +329,26 @@ impl ConfigBuilder {
self
}

/// Add allowed module filters.
/// If any are specified, only records from modules starting with one of these entries will be printed
/// Add allowed target filters.
/// If any are specified, only records from targets matching one of these entries will be printed
///
/// For example, `add_filter_allow(format!("{}{}","tokio", "uds"))` would allow only logging from the `tokio` crates `uds` module.
/// For example, `add_filter_allow(format!("{}::{}","tokio", "uds"))` would allow only logging from the `tokio` crates `uds` module.
pub fn add_filter_allow(&mut self, filter_allow: String) -> &mut ConfigBuilder {
let mut list = Vec::from(&*self.0.filter_allow);
list.push(Cow::Owned(filter_allow));
self.0.filter_allow = Cow::Owned(list);
self
}

/// Clear allowed module filters.
/// Clear allowed target filters.
/// If none are specified, nothing is filtered out
pub fn clear_filter_allow(&mut self) -> &mut ConfigBuilder {
self.0.filter_allow = Cow::Borrowed(&[]);
self
}

/// Add denied module filters.
/// If any are specified, records from modules starting with one of these entries will be ignored
/// Add denied target filters.
/// If any are specified, records from targets matching one of these entries will be ignored
///
/// For example, `add_filter_ignore_str("tokio::uds")` would deny logging from the `tokio` crates `uds` module.
pub fn add_filter_ignore_str(&mut self, filter_ignore: &'static str) -> &mut ConfigBuilder {
Expand All @@ -282,18 +358,18 @@ impl ConfigBuilder {
self
}

/// Add denied module filters.
/// If any are specified, records from modules starting with one of these entries will be ignored
/// Add denied target filters.
/// If any are specified, records from targets matching one of these entries will be ignored
///
/// For example, `add_filter_ignore(format!("{}{}","tokio", "uds"))` would deny logging from the `tokio` crates `uds` module.
/// For example, `add_filter_ignore(format!("{}::{}","tokio", "uds"))` would deny logging from the `tokio` crates `uds` module.
pub fn add_filter_ignore(&mut self, filter_ignore: String) -> &mut ConfigBuilder {
let mut list = Vec::from(&*self.0.filter_ignore);
list.push(Cow::Owned(filter_ignore));
self.0.filter_ignore = Cow::Owned(list);
self
}

/// Clear ignore module filters.
/// Clear ignore target filters.
/// If none are specified, nothing is filtered
pub fn clear_filter_ignore(&mut self) -> &mut ConfigBuilder {
self.0.filter_ignore = Cow::Borrowed(&[]);
Expand Down Expand Up @@ -324,8 +400,17 @@ impl Default for Config {
target: LevelFilter::Debug,
target_padding: TargetPadding::Off,
location: LevelFilter::Trace,
module: LevelFilter::Off,
#[cfg(feature = "chrono")]
time_local: false,
#[cfg(feature = "chrono")]
time_format: TimeFormat::Custom(Cow::Borrowed("%H:%M:%S")),
#[cfg(not(feature = "chrono"))]
time_format: TimeFormat::Custom(format_description!("[hour]:[minute]:[second]")),
time_offset: UtcOffset::UTC,
#[cfg(feature = "chrono")]
time_offset: TimeOffset::Chrono(FixedOffset::east_opt(0).unwrap()),
#[cfg(not(feature = "chrono"))]
time_offset: TimeOffset::Time(UtcOffset::UTC),
filter_allow: Cow::Borrowed(&[]),
filter_ignore: Cow::Borrowed(&[]),
write_log_enable_colors: false,
Expand All @@ -339,6 +424,9 @@ impl Default for Config {
Some(Color::Cyan), // Debug
Some(Color::White), // Trace
],

#[cfg(feature = "paris")]
enable_paris_formatting: true,
}
}
}
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
mod config;
mod loggers;

#[cfg(feature = "chrono")]
pub use self::config::{
Config, ConfigBuilder, LevelPadding, TargetPadding, ThreadLogMode, ThreadPadding,
};

#[cfg(not(feature = "chrono"))]
pub use self::config::{
format_description, Config, ConfigBuilder, FormatItem, LevelPadding, TargetPadding,
ThreadLogMode, ThreadPadding,
Expand All @@ -39,15 +45,12 @@ pub use termcolor::{Color, ColorChoice};
pub use log::{Level, LevelFilter};

use log::Log;
#[cfg(all(test, not(feature = "paris")))]
#[cfg(test)]
use log::*;

#[cfg(feature = "paris")]
pub(crate) mod paris_macros;
#[cfg(feature = "paris")]
#[doc(hidden)]
pub mod __private {
pub use log;
pub use paris;
}

Expand Down Expand Up @@ -118,7 +121,7 @@ mod tests {
File::create("thread_naming.log").unwrap(),
) as Box<dyn SharedLogger>);

for elem in vec![
for elem in [
LevelFilter::Off,
LevelFilter::Trace,
LevelFilter::Debug,
Expand Down
Loading

0 comments on commit f7c7a36

Please sign in to comment.