Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Dan Talayco | 2e1a107 | 2010-02-15 14:15:19 -0800 | [diff] [blame] | 17 | '''Docstring to silence pylint; ignores --ignore option for __init__.py''' |
Rich Lane | 6242d9f | 2013-01-06 17:35:39 -0800 | [diff] [blame] | 18 | import sys |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 19 | import os |
| 20 | import logging |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 21 | |
| 22 | # Global config dictionary |
| 23 | # Populated by oft. |
| 24 | config = {} |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 25 | |
| 26 | # Global DataPlane instance used by all tests. |
| 27 | # Populated by oft. |
| 28 | dataplane_instance = None |
Rich Lane | 69fd8e0 | 2013-08-23 16:23:42 -0700 | [diff] [blame] | 29 | |
| 30 | def open_logfile(name): |
| 31 | """ |
| 32 | (Re)open logfile |
| 33 | |
| 34 | When using a log directory a new logfile is created for each test. The same |
| 35 | code is used to implement a single logfile in the absence of --log-dir. |
| 36 | """ |
| 37 | |
| 38 | _format = "%(asctime)s.%(msecs)03d %(name)-10s: %(levelname)-8s: %(message)s" |
| 39 | _datefmt = "%H:%M:%S" |
| 40 | |
| 41 | if config["log_dir"] != None: |
| 42 | filename = os.path.join(config["log_dir"], name) + ".log" |
| 43 | else: |
| 44 | filename = config["log_file"] |
| 45 | |
| 46 | logger = logging.getLogger() |
| 47 | |
| 48 | # Remove any existing handlers |
| 49 | for handler in logger.handlers: |
| 50 | logger.removeHandler(handler) |
| 51 | handler.close() |
| 52 | |
| 53 | # Add a new handler |
| 54 | handler = logging.FileHandler(filename, mode='a') |
| 55 | handler.setFormatter(logging.Formatter(_format, _datefmt)) |
| 56 | logger.addHandler(handler) |