1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2021-2023 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# This file does only contain a selection of the most common options. For a 18# full list see the documentation: 19# http://www.sphinx-doc.org/en/master/config 20 21import logging 22from os import path, makedirs 23from typing import NoReturn, Type, Optional 24 25from runner.enum_types.verbose_format import VerboseKind 26 27SUMMARY_LOG_LEVEL = 21 28NONE_LOG_LEVEL = 22 29 30 31class Log: 32 _is_init = False 33 34 @staticmethod 35 def setup(verbose: VerboseKind, report_root: str) -> logging.Logger: 36 logger = logging.getLogger("runner") 37 38 log_path = report_root if report_root is not None else \ 39 path.join(path.sep, "tmp") 40 makedirs(log_path, exist_ok=True) 41 42 file_handler = logging.FileHandler(path.join(log_path, "runner.log")) 43 console_handler = logging.StreamHandler() 44 45 if verbose == VerboseKind.ALL: 46 logger.setLevel(logging.DEBUG) 47 file_handler.setLevel(logging.DEBUG) 48 console_handler.setLevel(logging.DEBUG) 49 elif verbose == VerboseKind.SHORT: 50 logger.setLevel(logging.INFO) 51 file_handler.setLevel(logging.INFO) 52 console_handler.setLevel(logging.INFO) 53 else: 54 logger.setLevel(NONE_LOG_LEVEL) 55 file_handler.setLevel(NONE_LOG_LEVEL) 56 console_handler.setLevel(NONE_LOG_LEVEL) 57 58 file_formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s') 59 file_handler.setFormatter(file_formatter) 60 console_formatter = logging.Formatter('%(message)s') 61 console_handler.setFormatter(console_formatter) 62 63 logger.addHandler(file_handler) 64 logger.addHandler(console_handler) 65 66 Log._is_init = True 67 68 return logger 69 70 @staticmethod 71 def all(logger: logging.Logger, message: str) -> None: 72 """ 73 Logs on the level verbose=ALL 74 """ 75 if Log._is_init: 76 logger.debug(message) 77 else: 78 print(message) 79 80 @staticmethod 81 def short(logger: logging.Logger, message: str) -> None: 82 """ 83 Logs on the level verbose=SHORT 84 """ 85 if Log._is_init: 86 logger.info(message) 87 else: 88 print(message) 89 90 @staticmethod 91 def summary(logger: logging.Logger, message: str) -> None: 92 """ 93 Logs on the level verbose=SUMMARY (sum) 94 """ 95 if Log._is_init: 96 logger.log(SUMMARY_LOG_LEVEL, message) 97 else: 98 print(message) 99 100 @staticmethod 101 def default(logger: logging.Logger, message: str) -> None: 102 """ 103 Logs on the level verbose=None 104 """ 105 if Log._is_init: 106 logger.log(NONE_LOG_LEVEL, message) 107 else: 108 print(message) 109 110 @staticmethod 111 def exception_and_raise(logger: logging.Logger, message: str, 112 exception_cls: Optional[Type[Exception]] = None) -> NoReturn: 113 """ 114 Logs and throw the exception 115 """ 116 if Log._is_init: 117 logger.critical(message) 118 if exception_cls is None: 119 raise Exception(message) 120 raise exception_cls(message) 121