1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2021 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 17import logging 18import sys 19 20 21class UpdateToolLogger: 22 """ 23 Global log class 24 """ 25 INFO_LOG = 'INFO_LOG' 26 WARNING_LOG = 'WARNING_LOG' 27 ERROR_LOG = 'ERROR_LOG' 28 LOG_TYPE = (INFO_LOG, WARNING_LOG, ERROR_LOG) 29 30 def __init__(self, output_type='console'): 31 self.__logger_obj = self.__get_logger_obj(output_type=output_type) 32 33 @staticmethod 34 def __get_logger_obj(output_type='console'): 35 ota_logger = logging.getLogger(__name__) 36 ota_logger.setLevel(level=logging.INFO) 37 formatter = logging.Formatter( 38 '%(asctime)s %(levelname)s : %(message)s', 39 "%Y-%m-%d %H:%M:%S") 40 if output_type == 'console': 41 console_handler = logging.StreamHandler() 42 console_handler.setLevel(logging.INFO) 43 console_handler.setFormatter(formatter) 44 ota_logger.addHandler(console_handler) 45 elif output_type == 'file': 46 file_handler = logging.FileHandler("UpdateToolLog.txt") 47 file_handler.setLevel(logging.INFO) 48 file_handler.setFormatter(formatter) 49 ota_logger.addHandler(file_handler) 50 return ota_logger 51 52 def print_log(self, msg, log_type=INFO_LOG): 53 """ 54 Print log information. 55 :param msg: log information 56 :param log_type: log type 57 :return: 58 """ 59 if log_type == self.LOG_TYPE[0]: 60 self.__logger_obj.info(msg) 61 elif log_type == self.LOG_TYPE[1]: 62 self.__logger_obj.warning(msg) 63 elif log_type == self.LOG_TYPE[2]: 64 self.__logger_obj.error(msg) 65 else: 66 self.__logger_obj.error("Unknown log type! %s", log_type) 67 return False 68 return True 69 70 def print_uncaught_exception_msg(self, msg, exc_info): 71 """ 72 Print log when an uncaught exception occurs. 73 :param msg: Uncaught exception 74 :param exc_info: information about the uncaught exception 75 """ 76 self.__logger_obj.error(msg, exc_info=exc_info) 77 78 79UPDATE_LOGGER = UpdateToolLogger() 80 81 82def handle_exception(exc_type, exc_value, exc_traceback): 83 """ 84 Override global caught exceptions. 85 :param exc_type: exception type 86 :param exc_value: exception value 87 :param exc_traceback: exception traceback 88 :return: 89 """ 90 if issubclass(exc_type, KeyboardInterrupt): 91 sys.__excepthook__(exc_type, exc_value, exc_traceback) 92 return 93 UPDATE_LOGGER.print_uncaught_exception_msg( 94 "Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback)) 95 from utils import clear_resource 96 clear_resource(err_clear=True) 97 98 99sys.excepthook = handle_exception 100 101 102class VendorExpandError(OSError): 103 """ 104 Vendor extended exception class. 105 Script interfaces are not completely overriden. 106 """ 107 def __init__(self, script_class, func_name): 108 super().__init__() 109 self.script_class = script_class 110 self.func_name = func_name 111 112 def __str__(self, ): 113 return ('%s Vendor expansion does not override function %s' % 114 (self.script_class, self.func_name)) 115