From 5bca38d9af67001ea6d406c1a447e7cc80a41e29 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 10 Sep 2014 16:02:42 -0400 Subject: move debug output into the library --- examples/client.rs | 9 ++------- src/client.rs | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/examples/client.rs b/examples/client.rs index c5f31a7..db3b244 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -1,18 +1,13 @@ extern crate irc; -use std::io; - pub struct ExampleClient; impl irc::ClientCallbacks for ExampleClient { - fn on_any_message (&mut self, _client: &mut irc::Client, m: &irc::Message) -> io::IoResult<()> { - print!("{}", m.to_protocol_string()); - Ok(()) - } } fn main () { - let builder = irc::ClientBuilder::new("doytest", "chat.freenode.net"); + let mut builder = irc::ClientBuilder::new("doytest", "chat.freenode.net"); + builder.set_debug(true); let client = builder.connect(); client.run_loop_with_callbacks(ExampleClient); } diff --git a/src/client.rs b/src/client.rs index 04b2b0b..8f7ee6f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -19,6 +19,8 @@ pub struct ClientBuilder { servername: String, port: u16, + + debug: bool, } impl ClientBuilder { @@ -33,6 +35,8 @@ impl ClientBuilder { servername: servername.to_string(), port: 6667, + + debug: false, } } @@ -61,6 +65,11 @@ impl ClientBuilder { self } + pub fn set_debug (&mut self, debug: bool) -> &mut ClientBuilder { + self.debug = debug; + self + } + pub fn connect (self) -> Client { let stream = io::TcpStream::connect(self.servername.as_slice(), self.port); let mut stream = stream.unwrap(); @@ -116,13 +125,21 @@ impl Client { // XXX handle different encodings match Message::parse(String::from_utf8_lossy(buf.slice(0, len)).as_slice()) { - Ok(m) => Ok(m), + Ok(m) => { + if self.builder.debug { + print!("R {}", m.to_protocol_string()); + } + Ok(m) + }, Err(s) => Err(ParseError(s)), } } - pub fn write (&mut self, msg: Message) -> io::IoResult<()> { - try!(msg.write_protocol_string(self.conn())); + pub fn write (&mut self, m: Message) -> io::IoResult<()> { + try!(m.write_protocol_string(self.conn())); + if self.builder.debug { + print!("W {}", m.to_protocol_string()); + } Ok(()) } -- cgit v1.2.3