Replace print statements with logging
authorRichard Whitehouse <github@richardwhiuk.com>
Sun, 19 Nov 2017 22:12:17 +0000 (22:12 +0000)
committerRichard Whitehouse <github@richardwhiuk.com>
Sun, 19 Nov 2017 22:12:17 +0000 (22:12 +0000)
Cargo.toml
src/codec.rs
src/lib.rs
src/parser.rs

index 2e97f2fa9fa84992548533f96910a80148d7d298..1e5d3b8729f988096a97b3db7f97832f56386463 100644 (file)
@@ -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"
 
index c9827894fd1cb10fe5ea9ae1ace885070b2e1ed0..8ade42c150912b62bca33a947ce7433a31b2ea85 100644 (file)
@@ -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<Option<Message>> {
         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<Iterator<Item = &u8>> = 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<Option<Message>> {
         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:\
index dedf171770ec85ff3f62d2ecd04864b9c81aa40f..999343560000c55f873cd2c251da27244a581ffd 100644 (file)
@@ -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;
 
index 1052e82fcfac2bb0be43f9dc8704afc73e474f96..06fb0d6877aa00a0adb3410a0d59fc6c345b6ee7 100644 (file)
@@ -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))
           }
         },