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
178
179
180
181
use std::marker::PhantomData;
use std::time::Instant;
use http::StatusCode;
use ::filter::{Filter, WrapSealed};
use ::reject::Reject;
use ::reply::Reply;
use ::route;
use self::internal::{WithLog};
pub fn log(name: &'static str) -> Log<impl Fn(Info) + Copy> {
let func = move |info: Info| {
route::with(|route| {
info!(
target: name,
"\"{} {} {:?}\" {} {:?}",
route.method(),
route.full_path(),
route.version(),
info.status.as_u16(),
info.start.elapsed(),
);
});
};
Log {
func,
}
}
#[derive(Clone, Copy, Debug)]
pub struct Log<F> {
func: F,
}
#[allow(missing_debug_implementations)]
pub struct Info<'a> {
start: Instant,
status: StatusCode,
_marker: PhantomData<&'a ()>,
}
impl<FN, F> WrapSealed<F> for Log<FN>
where
FN: Fn(Info) + Clone + Send,
F: Filter + Clone + Send,
F::Extract: Reply,
F::Error: Reject,
{
type Wrapped = WithLog<FN, F>;
fn wrap(&self, filter: F) -> Self::Wrapped {
WithLog {
filter,
log: self.clone(),
}
}
}
mod internal {
use std::marker::PhantomData;
use std::time::Instant;
use futures::{Async, Future, Poll};
use ::filter::{FilterBase, Filter};
use ::reject::Reject;
use ::reply::{Reply, ReplySealed, Response};
use super::{Info, Log};
#[allow(missing_debug_implementations)]
pub struct Logged(pub(super) Response);
impl ReplySealed for Logged {
#[inline]
fn into_response(self) -> Response {
self.0
}
}
#[allow(missing_debug_implementations)]
#[derive(Clone, Copy)]
pub struct WithLog<FN, F> {
pub(super) filter: F,
pub(super) log: Log<FN>,
}
impl<FN, F> FilterBase for WithLog<FN, F>
where
FN: Fn(Info) + Clone + Send,
F: Filter + Clone + Send,
F::Extract: Reply,
F::Error: Reject,
{
type Extract = (Logged,);
type Error = F::Error;
type Future = WithLogFuture<FN, F::Future>;
fn filter(&self) -> Self::Future {
let started = Instant::now();
WithLogFuture {
log: self.log.clone(),
future: self.filter.filter(),
started,
}
}
}
#[allow(missing_debug_implementations)]
pub struct WithLogFuture<FN, F> {
log: Log<FN>,
future: F,
started: Instant,
}
impl<FN, F> Future for WithLogFuture<FN, F>
where
FN: Fn(Info),
F: Future,
F::Item: Reply,
F::Error: Reject,
{
type Item = (Logged,);
type Error = F::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let (result, status) = match self.future.poll() {
Ok(Async::Ready(reply)) => {
let resp = reply.into_response();
let status = resp.status();
(Ok(Async::Ready((Logged(resp),))), status)
},
Ok(Async::NotReady) => return Ok(Async::NotReady),
Err(reject) => {
let status = reject.status();
(Err(reject), status)
},
};
(self.log.func)(Info {
start: self.started,
status,
_marker: PhantomData,
});
result
}
}
}