forked from slog-rs/term
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use chrono to detect local timezone thread-safely
As discussed in PR slog-rs#44, the 'time' crate fails timezine detection in the prsesense of multiple threads, because the underlying POSIX function localtime_r is not thread safe (at least not on Linux). In order to avoid these thread-safety issues, we use the chrono crate. It avoids system libraries and reimplements timezone detection from scratch. (Thanks to @yaozongyou for pointing this out) TODO: Maybe we should switch to chono entirely. It seems to be a fairly complete replacement. What are the advantages & disadvantages?
- Loading branch information
Showing
3 changed files
with
124 additions
and
6 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//! Timestamp logic for slog-term | ||
/// A threadsafe timestamp formatter | ||
/// | ||
/// To satify `slog-rs` thread and unwind safety requirements, the | ||
/// bounds expressed by this trait need to satisfied for a function | ||
/// to be used in timestamp formatting. | ||
pub trait TimestampWriter: Sealed + Send + Sync + UnwindSafe + RefUnwindSafe + 'static { | ||
fn write_timestamp(&self, writer: &mut dyn io::Write) -> io::Result<()>; | ||
} | ||
|
||
|
||
impl<F> ThreadSafeTimestampFn for F | ||
where | ||
F: Fn(&mut dyn io::Write) -> io::Result<()> + Send + Sync, | ||
F: UnwindSafe + RefUnwindSafe + 'static, | ||
F: ?Sized, | ||
{} | ||
|
||
mod sealed { | ||
pub trait Sealed {} | ||
} |