140681896Sopenharmony_ci#!/usr/bin/env python3 240681896Sopenharmony_ci# -*- coding: utf-8 -*- 340681896Sopenharmony_ci 440681896Sopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd. 540681896Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 640681896Sopenharmony_ci# you may not use this file except in compliance with the License. 740681896Sopenharmony_ci# You may obtain a copy of the License at 840681896Sopenharmony_ci# 940681896Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 1040681896Sopenharmony_ci# 1140681896Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software 1240681896Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 1340681896Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1440681896Sopenharmony_ci# See the License for the specific language governing permissions and 1540681896Sopenharmony_ci# limitations under the License. 1640681896Sopenharmony_ci 1740681896Sopenharmony_ciimport logging 1840681896Sopenharmony_ciimport sys 1940681896Sopenharmony_ci 2040681896Sopenharmony_ci 2140681896Sopenharmony_ciclass UpdateToolLogger: 2240681896Sopenharmony_ci """ 2340681896Sopenharmony_ci Global log class 2440681896Sopenharmony_ci """ 2540681896Sopenharmony_ci INFO_LOG = 'INFO_LOG' 2640681896Sopenharmony_ci WARNING_LOG = 'WARNING_LOG' 2740681896Sopenharmony_ci ERROR_LOG = 'ERROR_LOG' 2840681896Sopenharmony_ci LOG_TYPE = (INFO_LOG, WARNING_LOG, ERROR_LOG) 2940681896Sopenharmony_ci 3040681896Sopenharmony_ci def __init__(self, output_type='console'): 3140681896Sopenharmony_ci self.__logger_obj = self.__get_logger_obj(output_type=output_type) 3240681896Sopenharmony_ci 3340681896Sopenharmony_ci @staticmethod 3440681896Sopenharmony_ci def __get_logger_obj(output_type='console'): 3540681896Sopenharmony_ci ota_logger = logging.getLogger(__name__) 3640681896Sopenharmony_ci ota_logger.setLevel(level=logging.INFO) 3740681896Sopenharmony_ci formatter = logging.Formatter( 3840681896Sopenharmony_ci '%(asctime)s %(levelname)s : %(message)s', 3940681896Sopenharmony_ci "%Y-%m-%d %H:%M:%S") 4040681896Sopenharmony_ci if output_type == 'console': 4140681896Sopenharmony_ci console_handler = logging.StreamHandler() 4240681896Sopenharmony_ci console_handler.setLevel(logging.INFO) 4340681896Sopenharmony_ci console_handler.setFormatter(formatter) 4440681896Sopenharmony_ci ota_logger.addHandler(console_handler) 4540681896Sopenharmony_ci elif output_type == 'file': 4640681896Sopenharmony_ci file_handler = logging.FileHandler("UpdateToolLog.txt") 4740681896Sopenharmony_ci file_handler.setLevel(logging.INFO) 4840681896Sopenharmony_ci file_handler.setFormatter(formatter) 4940681896Sopenharmony_ci ota_logger.addHandler(file_handler) 5040681896Sopenharmony_ci return ota_logger 5140681896Sopenharmony_ci 5240681896Sopenharmony_ci def print_log(self, msg, log_type=INFO_LOG): 5340681896Sopenharmony_ci """ 5440681896Sopenharmony_ci Print log information. 5540681896Sopenharmony_ci :param msg: log information 5640681896Sopenharmony_ci :param log_type: log type 5740681896Sopenharmony_ci :return: 5840681896Sopenharmony_ci """ 5940681896Sopenharmony_ci if log_type == self.LOG_TYPE[0]: 6040681896Sopenharmony_ci self.__logger_obj.info(msg) 6140681896Sopenharmony_ci elif log_type == self.LOG_TYPE[1]: 6240681896Sopenharmony_ci self.__logger_obj.warning(msg) 6340681896Sopenharmony_ci elif log_type == self.LOG_TYPE[2]: 6440681896Sopenharmony_ci self.__logger_obj.error(msg) 6540681896Sopenharmony_ci else: 6640681896Sopenharmony_ci self.__logger_obj.error("Unknown log type! %s", log_type) 6740681896Sopenharmony_ci return False 6840681896Sopenharmony_ci return True 6940681896Sopenharmony_ci 7040681896Sopenharmony_ci def print_uncaught_exception_msg(self, msg, exc_info): 7140681896Sopenharmony_ci """ 7240681896Sopenharmony_ci Print log when an uncaught exception occurs. 7340681896Sopenharmony_ci :param msg: Uncaught exception 7440681896Sopenharmony_ci :param exc_info: information about the uncaught exception 7540681896Sopenharmony_ci """ 7640681896Sopenharmony_ci self.__logger_obj.error(msg, exc_info=exc_info) 7740681896Sopenharmony_ci 7840681896Sopenharmony_ci 7940681896Sopenharmony_ciUPDATE_LOGGER = UpdateToolLogger() 8040681896Sopenharmony_ci 8140681896Sopenharmony_ci 8240681896Sopenharmony_cidef handle_exception(exc_type, exc_value, exc_traceback): 8340681896Sopenharmony_ci """ 8440681896Sopenharmony_ci Override global caught exceptions. 8540681896Sopenharmony_ci :param exc_type: exception type 8640681896Sopenharmony_ci :param exc_value: exception value 8740681896Sopenharmony_ci :param exc_traceback: exception traceback 8840681896Sopenharmony_ci :return: 8940681896Sopenharmony_ci """ 9040681896Sopenharmony_ci if issubclass(exc_type, KeyboardInterrupt): 9140681896Sopenharmony_ci sys.__excepthook__(exc_type, exc_value, exc_traceback) 9240681896Sopenharmony_ci return 9340681896Sopenharmony_ci UPDATE_LOGGER.print_uncaught_exception_msg( 9440681896Sopenharmony_ci "Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback)) 9540681896Sopenharmony_ci from utils import clear_resource 9640681896Sopenharmony_ci clear_resource(err_clear=True) 9740681896Sopenharmony_ci 9840681896Sopenharmony_ci 9940681896Sopenharmony_cisys.excepthook = handle_exception 10040681896Sopenharmony_ci 10140681896Sopenharmony_ci 10240681896Sopenharmony_ciclass VendorExpandError(OSError): 10340681896Sopenharmony_ci """ 10440681896Sopenharmony_ci Vendor extended exception class. 10540681896Sopenharmony_ci Script interfaces are not completely overriden. 10640681896Sopenharmony_ci """ 10740681896Sopenharmony_ci def __init__(self, script_class, func_name): 10840681896Sopenharmony_ci super().__init__() 10940681896Sopenharmony_ci self.script_class = script_class 11040681896Sopenharmony_ci self.func_name = func_name 11140681896Sopenharmony_ci 11240681896Sopenharmony_ci def __str__(self, ): 11340681896Sopenharmony_ci return ('%s Vendor expansion does not override function %s' % 11440681896Sopenharmony_ci (self.script_class, self.func_name)) 115