1#![forbid(unsafe_code)]
2
3use std::cmp::Ordering;
4use std::collections::BTreeMap;
5use std::time::SystemTime;
6
7pub mod copy;
8pub mod proxy;
9pub mod serialize;
10pub mod write;
11
12#[derive(PartialEq, Eq, Clone, Copy)]
13pub enum Severity {
14 Trace,
15 Debug,
16 Info,
17 Warn,
18 Error,
19 Fatal,
20}
21
22impl PartialOrd for Severity {
23 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
24 Some(self.cmp(other))
25 }
26}
27
28impl Ord for Severity {
29 fn cmp(&self, other: &Self) -> Ordering {
30 let su: u8 = (*self).into();
31 let ou: u8 = (*other).into();
32 su.cmp(&ou)
33 }
34}
35
36impl From<u8> for Severity {
37 fn from(num: u8) -> Self {
38 match num {
39 1..=4 => Self::Trace,
40 5..=8 => Self::Debug,
41 9..=12 => Self::Info,
42 13..=16 => Self::Warn,
43 17..=20 => Self::Error,
44 21..=24 => Self::Fatal,
45 _ => Self::Fatal,
46 }
47 }
48}
49
50impl From<Severity> for u8 {
51 fn from(s: Severity) -> Self {
52 match s {
53 Severity::Trace => 1,
54 Severity::Debug => 5,
55 Severity::Info => 9,
56 Severity::Warn => 13,
57 Severity::Error => 17,
58 Severity::Fatal => 21,
59 }
60 }
61}
62
63impl Severity {
64 pub fn as_str(&self) -> &str {
65 match self {
66 Self::Trace => "trace",
67 Self::Debug => "debug",
68 Self::Info => "info",
69 Self::Warn => "warn",
70 Self::Error => "error",
71 Self::Fatal => "fatal",
72 }
73 }
74}
75
76pub struct Item {
77 pub timestamp: SystemTime,
78 pub severity: Severity,
79 pub body: String,
80 pub attributes: BTreeMap<String, String>,
81 pub resource: BTreeMap<String, String>,
82 pub trace_id: Option<String>,
83 pub span_id: Option<String>,
84}
85
86impl Item {
87 pub fn new(body: &str, attr: BTreeMap<String, String>) -> Self {
88 Self {
89 timestamp: SystemTime::now(),
90 severity: Severity::Trace,
91 body: body.into(),
92 attributes: attr,
93 resource: BTreeMap::new(),
94 trace_id: None,
95 span_id: None,
96 }
97 }
98
99 pub fn with_resource_keys(self, keys: &[&str]) -> Self {
100 let resource = BTreeMap::from_iter(keys.iter().map(|&key: &&str| (key.into(), "".into())));
101 Self {
102 timestamp: self.timestamp,
103 severity: self.severity,
104 body: self.body,
105 attributes: self.attributes,
106 resource,
107 trace_id: self.trace_id,
108 span_id: self.span_id,
109 }
110 }
111}