New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make chrono optional #109
Draft
jb-alvarado
wants to merge
2
commits into
Drakulix:master
Choose a base branch
from
jb-alvarado:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
make chrono optional #109
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)] | ||
|
@@ -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: | ||
|
@@ -80,8 +97,10 @@ pub struct Config { | |
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")] | ||
|
@@ -200,6 +219,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>], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So instead of adding |
||
|
@@ -221,8 +250,8 @@ 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 | ||
} | ||
|
||
|
@@ -233,6 +262,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() { | ||
|
@@ -343,8 +373,16 @@ impl Default for Config { | |
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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then only the
time_local
field would be specific to chrono (and I don't really see a way to remove it).