summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-09-10 16:02:42 -0400
committerJesse Luehrs <doy@tozt.net>2014-09-10 16:02:42 -0400
commit5bca38d9af67001ea6d406c1a447e7cc80a41e29 (patch)
tree14b22ddb5029ecf95a2855dd763c68082ca5e210
parentb457ab74c224d65b64cfb46732a5b7b5e9bc554d (diff)
downloadrust-irc-5bca38d9af67001ea6d406c1a447e7cc80a41e29.tar.gz
rust-irc-5bca38d9af67001ea6d406c1a447e7cc80a41e29.zip
move debug output into the library
-rw-r--r--examples/client.rs9
-rw-r--r--src/client.rs23
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(())
}