[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Support for logging events



commit 7ba579494215a5353cff1db9fbf7e59d6b666af9
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Nov 5 09:14:07 2012 -0800

    Support for logging events
    
    Adding an event class for DEBUG, INFO, NOTICE, WARN, and ERR.
---
 docs/api/response.rst        |    1 +
 stem/control.py              |   15 ++++++++++-----
 stem/response/events.py      |   27 +++++++++++++++++++++++++++
 test/unit/response/events.py |   19 +++++++++++++++++++
 4 files changed, 57 insertions(+), 5 deletions(-)

diff --git a/docs/api/response.rst b/docs/api/response.rst
index 6e23c91..fd6e2cc 100644
--- a/docs/api/response.rst
+++ b/docs/api/response.rst
@@ -16,5 +16,6 @@ Events
 ------
 
 .. autoclass:: stem.response.events.Event
+.. autoclass:: stem.response.events.LogEvent
 .. autoclass:: stem.response.events.BandwidthEvent
 
diff --git a/stem/control.py b/stem/control.py
index f9f7def..baeb96e 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -590,11 +590,16 @@ class Controller(BaseController):
     
     The EventType enumeration is mapped to event classes as follows...
     
-    ========= ===========
-    EventType Event Class
-    ========= ===========
-    **BW**    :class:`stem.response.events.BandwidthEvent`
-    ========= ===========
+    =========== ===========
+    EventType   Event Class
+    =========== ===========
+    **DEBUG**   :class:`stem.response.events.LogEvent`
+    **INFO**    :class:`stem.response.events.LogEvent`
+    **NOTICE**  :class:`stem.response.events.LogEvent`
+    **WARN**    :class:`stem.response.events.LogEvent`
+    **ERR**     :class:`stem.response.events.LogEvent`
+    **BW**      :class:`stem.response.events.BandwidthEvent`
+    =========== ===========
     
     :param functor listener: function to be called when an event is received
     :param stem.control.EventType events: event types to be listened for
diff --git a/stem/response/events.py b/stem/response/events.py
index fa451c7..93c3191 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -14,6 +14,11 @@ class Event(stem.response.ControlMessage):
   Base for events we receive asynchronously, as described in section 4.1 of the
   `control-spec
   <https://gitweb.torproject.org/torspec.git/blob/HEAD:/control-spec.txt>`_.
+  
+  :var str type: event type
+  :var int arrived_at: unix timestamp for when the message arrived
+  :var list positional_args: positional arguments of the event
+  :var dict keyword_args: key/value arguments of the event
   """
   
   _POSITIONAL_ARGS = ()
@@ -91,7 +96,29 @@ class BandwidthEvent(Event):
     self.read = long(self.read)
     self.written = long(self.written)
 
+class LogEvent(Event):
+  """
+  Tor logging event. These are the most visible kind of event since, by
+  default, tor logs at the NOTICE runlevel to stdout.
+  
+  :var str runlevel: runlevel of the logged message
+  :var str message: logged message
+  """
+  
+  def _parse(self):
+    self.runlevel = self.type
+    
+    # message is our content, minus the runlevel and ending "OK" if a
+    # multi-line message
+    
+    self.message = str(self)[len(self.runlevel) + 1:].rstrip("\nOK")
+
 EVENT_TYPE_TO_CLASS = {
   "BW": BandwidthEvent,
+  "DEBUG": LogEvent,
+  "INFO": LogEvent,
+  "NOTICE": LogEvent,
+  "WARN": LogEvent,
+  "ERR": LogEvent,
 }
 
diff --git a/test/unit/response/events.py b/test/unit/response/events.py
index 1dea7c7..1942b69 100644
--- a/test/unit/response/events.py
+++ b/test/unit/response/events.py
@@ -72,4 +72,23 @@ class TestEvents(unittest.TestCase):
     self.assertRaises(ProtocolError, _get_event, "650 BW -15 25")
     self.assertRaises(ProtocolError, _get_event, "650 BW 15 -25")
     self.assertRaises(ProtocolError, _get_event, "650 BW x 25")
+  
+  def test_log_events(self):
+    event = _get_event("650 DEBUG connection_edge_process_relay_cell(): Got an extended cell! Yay.")
+    
+    self.assertTrue(isinstance(event, stem.response.events.LogEvent))
+    self.assertEqual("DEBUG", event.runlevel)
+    self.assertEqual("connection_edge_process_relay_cell(): Got an extended cell! Yay.", event.message)
+    
+    event = _get_event("650 INFO circuit_finish_handshake(): Finished building circuit hop:")
+    
+    self.assertTrue(isinstance(event, stem.response.events.LogEvent))
+    self.assertEqual("INFO", event.runlevel)
+    self.assertEqual("circuit_finish_handshake(): Finished building circuit hop:", event.message)
+    
+    event = _get_event("650+WARN\na multi-line\nwarning message\n.\n650 OK\n")
+    
+    self.assertTrue(isinstance(event, stem.response.events.LogEvent))
+    self.assertEqual("WARN", event.runlevel)
+    self.assertEqual("a multi-line\nwarning message", event.message)
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits