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
177
use actix::prelude::*;
use std::fmt;
use std::time::Duration;
use std::time::SystemTime;
use chrono::offset::Utc;
use chrono::DateTime;
use serde_json;
#[macro_export]
macro_rules! audit_log {
($audit:expr, $($arg:tt)*) => ({
use std::fmt;
if cfg!(test) || cfg!(debug_assertions) {
debug!($($arg)*)
}
$audit.log_event(
fmt::format(
format_args!($($arg)*)
)
)
})
}
macro_rules! audit_segment {
($au:expr, $fun:expr) => {{
use std::time::Instant;
let start = Instant::now();
let r = $fun();
let end = Instant::now();
let diff = end.duration_since(start);
audit_log!($au, "duration -> {:?}", diff);
$au.set_duration(diff);
r
}};
}
macro_rules! try_audit {
($audit:ident, $result:expr, $logFormat:expr, $errorType:expr) => {
match $result {
Ok(v) => v,
Err(e) => {
audit_log!($audit, $logFormat, e);
return Err($errorType);
}
}
};
($audit:ident, $result:expr, $logFormat:expr) => {
match $result {
Ok(v) => v,
Err(e) => {
audit_log!($audit, $logFormat, e);
return Err(e);
}
}
};
($audit:ident, $result:expr) => {
match $result {
Ok(v) => v,
Err(e) => {
audit_log!($audit, "error @ {} {} -> {:?}", file!(), line!(), e);
return Err(e);
}
}
};
}
#[derive(Serialize, Deserialize)]
enum AuditEvent {
Log(AuditLog),
Scope(AuditScope),
}
#[derive(Debug, Serialize, Deserialize)]
struct AuditLog {
time: String,
name: String,
}
#[derive(Serialize, Deserialize)]
pub struct AuditScope {
time: String,
name: String,
duration: Option<Duration>,
events: Vec<AuditEvent>,
}
impl Message for AuditScope {
type Result = ();
}
impl fmt::Display for AuditScope {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut _depth = 0;
let d = serde_json::to_string_pretty(self).map_err(|_| fmt::Error)?;
write!(f, "{}", d)
}
}
impl AuditScope {
pub fn new(name: &str) -> Self {
let t_now = SystemTime::now();
let datetime: DateTime<Utc> = t_now.into();
AuditScope {
time: datetime.to_rfc3339(),
name: String::from(name),
duration: None,
events: Vec::new(),
}
}
pub fn id(&self) -> &str {
self.name.as_str()
}
pub fn set_duration(&mut self, diff: Duration) {
self.duration = Some(diff);
}
pub fn append_scope(&mut self, scope: AuditScope) {
self.events.push(AuditEvent::Scope(scope))
}
pub fn log_event(&mut self, data: String) {
let t_now = SystemTime::now();
let datetime: DateTime<Utc> = t_now.into();
self.events.push(AuditEvent::Log(AuditLog {
time: datetime.to_rfc3339(),
name: data,
}))
}
}
#[cfg(test)]
mod tests {
use crate::audit::AuditScope;
#[test]
fn test_audit_simple() {
let au = AuditScope::new("au");
let d = serde_json::to_string_pretty(&au).expect("Json serialise failure");
println!("{}", d);
}
}