-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathlogging.rs
More file actions
58 lines (46 loc) · 1.53 KB
/
logging.rs
File metadata and controls
58 lines (46 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::fmt;
use std::time::Duration;
use tower_http::LatencyUnit;
/// This is the prefix used in logs to identify a started service.
///
/// For example:
///
/// ```text
/// 2024-06-25T12:36:25.025312Z INFO UDP TRACKER: Started on: udp://0.0.0.0:6969
/// 2024-06-25T12:36:25.025445Z INFO HTTP TRACKER: Started on: http://0.0.0.0:7070
/// 2024-06-25T12:36:25.025527Z INFO API: Started on: http://0.0.0.0:1212
/// 2024-06-25T12:36:25.025580Z INFO HEALTH CHECK API: Started on: http://127.0.0.1:1313
/// ```
pub const STARTED_ON: &str = "Started on";
/*
todo: we should use a field fot the URL.
For example, instead of:
```
2024-06-25T12:36:25.025312Z INFO UDP TRACKER: Started on: udp://0.0.0.0:6969
```
We should use something like:
```
2024-06-25T12:36:25.025312Z INFO UDP TRACKER started_at_url=udp://0.0.0.0:6969
```
*/
pub struct Latency {
unit: LatencyUnit,
duration: Duration,
}
impl Latency {
#[must_use]
pub fn new(unit: LatencyUnit, duration: Duration) -> Self {
Self { unit, duration }
}
}
impl fmt::Display for Latency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.unit {
LatencyUnit::Seconds => write!(f, "{} s", self.duration.as_secs_f64()),
LatencyUnit::Millis => write!(f, "{} ms", self.duration.as_millis()),
LatencyUnit::Micros => write!(f, "{} μs", self.duration.as_micros()),
LatencyUnit::Nanos => write!(f, "{} ns", self.duration.as_nanos()),
_ => panic!("Invalid latency unit"),
}
}
}