quicklog_clock/
lib.rs

1//! `Clock` dictates how timestamps are done in the Quicklog.
2//! The idea is to use TSC time, storing a start TSC time, and a start System time.
3//!
4//! We store TSC time when logging is done in Quicklog, only getting the System
5//! time on the writer thread that is performance sensitive, as we would
6//! be able to decode the true System time, given the delta between
7//! `Instant` of generating the log line and `Instant` of the start time to get a
8//! `Duration`, which can be added to start System time to give a final `DateTime<Utc>`.
9//!
10//! Here's an example of how things are done in time taking.
11//!
12//! ```rust no_run
13//! use std::thread;
14//! use quicklog_clock::{Clock, quanta::QuantaClock};
15//!
16//! // initialize the clock, impls `Clock` trait
17//! let clock = QuantaClock::new();
18//!
19//! let some_log_line_instant = clock.get_instant();
20//! // add log_line onto some queue
21//!
22//! // simulate flush thread
23//! let flush_thread = thread::spawn(move || {
24//!     // some code to flush log lines
25//!     let actual_system_time = clock.compute_system_time_from_instant(some_log_line_instant);
26//! });
27//!
28//! # flush_thread.join();
29//! ```
30
31use ::quanta::Instant;
32use chrono::{DateTime, OutOfRangeError, Utc};
33
34pub mod quanta;
35
36pub trait Clock {
37    /// Returns current tsc instant
38    fn get_instant(&self) -> Instant;
39    /// Returns system time from TSC time
40    fn compute_system_time_from_instant(
41        &self,
42        instant: Instant,
43    ) -> Result<DateTime<Utc>, OutOfRangeError>;
44}