1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2021-2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import logging 19from os import path, makedirs 20from typing import NoReturn, Type, Optional 21 22from runner.enum_types.verbose_format import VerboseKind 23 24SUMMARY_LOG_LEVEL = 21 25NONE_LOG_LEVEL = 22 26 27 28class Log: 29 _is_init = False 30 31 @staticmethod 32 def setup(verbose: VerboseKind, report_root: str) -> logging.Logger: 33 logger = logging.getLogger("runner") 34 35 log_path = report_root if report_root is not None else \ 36 path.join(path.sep, "tmp") 37 makedirs(log_path, exist_ok=True) 38 39 file_handler = logging.FileHandler(path.join(log_path, "runner.log")) 40 console_handler = logging.StreamHandler() 41 42 if verbose == VerboseKind.ALL: 43 logger.setLevel(logging.DEBUG) 44 file_handler.setLevel(logging.DEBUG) 45 console_handler.setLevel(logging.DEBUG) 46 elif verbose == VerboseKind.SHORT: 47 logger.setLevel(logging.INFO) 48 file_handler.setLevel(logging.INFO) 49 console_handler.setLevel(logging.INFO) 50 else: 51 logger.setLevel(NONE_LOG_LEVEL) 52 file_handler.setLevel(NONE_LOG_LEVEL) 53 console_handler.setLevel(NONE_LOG_LEVEL) 54 55 file_formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s') 56 file_handler.setFormatter(file_formatter) 57 console_formatter = logging.Formatter('%(message)s') 58 console_handler.setFormatter(console_formatter) 59 60 logger.addHandler(file_handler) 61 logger.addHandler(console_handler) 62 63 Log._is_init = True 64 65 return logger 66 67 @staticmethod 68 def all(logger: logging.Logger, message: str) -> None: 69 """ 70 Logs on the level verbose=ALL 71 """ 72 if Log._is_init: 73 logger.debug(message) 74 else: 75 print(message) 76 77 @staticmethod 78 def short(logger: logging.Logger, message: str) -> None: 79 """ 80 Logs on the level verbose=SHORT 81 """ 82 if Log._is_init: 83 logger.info(message) 84 else: 85 print(message) 86 87 @staticmethod 88 def summary(logger: logging.Logger, message: str) -> None: 89 """ 90 Logs on the level verbose=SUMMARY (sum) 91 """ 92 if Log._is_init: 93 logger.log(SUMMARY_LOG_LEVEL, message) 94 else: 95 print(message) 96 97 @staticmethod 98 def default(logger: logging.Logger, message: str) -> None: 99 """ 100 Logs on the level verbose=None 101 """ 102 if Log._is_init: 103 logger.log(NONE_LOG_LEVEL, message) 104 else: 105 print(message) 106 107 @staticmethod 108 def exception_and_raise(logger: logging.Logger, message: str, 109 exception_cls: Optional[Type[Exception]] = None) -> NoReturn: 110 """ 111 Logs and throw the exception 112 """ 113 if Log._is_init: 114 logger.critical(message) 115 if exception_cls is None: 116 raise Exception(message) 117 raise exception_cls(message) 118