future_utils/
log_errors.rs

1use std::fmt::Display;
2use futures::{Async, Stream};
3use void::Void;
4use log::LogLevel;
5
6/// Removes the errors from a stream and logs them.
7pub struct LogErrors<S> {
8    stream: S,
9    level: LogLevel,
10    description: &'static str,
11}
12
13impl<S> LogErrors<S> {
14    pub fn new(
15        stream: S,
16        level: LogLevel,
17        description: &'static str,
18    ) -> LogErrors<S> {
19        LogErrors {
20            stream,
21            level,
22            description,
23        }
24    }
25}
26
27impl<S> Stream for LogErrors<S>
28where
29    S: Stream,
30    S::Error: Display,
31{
32    type Item = S::Item;
33    type Error = Void;
34
35    fn poll(&mut self) -> Result<Async<Option<S::Item>>, Void> {
36        match self.stream.poll() {
37            Ok(x) => Ok(x),
38            Err(e) => {
39                log!(self.level, "{}: {}", self.description, e);
40                Ok(Async::NotReady)
41            },
42        }
43    }
44}
45