summaryrefslogtreecommitdiff
path: root/rust/src/apachelog.rs
blob: 2517979ba8a333fac7c44abb1de1199ac9d4c9ed (plain)
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
///! Prints logging similar to apache http access.log
use std::net::IpAddr;
use conduit::{Request, Response};
use time;

pub struct LogEntry {
    remote_ip_address: IpAddr,
    remote_user: String,
    request_path: String,
    time: time::Tm,
    status: u32,
    response_size: u32,
}

impl LogEntry {
    pub fn start(req: &Request) -> LogEntry {
        let entry = LogEntry {
            remote_ip_address: req.remote_addr().ip(),
            remote_user: String::new(),
            request_path: String::from(req.path()),
            time: time::now(),
            status: 0,
            response_size: 0,
        };
        return entry
    }

    pub fn done(&mut self, response: Response) ->  Response {
        let (status_code, _) = response.status;
        self.status = status_code;
        self.print();
        return response;
    }

    #[inline(always)]
    fn print(&self) {
        println!("{} - {} - [{}] \"{}\" {} {}",
                 self.remote_ip_address,
                 self.remote_user,
                 time::strftime("%d/%b/%Y %T %z", &self.time).unwrap(),
                 self.request_path,
                 self.status,
                 self.response_size);
    }
}