Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / benchmark / logging.py
1 """
2 Copyright (C) 2018-2019 Intel Corporation
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 import logging
18 import logging.config
19 import sys
20 import warnings
21
22 # TODO: move to utils
23 _DEFAULT_LOGGER_NAME = 'openvino.tools.benchmark'
24 _DEFAULT_LOG_FILE = 'openvino.tools.benchmark.log'
25
26 PRINT_INFO = logging.INFO + 5
27 logging.addLevelName(PRINT_INFO, "PRINT_INFO")
28
29 _LOG_LEVEL_ENVIRON = "CALIBRATION_TOOL_LOG_LEVEL"
30 # _LOGGING_LEVEL = logging.getLevelName(os.environ.get(_LOG_LEVEL_ENVIRON, PRINT_INFO))
31 # TODO: refactoring: remove, use original line
32 _LOGGING_LEVEL = "DEBUG"
33
34
35 class LoggingFormatter(logging.Formatter):
36     def format(self, record: logging.LogRecord):
37         if record.levelno == PRINT_INFO:
38             return record.msg
39         return super().format(record)
40
41
42 class ConsoleHandler(logging.StreamHandler):
43     def __init__(self, default_stream=sys.stdout):
44         super().__init__(default_stream)
45         self.default_stream = default_stream
46         self.err_stream = sys.stderr
47
48     def emit(self, record):
49         if record.levelno >= logging.WARNING:
50             self.stream = self.err_stream
51         else:
52             self.stream = self.default_stream
53         super().emit(record)
54
55
56 _LOGGING_CONFIGURATION = {
57     'version': 1,
58     'disable_existing_loggers': False,
59     'formatters': {
60         'default': {
61             '()': LoggingFormatter,
62             'format': '%(asctime)s %(name)s %(levelname)s: %(message)s',
63             'datefmt': '%H:%M:%S'
64         },
65         'detailed': {
66             'format': '%(asctime)s %(name)s %(levelname)s: %(message)s'
67         }
68     },
69     'handlers': {
70         'console': {
71             'level': 'DEBUG',
72             '()': ConsoleHandler,
73             'formatter': 'default',
74         }
75     },
76
77     'loggers': {
78         _DEFAULT_LOGGER_NAME: {
79             'handlers': ['console'],
80             'level': _LOGGING_LEVEL,
81             'propagate': False
82         }
83     }
84 }
85
86 logging.config.dictConfig(_LOGGING_CONFIGURATION)
87
88 _default_logger = logging.getLogger(_DEFAULT_LOGGER_NAME)
89
90
91 def _warning_handler(message, category, filename, lineno):
92     s = warnings.formatwarning(message, category, filename, lineno)
93     _default_logger.warning(s)
94
95
96 warnings.showwarning = _warning_handler
97
98
99 def get_logger(logger_name: str):
100     if logger_name.startswith(_DEFAULT_LOGGER_NAME):
101         return _default_logger.getChild(logger_name)
102     return logging.getLogger(logger_name)
103
104
105 def error(msg, *args, **kwargs):
106     _default_logger.error(msg, *args, **kwargs)
107
108
109 def warning(msg, *args, raise_warning=True, **kwargs):
110     if raise_warning:
111         warnings.warn(msg)
112     else:
113         _default_logger.warning(msg, *args, **kwargs)
114
115
116 def info(msg, *args, **kwargs):
117     _default_logger.info(msg, *args, **kwargs)
118
119
120 def debug(msg, *args, **kwargs):
121     _default_logger.debug(msg, *args, **kwargs)
122
123
124 def print_info(msg, *args, **kwargs):
125     _default_logger.log(PRINT_INFO, msg, *args, **kwargs)