1# -*- coding: utf-8 -*- 2# Copyright (c) 2023 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14#!/usr/bin/python3 15import math 16import enum 17import time 18import logging 19import threading 20from apl_config import * 21 22log_tag = 'common' 23 24apl_file_log = logging.FileHandler(filename=APL_LOG_FILE, mode='a', encoding='utf-8') 25fmt = logging.Formatter(fmt="%(asctime)s %(message)s", datefmt='%Y-%m-%d %H:%M:%S %a') 26apl_file_log.setFormatter(fmt) 27 28# 定义日志 29apl_logger = logging.Logger(name = 'apl_compare_log', level=logging.INFO) 30apl_logger.addHandler(apl_file_log) 31 32class ErrorType(enum.Enum): 33 not_in_apl_table = 1 34 apl_is_invalid = 2 35 36class ApiLevel(enum.Enum): 37 normal = 1 38 system_basic = 2 39 system_core = 3 40 41class LogLevel(enum.Enum): 42 Error = 1 43 Info = 2 44 45class AplCompareException(Exception): 46 def __init__(self, msg): 47 self.msg = msg 48 49class AplCompareThread(threading.Thread): 50 def __init__(self, func, args=()): 51 super(AplCompareThread, self).__init__() 52 self.func = func 53 self.args = args 54 self.result = None 55 def run(self): 56 self.result = self.func(*self.args) 57 def get_result(self): 58 threading.Thread.join(self) 59 try: 60 return self.result 61 except Exception as e: 62 apl_set_log_content(LogLevel(1).name, log_tag, '{}'.format(e.args[0])) 63 return None 64 65def apl_log(msg): 66 # 写日志 67 apl_logger.info(msg) 68 69def apl_set_log_content(level, tag, msg): 70 log_content = timestamp() + ' {}'.format(level) + ' [{}]'.format(tag) + ' {}'.format(msg) 71 print(log_content) 72 apl_log(log_content) 73 return(log_content) 74 75def set_error_record(name,error): 76 return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())+' %(name)-50s: %(error)-50s\n'%{'name':name,'error':error} 77 78def set_map(results): 79 if results == None: 80 return None 81 res_map = {} 82 for result in results: 83 res_map[result[0]] = set_value(result[1:]) 84 return res_map 85 86def set_value(result): 87 value = [] 88 for res in result: 89 if math.isnan(res): 90 res = 0 91 value.append(res) 92 return value 93 94def timestamp(): 95 return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) 96