commit 0dd3c643d48fbab03014c2c1d1d5aa201f5be57c Author: Damian Johnson atagar@torproject.org Date: Wed Apr 29 09:56:14 2015 -0700
Pretend log file entries are from the currnet year
Log files lack the year so we gotta guess (#15607). We hardcoded 2012 so this would still work for leap years but this is a pretty gross hack. Better to pretend the log entries are recent, and if we hit a leap year edge case then meh. User just won't get log prepopulation. --- nyx/log_panel.py | 13 +++++-------- nyx/util/log.py | 14 ++++++++++---- 2 files changed, 15 insertions(+), 12 deletions(-)
diff --git a/nyx/log_panel.py b/nyx/log_panel.py index a592e56..e1465ab 100644 --- a/nyx/log_panel.py +++ b/nyx/log_panel.py @@ -128,18 +128,15 @@ class LogPanel(panel.Panel, threading.Thread): # fetches past tor events from log file, if available
if CONFIG['features.log.prepopulate']: - set_runlevels = list(set.intersection(set(self.logged_events), set(list(log.Runlevel)))) - read_limit = CONFIG['features.log.prepopulateReadLimit'] + log_location = log_file_path(tor_controller())
- logging_location = log_file_path(tor_controller()) - - if logging_location: + if log_location: try: - for entry in reversed(list(read_tor_log(logging_location, read_limit))): - if entry.type in set_runlevels: + for entry in reversed(list(read_tor_log(log_location, CONFIG['features.log.prepopulateReadLimit']))): + if entry.type in self.logged_events: self._msg_log.add(entry) except IOError as exc: - log.info('Unable to read log located at %s: %s' % (logging_location, exc)) + log.info('Unable to read log located at %s: %s' % (log_location, exc)) except ValueError as exc: log.info(str(exc))
diff --git a/nyx/util/log.py b/nyx/util/log.py index a2e3820..e188cb4 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 datetime import os import time import threading @@ -376,18 +377,23 @@ def read_tor_log(path, read_limit = None):
runlevel = line_comp[3][1:-1].upper() msg = ' '.join(line_comp[4:]) + current_year = str(datetime.datetime.now().year)
- # Pretending the year is 2012 because 2012 is a leap year. We don't know - # the actual year (#15607) so picking something else risks strptime failing - # when it reads Feb 29th (#5265). + # Pretending it's the current year. We don't know the actual year (#15607) + # and this may fail due to leap years when picking Feb 29th (#5265).
try: - timestamp_str = '2012 ' + ' '.join(line_comp[:3]) + timestamp_str = current_year + ' ' + ' '.join(line_comp[:3]) timestamp_str = timestamp_str.split('.', 1)[0] # drop fractional seconds timestamp_comp = list(time.strptime(timestamp_str, '%Y %b %d %H:%M:%S')) timestamp_comp[8] = isdst
timestamp = int(time.mktime(timestamp_comp)) # converts local to unix time + + if timestamp > time.time(): + # log entry is from before a year boundary + timestamp_comp[0] -= 1 + timestamp = int(time.mktime(timestamp_comp)) except ValueError: raise ValueError("Log located at %s has a timestamp we don't recognize: %s" % (path, ' '.join(line_comp[:3])))
tor-commits@lists.torproject.org