summaryrefslogtreecommitdiff
path: root/src/sock.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/sock.rs')
-rw-r--r--src/sock.rs55
1 files changed, 44 insertions, 11 deletions
diff --git a/src/sock.rs b/src/sock.rs
index 1724509..c4c8e8f 100644
--- a/src/sock.rs
+++ b/src/sock.rs
@@ -123,6 +123,7 @@ pub enum Auth {
}
+/// An asynchronous event received from Tor. These are returned by `read_event()`.
#[derive(Debug)]
pub enum Event {
// Log messages
@@ -134,6 +135,47 @@ pub enum Event {
}
+/// A Builder type for the argument to `setevents()`. Use `EventList::default()` to initialize a
+/// default (empty) event list.
+///
+/// ```
+/// # use torctl::EventList;
+/// let log_warn_and_error = EventList::default().warn();
+/// ```
+#[derive(Debug,Default,Clone,Copy)]
+pub struct EventList {
+ debug: bool,
+ info: bool,
+ notice: bool,
+ warn: bool,
+ err: bool,
+}
+
+impl EventList {
+ /// Enable all log events.
+ pub fn debug (mut self) -> Self { self.debug = true; self.info = true; self.notice = true; self.warn = true; self.err = true; self }
+ /// Enable all log events of level *INFO* and higher.
+ pub fn info (mut self) -> Self { self.debug = false; self.info = true; self.notice = true; self.warn = true; self.err = true; self }
+ /// Enable all log events of level *NOTICE* and higher.
+ pub fn notice(mut self) -> Self { self.debug = false; self.info = false; self.notice = true; self.warn = true; self.err = true; self }
+ /// Enable all log events of level *WARN* and higher.
+ pub fn warn (mut self) -> Self { self.debug = false; self.info = false; self.notice = false; self.warn = true; self.err = true; self }
+ /// Only enable logging for log level *ERR*.
+ pub fn err (mut self) -> Self { self.debug = false; self.info = false; self.notice = false; self.warn = false; self.err = true; self }
+
+ fn cmd(&self) -> String {
+ let mut s = "SETEVENTS".to_string();
+ if self.debug { s.push_str(" DEBUG" ) }
+ if self.info { s.push_str(" INFO" ) }
+ if self.notice { s.push_str(" NOTICE") }
+ if self.warn { s.push_str(" WARN" ) }
+ if self.err { s.push_str(" ERR" ) }
+ s.push_str("\r\n");
+ s
+ }
+}
+
+
// A double-quoted string where only \ and " are escaped.
// Encoding: QuotedString("hello").to_string()
@@ -459,17 +501,8 @@ impl Sock {
Ok(res)
}
- // TODO: Create an enum for supported event types, rather than this string thing. We don't
- // support reading all types of events anyway.
- pub fn setevents<'a,T: IntoIterator<Item = &'a &'a str>>(&self, events: T) -> Result<()> {
- let mut msg = "SETEVENTS".to_string();
- for e in events {
- is_keyword(e)?;
- msg.push(' ');
- msg.push_str(e);
- }
- msg.push_str("\r\n");
- self.cmd(msg).map(|_|())
+ pub fn setevents(&self, events: EventList) -> Result<()> {
+ self.cmd(events.cmd()).map(|_|())
}
/// Read an event from the socket. This method blocks until an event has been received.