2 Copyright (C) 2018-2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
23 _DEFAULT_LOGGER_NAME = 'openvino.tools.benchmark'
24 _DEFAULT_LOG_FILE = 'openvino.tools.benchmark.log'
26 PRINT_INFO = logging.INFO + 5
27 logging.addLevelName(PRINT_INFO, "PRINT_INFO")
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"
35 class LoggingFormatter(logging.Formatter):
36 def format(self, record: logging.LogRecord):
37 if record.levelno == PRINT_INFO:
39 return super().format(record)
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
48 def emit(self, record):
49 if record.levelno >= logging.WARNING:
50 self.stream = self.err_stream
52 self.stream = self.default_stream
56 _LOGGING_CONFIGURATION = {
58 'disable_existing_loggers': False,
61 '()': LoggingFormatter,
62 'format': '%(asctime)s %(name)s %(levelname)s: %(message)s',
66 'format': '%(asctime)s %(name)s %(levelname)s: %(message)s'
73 'formatter': 'default',
78 _DEFAULT_LOGGER_NAME: {
79 'handlers': ['console'],
80 'level': _LOGGING_LEVEL,
86 logging.config.dictConfig(_LOGGING_CONFIGURATION)
88 _default_logger = logging.getLogger(_DEFAULT_LOGGER_NAME)
91 def _warning_handler(message, category, filename, lineno):
92 s = warnings.formatwarning(message, category, filename, lineno)
93 _default_logger.warning(s)
96 warnings.showwarning = _warning_handler
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)
105 def error(msg, *args, **kwargs):
106 _default_logger.error(msg, *args, **kwargs)
109 def warning(msg, *args, raise_warning=True, **kwargs):
113 _default_logger.warning(msg, *args, **kwargs)
116 def info(msg, *args, **kwargs):
117 _default_logger.info(msg, *args, **kwargs)
120 def debug(msg, *args, **kwargs):
121 _default_logger.debug(msg, *args, **kwargs)
124 def print_info(msg, *args, **kwargs):
125 _default_logger.log(PRINT_INFO, msg, *args, **kwargs)