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

[tor-commits] [nyx/master] Move log_file_path() to util



commit 095dc9cceb57c5c7649b594023fdad6505a17ac0
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Apr 27 09:29:36 2015 -0700

    Move log_file_path() to util
    
    Small function that's been just begging to go into the util for a while now.
---
 nyx/log_panel.py |   29 ++++++++++-------------------
 nyx/util/log.py  |   18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/nyx/log_panel.py b/nyx/log_panel.py
index 5016e09..f250084 100644
--- a/nyx/log_panel.py
+++ b/nyx/log_panel.py
@@ -21,7 +21,7 @@ import nyx.popups
 
 from nyx import __version__
 from nyx.util import join, panel, tor_controller, ui_tools
-from nyx.util.log import TOR_RUNLEVELS, LogGroup, LogEntry, read_tor_log, condense_runlevels, days_since
+from nyx.util.log import TOR_RUNLEVELS, LogGroup, LogEntry, read_tor_log, condense_runlevels, days_since, log_file_path
 
 ENTRY_INDENT = 2  # spaces an entry's message is indented after the first line
 
@@ -48,7 +48,6 @@ CONFIG = conf.config_dict('nyx', {
   'features.log.regex': [],
   'cache.log_panel.size': 1000,
   'msg.misc.event_types': '',
-  'tor.chroot': '',
   'attr.log_color': {},
 }, conf_handler)
 
@@ -71,14 +70,6 @@ NYX_LOGGER = log.LogBuffer(log.Runlevel.DEBUG, yield_records = True)
 stem_logger.addHandler(NYX_LOGGER)
 
 
-def log_file_path():
-  for log_entry in tor_controller().get_conf('Log', [], True):
-    entry_comp = log_entry.split()  # looking for an entry like: notice file /var/log/tor/notices.log
-
-    if entry_comp[1] == 'file':
-      return CONFIG['tor.chroot'] + entry_comp[2]
-
-
 class LogPanel(panel.Panel, threading.Thread):
   """
   Listens for and displays tor, nyx, and stem events. This can prepopulate
@@ -115,7 +106,7 @@ class LogPanel(panel.Panel, threading.Thread):
 
     self.regex_filter = None             # filter for presented log events (no filtering if None)
     self.last_content_height = 0         # height of the rendered content when last drawn
-    self.log_file = None                 # file log messages are saved to (skipped if None)
+    self._log_file = None                # file log messages are saved to (skipped if None)
     self.scroll = 0
 
     self.set_pause_attr('_msg_log')
@@ -142,7 +133,7 @@ class LogPanel(panel.Panel, threading.Thread):
       set_runlevels = list(set.intersection(set(self.logged_events), set(list(log.Runlevel))))
       read_limit = CONFIG['features.log.prepopulateReadLimit']
 
-      logging_location = log_file_path()
+      logging_location = log_file_path(tor_controller())
 
       if logging_location:
         try:
@@ -188,14 +179,14 @@ class LogPanel(panel.Panel, threading.Thread):
         if not os.path.exists(base_dir):
           os.makedirs(base_dir)
 
-        self.log_file = open(log_path, 'a')
+        self._log_file = open(log_path, 'a')
         log.notice('nyx %s opening log file (%s)' % (__version__, log_path))
       except IOError as exc:
         log.error('Unable to write to log file: %s' % exc.strerror)
-        self.log_file = None
+        self._log_file = None
       except OSError as exc:
         log.error('Unable to write to log file: %s' % exc)
-        self.log_file = None
+        self._log_file = None
 
   def set_duplicate_visability(self, is_visible):
     """
@@ -747,13 +738,13 @@ class LogPanel(panel.Panel, threading.Thread):
 
     # note event in the log file if we're saving them
 
-    if self.log_file:
+    if self._log_file:
       try:
-        self.log_file.write(event.display_message + '\n')
-        self.log_file.flush()
+        self._log_file.write(event.display_message + '\n')
+        self._log_file.flush()
       except IOError as exc:
         log.error('Unable to write to log file: %s' % exc.strerror)
-        self.log_file = None
+        self._log_file = None
 
     with self.vals_lock:
       self._msg_log.add(event)
diff --git a/nyx/util/log.py b/nyx/util/log.py
index 9a40411..4df50c0 100644
--- a/nyx/util/log.py
+++ b/nyx/util/log.py
@@ -20,6 +20,7 @@ except ImportError:
 
 TOR_RUNLEVELS = ['DEBUG', 'INFO', 'NOTICE', 'WARN', 'ERR']
 TIMEZONE_OFFSET = time.altzone if time.localtime()[8] else time.timezone
+CONFIG = stem.util.conf.config_dict('nyx', {'tor.chroot': ''})
 
 
 def days_since(timestamp):
@@ -34,6 +35,23 @@ def days_since(timestamp):
   return int((timestamp - TIMEZONE_OFFSET) / 86400)
 
 
+def log_file_path(controller):
+  """
+  Provides the path where tor's log file resides, if one exists.
+
+  :params stem.control.Controller controller: tor controller connection
+
+  :returns: **str** with the absolute path of our log file, or **None** if one
+    doesn't exist
+  """
+
+  for log_entry in controller.get_conf('Log', [], True):
+    entry_comp = log_entry.split()  # looking for an entry like: notice file /var/log/tor/notices.log
+
+    if entry_comp[1] == 'file':
+      return CONFIG['tor.chroot'] + entry_comp[2]
+
+
 @lru_cache()
 def condense_runlevels(*events):
   """



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