blob: 2be503803866c37ea9a2f64fe8869e49ce1ddece [file] [log] [blame]
Matteo Scandoloa229eca2017-08-08 13:05:28 -07001
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 Talayco2e1a1072010-02-15 14:15:19 -080017'''Docstring to silence pylint; ignores --ignore option for __init__.py'''
Rich Lane6242d9f2013-01-06 17:35:39 -080018import sys
Rich Lane69fd8e02013-08-23 16:23:42 -070019import os
20import logging
Rich Lane477f4812012-10-04 22:49:00 -070021
22# Global config dictionary
23# Populated by oft.
24config = {}
Rich Lane2c7812c2012-12-27 17:52:23 -080025
26# Global DataPlane instance used by all tests.
27# Populated by oft.
28dataplane_instance = None
Rich Lane69fd8e02013-08-23 16:23:42 -070029
30def 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)