commit d84376a1ff3c459de603878763e4fddff019fa43 Author: Damian Johnson atagar@torproject.org Date: Sat Dec 31 12:13:45 2011 -0800
Making the testing log runlevel customizable
I'm gonna be making stem include two runlevels that it doesn't natively support: TRACE and NOTICE. The later is so we have runlevels to match tor, and the former is so we can have request/reply logging without cluttering the DEBUG logs. --- run_tests.py | 38 ++++++++++++++++++++++++++------------ test/output.py | 7 +++++-- 2 files changed, 31 insertions(+), 14 deletions(-)
diff --git a/run_tests.py b/run_tests.py index 9ceb2b1..577ed2e 100755 --- a/run_tests.py +++ b/run_tests.py @@ -31,8 +31,8 @@ import test.integ.connection.protocolinfo import stem.util.enum import stem.util.term as term
-OPT = "uic:t:lh" -OPT_EXPANDED = ["unit", "integ", "config=", "targets=", "log", "help"] +OPT = "uic:t:l:h" +OPT_EXPANDED = ["unit", "integ", "config=", "targets=", "log=", "help"] DIVIDER = "=" * 70
UNIT_TESTS = ( @@ -77,7 +77,8 @@ Runs tests for the stem library. -i, --integ runs integration tests -c, --config PATH path to a custom test configuration -t, --target TARGET comma separated list of extra targets for integ tests - -l, --log includes logging output with test results + -l, --log RUNLEVEL includes logging output with test results, runlevels: + TRACE, DEBUG, INFO, NOTICE, WARN, ERROR -h, --help presents this help
Integration targets: @@ -102,10 +103,11 @@ def print_divider(msg, is_header = False): print term.format("%s\n%s\n%s\n" % (DIVIDER, msg.center(70), DIVIDER), *attr)
def print_logging(logging_buffer): - for line in logging_buffer: - print term.format(line, term.Color.MAGENTA) - - print + if not logging_buffer.is_empty(): + for line in logging_buffer: + print term.format(line, term.Color.MAGENTA) + + print
if __name__ == '__main__': start_time = time.time() @@ -113,7 +115,7 @@ if __name__ == '__main__': run_integ_tests = False config_path = None test_config = stem.util.conf.get_config("test") - include_logging = False + logging_runlevel = logging.FATAL
# parses user input, noting any issues try: @@ -143,7 +145,19 @@ if __name__ == '__main__': config_flag = TARGET_ATTR[target][0] test_config.set(config_flag, "true") elif opt in ("-l", "--log"): - include_logging = True + runlevel = arg.upper() + + # TODO: logger has no notion of a TRACE or NOTICE runlevel + if runlevel == "TRACE": logging_runlevel = logging.DEBUG - 5 + elif runlevel == "DEBUG": logging_runlevel = logging.DEBUG + elif runlevel == "INFO": logging_runlevel = logging.INFO + elif runlevel == "NOTICE": logging_runlevel = logging.INFO + 5 + elif runlevel == "WARN": logging_runlevel = logging.WARN + elif runlevel == "ERROR": logging_runlevel = logging.ERROR + else: + print "'%s' isn't a logging runlevel, use one of the following instead:" % arg + print " TRACE, DEBUG, INFO, NOTICE, WARN, ERROR" + sys.exit(1) elif opt in ("-h", "--help"): # Prints usage information and quits. This includes a listing of the # valid integration targets. @@ -194,7 +208,7 @@ if __name__ == '__main__': )
stem_logger = logging.getLogger("stem") - logging_buffer = test.output.LogBuffer() + logging_buffer = test.output.LogBuffer(logging_runlevel) stem_logger.addHandler(logging_buffer) stem_logger.setLevel(logging.DEBUG)
@@ -210,7 +224,7 @@ if __name__ == '__main__': sys.stdout.write(test.output.apply_filters(test_results.getvalue(), *output_filters)) print
- if include_logging: print_logging(logging_buffer) + print_logging(logging_buffer)
@@ -262,7 +276,7 @@ if __name__ == '__main__': sys.stdout.write(test.output.apply_filters(test_results.getvalue(), *output_filters)) print
- if include_logging: print_logging(logging_buffer) + print_logging(logging_buffer) except OSError: pass finally: diff --git a/test/output.py b/test/output.py index b7a1991..a22d738 100644 --- a/test/output.py +++ b/test/output.py @@ -140,14 +140,17 @@ class LogBuffer(logging.Handler): can be read later. Log entries are cleared as they are read. """
- def __init__(self): - logging.Handler.__init__(self, level = logging.DEBUG) + def __init__(self, runlevel): + logging.Handler.__init__(self, level = runlevel) self.formatter = logging.Formatter( fmt = '%(asctime)s [%(levelname)s] %(message)s', datefmt = '%D %H:%M:%S')
self._buffer = []
+ def is_empty(self): + return not bool(self._buffer) + def __iter__(self): while self._buffer: yield self.formatter.format(self._buffer.pop(0))