commit 179a7dcd216661fa32a3ea300b9b1a7dd2c19cc7 Author: Damian Johnson atagar@torproject.org Date: Tue Apr 28 09:49:51 2015 -0700
Revise outputing logs to a file
Moving this to its own simple class. Much nicer. --- nyx/log_panel.py | 38 +++----------------------------------- nyx/util/log.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 35 deletions(-)
diff --git a/nyx/log_panel.py b/nyx/log_panel.py index f250084..44a2b2a 100644 --- a/nyx/log_panel.py +++ b/nyx/log_panel.py @@ -19,9 +19,8 @@ from stem.util import conf, log, str_tools import nyx.arguments 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, log_file_path +from nyx.util.log import TOR_RUNLEVELS, LogFileOutput, 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
@@ -106,7 +105,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 = LogFileOutput(CONFIG['features.log_file']) self.scroll = 0
self.set_pause_attr('_msg_log') @@ -166,28 +165,6 @@ class LogPanel(panel.Panel, threading.Thread):
controller.add_status_listener(reset_listener)
- # opens log file if we'll be saving entries - - if CONFIG['features.log_file']: - log_path = CONFIG['features.log_file'] - - try: - # make dir if the path doesn't already exist - - base_dir = os.path.dirname(log_path) - - if not os.path.exists(base_dir): - os.makedirs(base_dir) - - 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 - except OSError as exc: - log.error('Unable to write to log file: %s' % exc) - self._log_file = None - def set_duplicate_visability(self, is_visible): """ Sets if duplicate log entries are collaped or expanded. @@ -736,18 +713,9 @@ class LogPanel(panel.Panel, threading.Thread): if event.type not in self.logged_events: return
- # note event in the log file if we're saving them - - if self._log_file: - try: - 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 - with self.vals_lock: self._msg_log.add(event) + self._log_file.write(event.display_message)
# notifies the display that it has new content
diff --git a/nyx/util/log.py b/nyx/util/log.py index 4df50c0..a2e3820 100644 --- a/nyx/util/log.py +++ b/nyx/util/log.py @@ -3,6 +3,7 @@ Logging utilities, primiarily short aliases for logging a message at various runlevels. """
+import os import time import threading
@@ -10,6 +11,7 @@ import stem.util.conf import stem.util.log import stem.util.system
+import nyx import nyx.util
try: @@ -271,6 +273,39 @@ class LogEntry(object): return hash(self.display_message)
+class LogFileOutput(object): + """ + File where log messages we receive are written. If unable to do so then a + notification is logged and further write attempts are skipped. + """ + + def __init__(self, path): + self._file = None + + if path: + try: + path_dir = os.path.dirname(path) + + if not os.path.exists(path_dir): + os.makedirs(path_dir) + + self._file = open(path, 'a') + notice('nyx %s opening log file (%s)' % (nyx.__version__, path)) + except IOError as exc: + error('Unable to write to log file: %s' % exc.strerror) + except OSError as exc: + error('Unable to write to log file: %s' % exc) + + def write(self, msg): + if self._file: + try: + self._file.write(msg + '\n') + self._file.flush() + except IOError as exc: + error('Unable to write to log file: %s' % exc.strerror) + self._file = None + + def trace(msg, **attr): _log(stem.util.log.TRACE, msg, **attr)
tor-commits@lists.torproject.org