Dan Talayco | 2e1a107 | 2010-02-15 14:15:19 -0800 | [diff] [blame] | 1 | '''Docstring to silence pylint; ignores --ignore option for __init__.py''' |
Rich Lane | 6242d9f | 2013-01-06 17:35:39 -0800 | [diff] [blame] | 2 | import sys |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame^] | 3 | import os |
| 4 | import logging |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 5 | |
| 6 | # Global config dictionary |
| 7 | # Populated by oft. |
| 8 | config = {} |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 9 | |
| 10 | # Global DataPlane instance used by all tests. |
| 11 | # Populated by oft. |
| 12 | dataplane_instance = None |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame^] | 13 | |
| 14 | def open_logfile(name): |
| 15 | """ |
| 16 | (Re)open logfile |
| 17 | |
| 18 | When using a log directory a new logfile is created for each test. The same |
| 19 | code is used to implement a single logfile in the absence of --log-dir. |
| 20 | """ |
| 21 | |
| 22 | _format = "%(asctime)s.%(msecs)03d %(name)-10s: %(levelname)-8s: %(message)s" |
| 23 | _datefmt = "%H:%M:%S" |
| 24 | |
| 25 | if config["log_dir"] != None: |
| 26 | filename = os.path.join(config["log_dir"], name) + ".log" |
| 27 | else: |
| 28 | filename = config["log_file"] |
| 29 | |
| 30 | logger = logging.getLogger() |
| 31 | |
| 32 | # Remove any existing handlers |
| 33 | for handler in logger.handlers: |
| 34 | logger.removeHandler(handler) |
| 35 | handler.close() |
| 36 | |
| 37 | # Add a new handler |
| 38 | handler = logging.FileHandler(filename, mode='a') |
| 39 | handler.setFormatter(logging.Formatter(_format, _datefmt)) |
| 40 | logger.addHandler(handler) |