From 6368f25fedb1fe8d4296a2b791a2fdb3aa00bec8 Mon Sep 17 00:00:00 2001 From: Richard Whitehouse Date: Sun, 19 Nov 2017 22:12:17 +0000 Subject: [PATCH] Replace print statements with logging --- Cargo.toml | 2 ++ src/codec.rs | 46 +++++++++++++++++++++++++--------------------- src/lib.rs | 3 +++ src/parser.rs | 4 ---- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2e97f2f..1e5d3b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,6 @@ tokio-io = "0.1.3" tokio-core = "0.1.10" bytes = "0.4.5" nom = "3.2.0" +log = "0.3" +env_logger = "0.4.3" diff --git a/src/codec.rs b/src/codec.rs index c982789..8ade42c 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -25,7 +25,7 @@ impl Header { match header(line.as_bytes()) { nom::IResult::Done(_, h) => Some((h, line.clone())), result => { - println!("Failed to parse header: {:?} - {:?}", line, result); + warn!("Failed to parse header: {:?} - {:?}", line, result); None } } @@ -71,7 +71,7 @@ impl SipCodec { fn parse_message(&mut self, buf: &mut BytesMut) -> io::Result> { loop { - println!("Gathering headers from {:?}", std::str::from_utf8(&buf)); + trace!("Gathering headers from {:?}", std::str::from_utf8(&buf)); let mut done = false; let mut total = 0; @@ -80,17 +80,17 @@ impl SipCodec { let mut search: Box> = Box::new(buf.iter()); while !done { - println!("Gathering CRLF"); + trace!("Gathering CRLF"); if let Some(cr) = search.position(|&b| b == CR) { - println!("Found CR at {}", cr); + trace!("Found CR at {}", cr); total += cr; let next = search.next(); - println!("Got {:?} after CR", next); + trace!("Got {:?} after CR", next); if let Some(&LF) = next { done = true; @@ -104,32 +104,32 @@ impl SipCodec { // We've got a line terminating in \r\n. let line = buf.split_to(total + 2); - println!("Using line: {:?}", std::str::from_utf8(&line)); + trace!("Using line: {:?}", std::str::from_utf8(&line)); match (&mut self.top_line, chartype(line.first()), &mut self.latest_header) { // Ignore empty top lines (&mut None, CharType::Line, _) => { - println!("Ignoring empty top line"); + trace!("Ignoring empty top line"); } // Top line beginnning with whitespace - ignore (&mut None, CharType::Whitespace, _) => { - println!("Got top line beginning with whitespace - ignore: {:?}", + trace!("Got top line beginning with whitespace - ignore: {:?}", line); } // Top line with no headers! Discard. (top_line @ &mut None, CharType::Other, _) => { - println!("Got new top line of request: {:?}", line); + trace!("Got new top line of request: {:?}", line); top_line.get_or_insert(UnparsedLine::new(line)); } // Top line with no headers! Discard. (top_line @ &mut Some(_), CharType::Line, &mut None) => { - println!("Got request with no headers - discarding"); + warn!("Got request with no headers - discarding"); top_line.take(); } @@ -158,13 +158,13 @@ impl SipCodec { (top_line @ &mut Some(_), CharType::Line, old_header @ &mut Some(_)) => { self.headers.push(old_header.take().unwrap()); - println!("Got end of headers"); + trace!("Got end of headers"); if let Ok(new_message) = PartialMessage::new(top_line.take() .unwrap(), &mut self.headers) { - println!("Got partial message: {:?}", new_message); + debug!("Got partial message: {:?}", new_message); self.headers.clear(); @@ -172,11 +172,11 @@ impl SipCodec { if length == 0 { if let Some(message) = Message::new(new_message) { - println!("Got message without body: {:?}", message); + debug!("Got message without body: {:?}", message); return Ok(Some(message)); } else { - println!("Failed to parse message with no body!"); + debug!("Failed to parse message with no body!"); } } else if length > buf.len() { self.message.get_or_insert(new_message); @@ -184,15 +184,15 @@ impl SipCodec { } else { let body = buf.split_to(length); if let Some(message) = Message::new_with_body(new_message, body) { - println!("Got message with body: {:?}", message); + debug!("Got message with body: {:?}", message); return Ok(Some(message)); } else { - println!("Failed to parse message with body!"); + warn!("Failed to parse message with body!"); } } } else { - println!("Failed to parse partial message!"); + warn!("Failed to parse partial message!"); // Message failed to parse top_line.take(); @@ -282,7 +282,7 @@ impl Message { })) } result => { - println!("Failed to parse top line: {:?}", result); + warn!("Failed to parse top line: {:?}", result); None } } @@ -307,7 +307,7 @@ impl Decoder for SipCodec { fn decode(&mut self, buf: &mut BytesMut) -> io::Result> { if let Some(_) = self.message { - println!("Gathering body"); + trace!("Gathering body"); let length = self.message.iter().map(|m| m.body_length()).next().unwrap(); @@ -317,7 +317,7 @@ impl Decoder for SipCodec { buf.split_to(length)) { return Ok(Some(message)); } else { - println!("Failed to parse message with no body!"); + warn!("Failed to parse message with no body!"); } } @@ -386,7 +386,7 @@ impl Sip { let future = stream.for_each(move |message| caller(message)); - let future = future.map_err(|err| println!("Error {:?}", err)); + let future = future.map_err(|err| error!("Error {:?}", err)); // Spawn the future as a concurrent task handle.spawn(future); @@ -403,6 +403,8 @@ impl Sip { #[cfg(test)] mod tests { + extern crate env_logger; + use std::ops::DerefMut; use std::sync::Arc; use std::sync::Mutex; @@ -475,6 +477,8 @@ mod tests { #[test] fn test_message_decode() { + env_logger::init().unwrap(); + let message = decode_sip_message("MESSAGE sip:test.com \ SIP/2.0\r\nAccept:text/plain\r\nAccept-Encoding:\ diff --git a/src/lib.rs b/src/lib.rs index dedf171..9993435 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,9 @@ extern crate tokio_core; extern crate tokio_io; extern crate bytes; +#[macro_use] +extern crate log; + #[macro_use] extern crate nom; diff --git a/src/parser.rs b/src/parser.rs index 1052e82..06fb0d6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -65,8 +65,6 @@ macro_rules! take_1 ( use nom::Slice; let input = $input; - println!("Taking 1 from {:?}", input); - match input.iter().next() { Some(c) => { if $submac!(*c, $($args)*) { @@ -75,8 +73,6 @@ macro_rules! take_1 ( } else { - println!("Failed to take 1 from {:?} - {:?} doesn't match", input, c); - nom::IResult::Error(error_position!(nom::ErrorKind::TakeWhile1, input)) } }, -- 2.34.1