Skip to content

Commit 162da7a

Browse files
committed
primitives - split logging and serde modules in util
1 parent a5f22fe commit 162da7a

File tree

4 files changed

+173
-175
lines changed

4 files changed

+173
-175
lines changed

primitives/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ pub mod market_channel;
1515
pub mod merkle_tree;
1616
pub mod sentry;
1717
pub mod targeting_tag;
18-
pub mod util;
18+
pub mod util {
19+
pub mod tests {
20+
pub mod prep_db;
21+
pub mod time;
22+
}
23+
24+
pub mod logging;
25+
pub mod serde;
26+
}
1927
pub mod validator;
2028

2129
pub use self::ad_unit::AdUnit;

primitives/src/util.rs

Lines changed: 0 additions & 174 deletions
This file was deleted.

primitives/src/util/logging.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use slog::{Drain, OwnedKVList, Record, KV};
2+
use slog_term::{
3+
timestamp_local, CompactFormatSerializer, CountingWriter, Decorator, RecordDecorator,
4+
Serializer, ThreadSafeTimestampFn,
5+
};
6+
use std::cell::RefCell;
7+
use std::{io, io::Write};
8+
9+
pub use slog_async::Async;
10+
pub use slog_term::TermDecorator;
11+
12+
pub struct PrefixedCompactFormat<D>
13+
where
14+
D: Decorator,
15+
{
16+
decorator: D,
17+
history: RefCell<Vec<(Vec<u8>, Vec<u8>)>>,
18+
fn_timestamp: Box<dyn ThreadSafeTimestampFn<Output = io::Result<()>>>,
19+
prefix: String,
20+
}
21+
22+
impl<D> Drain for PrefixedCompactFormat<D>
23+
where
24+
D: Decorator,
25+
{
26+
type Ok = ();
27+
type Err = io::Error;
28+
29+
fn log(&self, record: &Record<'_>, values: &OwnedKVList) -> Result<Self::Ok, Self::Err> {
30+
self.format_compact(record, values)
31+
}
32+
}
33+
34+
impl<D> PrefixedCompactFormat<D>
35+
where
36+
D: Decorator,
37+
{
38+
pub fn new(prefix: &str, d: D) -> PrefixedCompactFormat<D> {
39+
Self {
40+
fn_timestamp: Box::new(timestamp_local),
41+
decorator: d,
42+
history: RefCell::new(vec![]),
43+
prefix: prefix.to_owned(),
44+
}
45+
}
46+
47+
fn format_compact(&self, record: &Record<'_>, values: &OwnedKVList) -> io::Result<()> {
48+
self.decorator.with_record(record, values, |decorator| {
49+
let indent = {
50+
let mut history_ref = self.history.borrow_mut();
51+
let mut serializer = CompactFormatSerializer::new(decorator, &mut *history_ref);
52+
53+
values.serialize(record, &mut serializer)?;
54+
55+
serializer.finish()?
56+
};
57+
58+
decorator.start_whitespace()?;
59+
60+
for _ in 0..indent {
61+
write!(decorator, " ")?;
62+
}
63+
64+
let comma_needed =
65+
print_msg_header(&self.prefix, &*self.fn_timestamp, decorator, record)?;
66+
{
67+
let mut serializer = Serializer::new(decorator, comma_needed, false);
68+
69+
record.kv().serialize(record, &mut serializer)?;
70+
71+
serializer.finish()?;
72+
}
73+
74+
decorator.start_whitespace()?;
75+
writeln!(decorator)?;
76+
77+
decorator.flush()?;
78+
79+
Ok(())
80+
})
81+
}
82+
}
83+
84+
pub fn print_msg_header(
85+
prefix: &str,
86+
fn_timestamp: &dyn ThreadSafeTimestampFn<Output = io::Result<()>>,
87+
mut rd: &mut dyn RecordDecorator,
88+
record: &Record<'_>,
89+
) -> io::Result<bool> {
90+
rd.start_timestamp()?;
91+
fn_timestamp(&mut rd)?;
92+
93+
rd.start_whitespace()?;
94+
write!(rd, " ")?;
95+
96+
rd.start_level()?;
97+
write!(rd, "{}", record.level().as_short_str())?;
98+
99+
rd.start_whitespace()?;
100+
write!(rd, " ")?;
101+
102+
rd.start_msg()?;
103+
write!(rd, "{}:", prefix)?;
104+
105+
rd.start_whitespace()?;
106+
write!(rd, " ")?;
107+
108+
rd.start_msg()?;
109+
let mut count_rd = CountingWriter::new(&mut rd);
110+
write!(count_rd, "{}", record.msg())?;
111+
Ok(count_rd.count() != 0)
112+
}

primitives/src/util/serde.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
pub mod ts_milliseconds_option {
2+
use chrono::serde::ts_milliseconds::deserialize as from_ts_milliseconds;
3+
use chrono::serde::ts_milliseconds::serialize as to_ts_milliseconds;
4+
use chrono::{DateTime, Utc};
5+
use serde::{de, Serializer};
6+
use std::fmt;
7+
8+
pub fn serialize<S>(opt: &Option<DateTime<Utc>>, serializer: S) -> Result<S::Ok, S::Error>
9+
where
10+
S: Serializer,
11+
{
12+
match *opt {
13+
Some(ref dt) => to_ts_milliseconds(dt, serializer),
14+
None => serializer.serialize_none(),
15+
}
16+
}
17+
18+
pub fn deserialize<'de, D>(de: D) -> Result<Option<DateTime<Utc>>, D::Error>
19+
where
20+
D: de::Deserializer<'de>,
21+
{
22+
Ok(de
23+
.deserialize_option(OptionMilliSecondsTimestampVisitor)
24+
.map(|opt| opt.map(|dt| dt.with_timezone(&Utc))))?
25+
}
26+
27+
struct OptionMilliSecondsTimestampVisitor;
28+
29+
impl<'de> de::Visitor<'de> for OptionMilliSecondsTimestampVisitor {
30+
type Value = Option<DateTime<Utc>>;
31+
32+
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
33+
formatter.write_str("a unix timestamp in milliseconds or none")
34+
}
35+
36+
/// Deserialize a timestamp in seconds since the epoch
37+
fn visit_none<E>(self) -> Result<Option<DateTime<Utc>>, E>
38+
where
39+
E: de::Error,
40+
{
41+
Ok(None)
42+
}
43+
44+
/// Deserialize a timestamp in seconds since the epoch
45+
fn visit_some<D>(self, de: D) -> Result<Option<DateTime<Utc>>, D::Error>
46+
where
47+
D: de::Deserializer<'de>,
48+
{
49+
from_ts_milliseconds(de).map(Some)
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)