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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::string::String;
use std::collections::BTreeMap;
use std::convert::Into;
use rustc_serialize::{Encodable,Encoder};
#[allow(non_camel_case_types)]
pub type time_t = i64;
#[allow(non_camel_case_types)]
pub type pid_t = i32;
#[allow(non_camel_case_types)]
pub type msgid_t = String;
pub type MessageType = String;
use severity;
use facility;
#[derive(Clone,Debug,PartialEq,Eq)]
pub enum ProcIdType {
PID(pid_t),
Name(String)
}
impl Encodable for ProcIdType {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>
{
match *self {
ProcIdType::PID(ref p) => s.emit_i32(*p),
ProcIdType::Name(ref n) => s.emit_str(n)
}
}
}
pub type SDIDType = String;
pub type SDParamIDType = String;
pub type SDParamValueType = String;
pub type StructuredDataElement = BTreeMap<SDParamIDType, SDParamValueType>;
#[derive(Clone,Debug,PartialEq,Eq)]
pub struct StructuredData {
elements: BTreeMap<SDIDType, StructuredDataElement>,
}
impl Encodable for StructuredData {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>
{
self.elements.encode(s)
}
}
impl StructuredData {
pub fn new_empty() -> StructuredData
{
StructuredData {
elements: BTreeMap::new()
}
}
pub fn insert_tuple<SI, SPI, SPV> (&mut self, sd_id: SI, sd_param_id: SPI, sd_param_value: SPV) -> ()
where SI: Into<SDIDType>, SPI: Into<SDParamIDType>, SPV: Into<SDParamValueType>
{
let mut sub_map = self.elements.entry(sd_id.into()).or_insert(BTreeMap::new());
sub_map.insert(sd_param_id.into(), sd_param_value.into());
}
pub fn find_tuple<'b>(&'b self, sd_id: &str, sd_param_id: &str) -> Option<&'b SDParamValueType>
{
if let Some(sub_map) = self.elements.get(sd_id) {
if let Some(value) = sub_map.get(sd_param_id) {
Some(value)
} else {
None
}
} else {
None
}
}
pub fn find_sdid<'b>(&'b self, sd_id: &str) -> Option<&'b BTreeMap<SDParamIDType, SDParamValueType>>
{
self.elements.get(sd_id)
}
pub fn len(&self) -> usize
{
self.elements.len()
}
}
#[derive(Clone,Debug,RustcEncodable)]
pub struct SyslogMessage {
pub severity: severity::SyslogSeverity,
pub facility: facility::SyslogFacility,
pub version: i32,
pub timestamp: Option<time_t>,
pub hostname: Option<String>,
pub appname: Option<String>,
pub procid: Option<ProcIdType>,
pub msgid: Option<msgid_t>,
pub sd: StructuredData,
pub msg: String,
}
#[cfg(test)]
mod tests {
use rustc_serialize::json;
use super::{StructuredData,SyslogMessage};
use severity::SyslogSeverity::*;
use facility::SyslogFacility::*;
#[test]
fn test_structured_data_basic() {
let mut s = StructuredData::new_empty();
s.insert_tuple("foo", "bar", "baz");
let v = s.find_tuple("foo", "bar").expect("should find foo/bar");
assert_eq!(v, "baz");
assert!(s.find_tuple("foo", "baz").is_none());
}
#[test]
fn test_structured_data_serialization() {
let mut s = StructuredData::new_empty();
s.insert_tuple("foo", "bar", "baz");
s.insert_tuple("foo", "baz", "bar");
s.insert_tuple("faa", "bar", "baz");
let encoded = json::encode(&s).expect("Should encode to JSON");
assert_eq!(encoded, r#"{"faa":{"bar":"baz"},"foo":{"bar":"baz","baz":"bar"}}"#);
}
#[test]
fn test_serialization() {
let m = SyslogMessage {
severity: SEV_INFO,
facility: LOG_KERN,
version: 1,
timestamp: None,
hostname: None,
appname: None,
procid: None,
msgid: None,
sd: StructuredData::new_empty(),
msg: String::from("")
};
let encoded = json::encode(&m).expect("Should encode to JSON");
println!("{:?}", encoded);
assert_eq!(encoded, "{\"severity\":\"info\",\"facility\":\"kern\",\"version\":1,\"timestamp\":null,\"hostname\":null,\"appname\":null,\"procid\":null,\"msgid\":null,\"sd\":{},\"msg\":\"\"}");
}
}