From 8ceac0236c555979a9172c69c1a9e895d7487d6f Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Sat, 17 Dec 2016 15:50:27 +0100 Subject: replace all with subdirectory rust/ --- src/apachelog.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/apachelog.rs (limited to 'src/apachelog.rs') diff --git a/src/apachelog.rs b/src/apachelog.rs new file mode 100644 index 0000000..fb5d1a6 --- /dev/null +++ b/src/apachelog.rs @@ -0,0 +1,45 @@ +///! Prints logging similar to apache http access.log +use std::net::IpAddr; +use std::io::Read; + +use time; +use tiny_http::{Request, Response}; + +pub struct LogEntry { + remote_ip_address: IpAddr, + remote_user: String, + request_path: String, + time: time::Tm, + status: u16, + 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.url()), + time: time::now(), + status: 0, + response_size: 0, + }; + return entry + } + + pub fn done(&mut self, _: &Response, status_code: u16) where R: Read { + self.status = status_code; // request.statuscode is not accessible :( + self.print(); + } + + #[inline(always)] + fn print(&self) { + info!("{} - {} - [{}] \"{}\" {} {}", + 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); + } +} -- cgit v1.2.1