blob: c78bb678f78d80efbfa526b7b0591f8e5cc05b53 [file] [log] [blame]
Dan Talayco2e1a1072010-02-15 14:15:19 -08001'''Docstring to silence pylint; ignores --ignore option for __init__.py'''
Rich Lane6242d9f2013-01-06 17:35:39 -08002import sys
Rich Lane69fd8e02013-08-23 16:23:42 -07003import os
4import logging
Rich Lane477f4812012-10-04 22:49:00 -07005
6# Global config dictionary
7# Populated by oft.
8config = {}
Rich Lane2c7812c2012-12-27 17:52:23 -08009
10# Global DataPlane instance used by all tests.
11# Populated by oft.
12dataplane_instance = None
Rich Lane69fd8e02013-08-23 16:23:42 -070013
14def 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)