From 6f405b227e4b8df472e30b7ed61e29c4ded94522 Mon Sep 17 00:00:00 2001 From: liuchongming Date: Wed, 17 Mar 2021 16:28:32 +0800 Subject: [PATCH] Add mindconverter logger to print warning level msg to stdout. --- mindinsight/mindconverter/cli.py | 8 +---- .../mindconverter/common/exceptions.py | 7 +--- mindinsight/mindconverter/common/log.py | 33 +++++++++++++++++-- .../graph_based_converter/framework.py | 6 ++-- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/mindinsight/mindconverter/cli.py b/mindinsight/mindconverter/cli.py index a5833186..3686fd6b 100644 --- a/mindinsight/mindconverter/cli.py +++ b/mindinsight/mindconverter/cli.py @@ -459,9 +459,7 @@ def _run(in_files, model_file, shape, input_nodes, output_nodes, out_dir, report for file in files: files_config['in_files'].append(os.path.join(root_dir, file)) main(files_config) - log_console.info("\n") log_console.info("MindConverter: conversion is completed.") - log_console.info("\n") elif model_file: file_config = { @@ -478,14 +476,10 @@ def _run(in_files, model_file, shape, input_nodes, output_nodes, out_dir, report sys.path.append(project_path) main_graph_base_converter(file_config) - log_console.info("\n") log_console.info("MindConverter: conversion is completed.") - log_console.info("\n") else: error_msg = "`--in_file` and `--model_file` should be set at least one." error = FileNotFoundError(error_msg) log.error(str(error)) - log_console.error("\n") - log_console.error("mindconverter: error: %s", str(error)) - log_console.error("\n") + log_console.error(f"mindconverter: error: {str(error)}") sys.exit(-1) diff --git a/mindinsight/mindconverter/common/exceptions.py b/mindinsight/mindconverter/common/exceptions.py index fa4dfa5c..7012d402 100644 --- a/mindinsight/mindconverter/common/exceptions.py +++ b/mindinsight/mindconverter/common/exceptions.py @@ -140,17 +140,12 @@ class MindConverterException(Exception): if not isinstance(e, MindConverterException): detail_info = cls.normalize_error_msg(str(e)) log.error(error) - log_console.error("\n") log_console.error(detail_info) - log_console.error("\n") log.exception(e) sys.exit(0) except ModuleNotFoundError as e: detail_info = "Error detail: Required package not found, please check the runtime environment." - log_console.error("\n") - log_console.error(str(e)) - log_console.error(detail_info) - log_console.error("\n") + log_console.error(f"{str(e)}\n{detail_info}") log.exception(e) sys.exit(0) return res diff --git a/mindinsight/mindconverter/common/log.py b/mindinsight/mindconverter/common/log.py index ad17e716..47362fae 100644 --- a/mindinsight/mindconverter/common/log.py +++ b/mindinsight/mindconverter/common/log.py @@ -1,4 +1,4 @@ -# Copyright 2020 Huawei Technologies Co., Ltd +# Copyright 2020-2021 Huawei Technologies Co., Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,8 +13,35 @@ # limitations under the License. # ============================================================================ """Create a logger.""" +__all__ = ["logger", "logger_console"] + from mindinsight.utils.log import setup_logger logger = setup_logger("mindconverter", "mindconverter", console=False) -logger_console = setup_logger("mindconverter", "mindconverter", console=True, - sub_log_name="logger_console", formatter="%(message)s") + + +class MindConverterLogger: + """MindConverter logger for stdout.""" + + def __init__(self): + self.logger = setup_logger("mindconverter", "mindconverter", console=True, + sub_log_name="logger_console", formatter="%(message)s") + + def warning(self, msg): + """Log warning message to stdout.""" + self.logger.warning("[WARNING] MINDCONVERTER: %s", msg) + + def info(self, msg): + """Log info level message to stdout.""" + self.logger.info("\n") + self.logger.info("[INFO] MINDCONVERTER: %s", msg) + self.logger.info("\n") + + def error(self, msg): + """Log error level message to stdout.""" + self.logger.error("\n") + self.logger.error("[ERROR] MINDCONVERTER: %s", msg) + self.logger.error("\n") + + +logger_console = MindConverterLogger() diff --git a/mindinsight/mindconverter/graph_based_converter/framework.py b/mindinsight/mindconverter/graph_based_converter/framework.py index 15c0ac53..f95a3788 100644 --- a/mindinsight/mindconverter/graph_based_converter/framework.py +++ b/mindinsight/mindconverter/graph_based_converter/framework.py @@ -45,8 +45,8 @@ def onnx_lib_version_satisfied(): ort = import_module("onnxruntime") optimizer = import_module("onnxoptimizer.version") if not lib_version_satisfied(getattr(ort, "__version__"), ONNXRUNTIME_MIN_VER): - log_console.warning("onnxruntime's version should be greater than %s, however current version is %s.", - ONNXRUNTIME_MIN_VER, ort.__version__) + log_console.warning(f"onnxruntime's version should be greater than {ONNXRUNTIME_MIN_VER}, " + f"however current version is {ort.__version__}.") if not lib_version_satisfied(getattr(onnx, "__version__"), ONNX_MIN_VER) \ or not lib_version_satisfied(getattr(optimizer, "version"), ONNXOPTIMIZER_MIN_VER): @@ -57,9 +57,7 @@ def onnx_lib_version_satisfied(): def _print_error(err): """Print error to stdout and record it.""" log.error(err) - log_console.error("\n") log_console.error(str(err)) - log_console.error("\n") def torch_version_satisfied(output_queue):