]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/topolog.py
Merge pull request #3391 from pguibert6WIND/doc_ldp_prefered_connection
[mirror_frr.git] / tests / topotests / lib / topolog.py
1 #
2 # topolog.py
3 # Library of helper functions for NetDEF Topology Tests
4 #
5 # Copyright (c) 2017 by
6 # Network Device Education Foundation, Inc. ("NetDEF")
7 #
8 # Permission to use, copy, modify, and/or distribute this software
9 # for any purpose with or without fee is hereby granted, provided
10 # that the above copyright notice and this permission notice appear
11 # in all copies.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
14 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
16 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
17 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 # OF THIS SOFTWARE.
21 #
22
23 """
24 Logging utilities for topology tests.
25
26 This file defines our logging abstraction.
27 """
28
29 import sys
30 import logging
31
32 # Helper dictionary to convert Topogen logging levels to Python's logging.
33 DEBUG_TOPO2LOGGING = {
34 'debug': logging.DEBUG,
35 'info': logging.INFO,
36 'output': logging.INFO,
37 'warning': logging.WARNING,
38 'error': logging.ERROR,
39 'critical': logging.CRITICAL,
40 }
41
42 class InfoFilter(logging.Filter):
43 def filter(self, rec):
44 return rec.levelno in (logging.DEBUG, logging.INFO)
45
46 #
47 # Logger class definition
48 #
49
50 class Logger(object):
51 """
52 Logger class that encapsulates logging functions, internaly it uses Python
53 logging module with a separated instance instead of global.
54
55 Default logging level is 'info'.
56 """
57
58 def __init__(self):
59 # Create default global logger
60 self.log_level = logging.INFO
61 self.logger = logging.Logger('topolog', level=self.log_level)
62
63 handler_stdout = logging.StreamHandler(sys.stdout)
64 handler_stdout.setLevel(logging.DEBUG)
65 handler_stdout.addFilter(InfoFilter())
66 handler_stdout.setFormatter(
67 logging.Formatter(fmt='%(asctime)s %(levelname)s: %(message)s')
68 )
69 handler_stderr = logging.StreamHandler()
70 handler_stderr.setLevel(logging.WARNING)
71 handler_stderr.setFormatter(
72 logging.Formatter(fmt='%(asctime)s %(levelname)s: %(message)s')
73 )
74
75 self.logger.addHandler(handler_stdout)
76 self.logger.addHandler(handler_stderr)
77
78 # Handle more loggers
79 self.loggers = {'topolog': self.logger}
80
81 def set_log_level(self, level):
82 "Set the logging level"
83 self.log_level = DEBUG_TOPO2LOGGING.get(level)
84 self.logger.setLevel(self.log_level)
85
86 def get_logger(self, name='topolog', log_level=None, target=sys.stdout):
87 """
88 Get a new logger entry. Allows creating different loggers for formating,
89 filtering or handling (file, stream or stdout/stderr).
90 """
91 if log_level is None:
92 log_level = self.log_level
93 if self.loggers.has_key(name):
94 return self.loggers[name]
95
96 nlogger = logging.Logger(name, level=log_level)
97 if isinstance(target, str):
98 handler = logging.FileHandler(filename=target)
99 else:
100 handler = logging.StreamHandler(stream=target)
101
102 handler.setFormatter(
103 logging.Formatter(fmt='%(asctime)s %(levelname)s: %(message)s')
104 )
105 nlogger.addHandler(handler)
106 self.loggers[name] = nlogger
107 return nlogger
108
109 #
110 # Global variables
111 #
112
113 logger_config = Logger()
114 logger = logger_config.logger