use 'chrono' for grabbing and printing timestamps in log

This commit is contained in:
Milo Turner 2020-03-09 12:23:19 -04:00
parent ca8a1b6884
commit e687f03227
4 changed files with 77 additions and 9 deletions

54
Cargo.lock generated
View File

@ -1,5 +1,11 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]]
name = "bitflags"
version = "1.2.1"
@ -27,6 +33,17 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "chrono"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2"
dependencies = [
"num-integer",
"num-traits",
"time",
]
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
@ -58,6 +75,7 @@ dependencies = [
name = "hptp"
version = "0.1.0"
dependencies = [
"chrono",
"rand",
"thiserror",
"tokio",
@ -169,6 +187,25 @@ dependencies = [
"winapi 0.3.8",
]
[[package]]
name = "num-integer"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
dependencies = [
"autocfg",
]
[[package]]
name = "pin-project-lite"
version = "0.1.4"
@ -240,6 +277,12 @@ dependencies = [
"rand_core",
]
[[package]]
name = "redox_syscall"
version = "0.1.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
[[package]]
name = "slab"
version = "0.4.2"
@ -277,6 +320,17 @@ dependencies = [
"syn",
]
[[package]]
name = "time"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
dependencies = [
"libc",
"redox_syscall",
"winapi 0.3.8",
]
[[package]]
name = "tokio"
version = "0.2.13"

View File

@ -10,4 +10,5 @@ edition = "2018"
[dependencies]
tokio = {version = "0.2.*", features = ["io-std", "io-util", "udp"]}
rand = "0.7.*"
thiserror = "*"
thiserror = "*"
chrono = "0.4.*"

View File

@ -1,6 +1,7 @@
extern crate rand;
#[macro_use]
extern crate thiserror;
extern crate chrono;
pub mod logger;
pub mod msg;

View File

@ -1,7 +1,6 @@
extern crate tokio;
use std::fmt::Display;
use std::time::SystemTime;
use tokio::io::{AsyncWriteExt, Stderr};
pub struct Logger {
@ -26,11 +25,7 @@ impl Logger {
}
async fn log_payload(&mut self, payload: LogPayload<'_>) {
self.log_message(LogMessage {
timestamp: SystemTime::now(),
payload,
})
.await
self.log_message(LogMessage::now(payload)).await
}
// -----------------------------------------------------------------------------------
@ -71,10 +66,19 @@ impl Logger {
}
struct LogMessage<'a> {
pub timestamp: SystemTime,
pub timestamp: chrono::DateTime<chrono::Local>,
pub payload: LogPayload<'a>,
}
impl<'a> LogMessage<'a> {
fn now(payload: LogPayload<'a>) -> Self {
LogMessage {
timestamp: chrono::Local::now(),
payload,
}
}
}
enum LogPayload<'a> {
Debug {
what: &'a str,
@ -109,7 +113,15 @@ pub use AcceptedOrder::*;
impl Display for LogMessage<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
// TODO: write timestamp
use chrono::Timelike;
write!(
f,
"{:02}:{:02}:{:02} {:03.3} ",
self.timestamp.hour(),
self.timestamp.minute(),
self.timestamp.second(),
self.timestamp.nanosecond() as f64 * 0.000001
)?;
self.payload.fmt(f)
}
}