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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::collections::BTreeMap;

type JournalEntryFields = BTreeMap<String, String>;

#[derive(Clone, Debug)]
pub struct JournalEntry {
	pub	fields: JournalEntryFields,
}

#[derive(Clone, Debug)]
pub struct JournalEntryTimestamp {
	pub timestamp_us: i64
}

impl JournalEntry {

	pub fn new() -> JournalEntry {
		return JournalEntry {
			fields: BTreeMap::<String, String>::new(),
		};
	}

	pub fn from_fields(fields: &JournalEntryFields) -> JournalEntry {
		return JournalEntry {
			fields: fields.clone(),
		};
	}

	pub fn get_field(&self, field: &str) -> Option<&str> {
		return self.fields.get(field).map(|v| v.as_str());
	}

	pub fn get_fields(&self) -> &JournalEntryFields {
		return &self.fields;
	}

	pub fn get_message<'a>(&'a self) -> Option<&'a str> {
		return self
				.fields
				.get("MESSAGE")
				.map(|v| v.as_ref());
	}

	pub fn set_message(&mut self, msg: &str) {
		self.fields.insert(
				"MESSAGE".to_string(),
				msg.to_string());
	}

	pub fn get_wallclock_time(&self) -> Option<JournalEntryTimestamp> {
		let source_time = self.get_source_wallclock_time();
		let reception_time = self.get_reception_wallclock_time();

		return source_time.or(reception_time);
	}

	pub fn get_source_wallclock_time(&self) -> Option<JournalEntryTimestamp> {
		return self
				.fields
				.get("_SOURCE_REALTIME_TIMESTAMP")
				.and_then(|v| v.parse::<i64>().ok())
				.map(|v| JournalEntryTimestamp { timestamp_us: v });
	}

	pub fn get_reception_wallclock_time(&self) -> Option<JournalEntryTimestamp> {
		return self
				.fields
				.get("__REALTIME_TIMESTAMP")
				.and_then(|v| v.parse::<i64>().ok())
				.map(|v| JournalEntryTimestamp { timestamp_us: v });
	}

	pub fn get_monotonic_time(&self) -> Option<JournalEntryTimestamp> {
		return self.fields
				.get("__MONOTONIC_TIMESTAMP")
				.and_then(|v| v.parse::<i64>().ok())
				.map(|v| JournalEntryTimestamp { timestamp_us: v });
	}

}