blob: 2be503803866c37ea9a2f64fe8869e49ce1ddece [file] [log] [blame]
Sreeju Sreedhare3fefd92019-04-02 15:57:15 -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
17'''Docstring to silence pylint; ignores --ignore option for __init__.py'''
18import sys
19import os
20import logging
21
22# Global config dictionary
23# Populated by oft.
24config = {}
25
26# Global DataPlane instance used by all tests.
27# Populated by oft.
28dataplane_instance = None
29
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)