1#!/usr/bin/env python3 2# 3# Copyright (c) 2021 Intel Corporation 4# 5# SPDX-License-Identifier: Apache-2.0 6 7""" 8Abstract Class for Dictionary-based Logging Parsers 9""" 10 11import abc 12from colorama import Fore 13 14from .data_types import DataTypes 15 16LOG_LEVELS = [ 17 ('none', Fore.WHITE), 18 ('err', Fore.RED), 19 ('wrn', Fore.YELLOW), 20 ('inf', Fore.GREEN), 21 ('dbg', Fore.BLUE) 22] 23 24def get_log_level_str_color(lvl): 25 """Convert numeric log level to string""" 26 if lvl < 0 or lvl >= len(LOG_LEVELS): 27 return ("unk", Fore.WHITE) 28 29 return LOG_LEVELS[lvl] 30 31 32def formalize_fmt_string(fmt_str): 33 """Replace unsupported formatter""" 34 new_str = fmt_str 35 36 for spec in ['d', 'i', 'o', 'u', 'x', 'X']: 37 # Python doesn't support %ll for integer specifiers, so remove extra 'l' 38 new_str = new_str.replace("%ll" + spec, "%l" + spec) 39 40 if spec in ['x', 'X']: 41 new_str = new_str.replace("%#ll" + spec, "%#l" + spec) 42 43 # Python doesn't support %hh for integer specifiers, so remove extra 'h' 44 new_str = new_str.replace("%hh" + spec, "%h" + spec) 45 46 # No %p for pointer either, so use %x 47 new_str = new_str.replace("%p", "0x%x") 48 49 return new_str 50 51 52class LogParser(abc.ABC): 53 """Abstract class of log parser""" 54 def __init__(self, database): 55 self.database = database 56 57 self.data_types = DataTypes(self.database) 58 59 60 @abc.abstractmethod 61 def parse_log_data(self, logdata, debug=False): 62 """Parse log data""" 63 return None 64