From 6bfa4bfddcf4d064cd92fa8338aea529684cfef8 Mon Sep 17 00:00:00 2001 From: fawce Date: Thu, 24 May 2012 21:25:52 -0400 Subject: [PATCH] fixed logger tests. --- tests/test_logger.py | 33 ++------------------------------- zipline/utils/logger.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/tests/test_logger.py b/tests/test_logger.py index 050c7e29..9c7eb685 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -1,7 +1,7 @@ import logging import uuid -from zipline.utils.logger import configure_logging +from zipline.utils.logger import configure_logging, tail from unittest2 import TestCase @@ -17,35 +17,6 @@ class LoggerTestCase(TestCase): self.LOG.info(test_msg) logfile = open('/var/log/zipline/zipline.log','r') with logfile: - last_line = tail(logfile) + last_line = tail(logfile, window=1) logged_msg = last_line.split(" - ")[1] self.assertEqual(test_msg, logged_msg) - -def tail( f, window=20 ): - """ - from - http://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file- \ - with-python-similar-to-tail - """ - BUFSIZ = 1024 - f.seek(0, 2) - bytes = f.tell() - size = window - block = -1 - data = [] - while size > 0 and bytes > 0: - if (bytes - BUFSIZ > 0): - # Seek back one whole BUFSIZ - f.seek(block*BUFSIZ, 2) - # read BUFFER - data.append(f.read(BUFSIZ)) - else: - # file too small, start from begining - f.seek(0,0) - # only read what was not read - data.append(f.read(bytes)) - linesFound = data[-1].count('\n') - size -= linesFound - bytes -= BUFSIZ - block -= 1 - return '\n'.join(''.join(data).splitlines()[-window:]) diff --git a/zipline/utils/logger.py b/zipline/utils/logger.py index bc48f870..c3b97cda 100644 --- a/zipline/utils/logger.py +++ b/zipline/utils/logger.py @@ -3,6 +3,7 @@ Small classes to assist with timezone calculations, LOGGER configuration, and other common operations. """ + import logging import logging.config from os.path import join, abspath, dirname @@ -17,3 +18,34 @@ def logger_path(): import zipline log_path = dirname(abspath(zipline.__file__)) return join(log_path, 'logging.cfg') + + +# utility for tailing a log file. +def tail( f, window=20 ): + """ + from + http://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file- \ + with-python-similar-to-tail + """ + BUFSIZ = 1024 + f.seek(0, 2) + bytes = f.tell() + size = window + block = -1 + data = [] + while size > 0 and bytes > 0: + if (bytes - BUFSIZ > 0): + # Seek back one whole BUFSIZ + f.seek(block*BUFSIZ, 2) + # read BUFFER + data.append(f.read(BUFSIZ)) + else: + # file too small, start from begining + f.seek(0,0) + # only read what was not read + data.append(f.read(bytes)) + linesFound = data[-1].count('\n') + size -= linesFound + bytes -= BUFSIZ + block -= 1 + return '\n'.join(''.join(data).splitlines()[-window:])