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

[or-cvs] r23397: {arm} added: option to save a snapshot of the current log (in arm/trunk: . src/interface)



Author: atagar
Date: 2010-10-03 02:43:47 +0000 (Sun, 03 Oct 2010)
New Revision: 23397

Modified:
   arm/trunk/TODO
   arm/trunk/src/interface/controller.py
   arm/trunk/src/interface/logPanel.py
Log:
added: option to save a snapshot of the current log



Modified: arm/trunk/TODO
===================================================================
--- arm/trunk/TODO	2010-10-03 00:58:26 UTC (rev 23396)
+++ arm/trunk/TODO	2010-10-03 02:43:47 UTC (rev 23397)
@@ -10,9 +10,6 @@
       bugs are being fixed while refactoring.
       
       [ ] log panel
-        - log to file, allowing non-runlevel events to be saved (provide both
-            a continuous option and snapshots taking into account the current
-            filter)
         - log cropping based on time (idea by voidzero)
       [ ] conf panel
         - move torrc validation into util

Modified: arm/trunk/src/interface/controller.py
===================================================================
--- arm/trunk/src/interface/controller.py	2010-10-03 00:58:26 UTC (rev 23396)
+++ arm/trunk/src/interface/controller.py	2010-10-03 02:43:47 UTC (rev 23397)
@@ -663,6 +663,7 @@
           hiddenEntryLabel = "visible" if panels["log"].showDuplicates else "hidden"
           popup.addfstr(6, 2, "<b>u</b>: duplicate log entries (<b>%s</b>)" % hiddenEntryLabel)
           popup.addfstr(6, 41, "<b>x</b>: clear event log")
+          popup.addfstr(7, 41, "<b>a</b>: save snapshot of the log")
           
           pageOverrideKeys = (ord('m'), ord('n'), ord('s'), ord('i'), ord('d'), ord('e'), ord('r'), ord('f'), ord('x'))
         if page == 1:
@@ -801,6 +802,47 @@
         panel.CURSES_LOCK.release()
       
       panels["graph"].redraw(True)
+    elif page == 0 and (key == ord('a') or key == ord('A')):
+      # allow user to enter a path to take a snapshot - abandons if left blank
+      panel.CURSES_LOCK.acquire()
+      try:
+        setPauseState(panels, isPaused, page, True)
+        
+        # provides prompt
+        panels["control"].setMsg("Path to save log snapshot: ")
+        panels["control"].redraw(True)
+        
+        # makes cursor and typing visible
+        try: curses.curs_set(1)
+        except curses.error: pass
+        curses.echo()
+        
+        # gets user input (this blocks monitor updates)
+        pathInput = panels["control"].win.getstr(0, 27)
+        
+        # reverts visability settings
+        try: curses.curs_set(0)
+        except curses.error: pass
+        curses.noecho()
+        curses.halfdelay(REFRESH_RATE * 10) # evidenlty previous tweaks reset this...
+        
+        if pathInput != "":
+          try:
+            panels["log"].saveSnapshot(pathInput)
+            panels["control"].setMsg("Saved: %s" % pathInput, curses.A_STANDOUT)
+            panels["control"].redraw(True)
+            time.sleep(2)
+          except IOError, exc:
+            panels["control"].setMsg("Unable to save snapshot: %s" % str(exc), curses.A_STANDOUT)
+            panels["control"].redraw(True)
+            time.sleep(2)
+        
+        panels["control"].setMsg(CTL_PAUSED if isPaused else CTL_HELP)
+        setPauseState(panels, isPaused, page)
+      finally:
+        panel.CURSES_LOCK.release()
+      
+      panels["graph"].redraw(True)
     elif page == 0 and (key == ord('e') or key == ord('E')):
       # allow user to enter new types of events to log - unchanged if left blank
       panel.CURSES_LOCK.acquire()

Modified: arm/trunk/src/interface/logPanel.py
===================================================================
--- arm/trunk/src/interface/logPanel.py	2010-10-03 00:58:26 UTC (rev 23396)
+++ arm/trunk/src/interface/logPanel.py	2010-10-03 02:43:47 UTC (rev 23397)
@@ -669,6 +669,32 @@
     self.redraw(True)
     self.valsLock.release()
   
+  def saveSnapshot(self, path):
+    """
+    Saves the log events currently being displayed to the given path. This
+    takes filers into account. This overwrites the file if it already exists,
+    and raises an IOError if there's a problem.
+    
+    Arguments:
+      path - path where to save the log snapshot
+    """
+    
+    # make dir if the path doesn't already exist
+    baseDir = os.path.dirname(path)
+    if not os.path.exists(baseDir): os.makedirs(baseDir)
+    
+    snapshotFile = open(path, "w")
+    self.valsLock.acquire()
+    try:
+      for entry in self.msgLog:
+        isVisible = not self.regexFilter or self.regexFilter.search(entry.getDisplayMessage())
+        if isVisible: snapshotFile.write(entry.getDisplayMessage(True) + "\n")
+      
+      self.valsLock.release()
+    except Exception, exc:
+      self.valsLock.release()
+      raise exc
+  
   def handleKey(self, key):
     if uiTools.isScrollKey(key):
       pageHeight = self.getPreferredSize()[0] - 1