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 12import re 13 14from colorama import Fore 15 16from .data_types import DataTypes 17 18LOG_LEVELS = [ 19 ('none', Fore.WHITE), 20 ('err', Fore.RED), 21 ('wrn', Fore.YELLOW), 22 ('inf', Fore.GREEN), 23 ('dbg', Fore.BLUE), 24] 25 26 27def get_log_level_str_color(lvl): 28 """Convert numeric log level to string""" 29 if lvl < 0 or lvl >= len(LOG_LEVELS): 30 return ("unk", Fore.WHITE) 31 32 return LOG_LEVELS[lvl] 33 34 35def formalize_fmt_string(fmt_str): 36 """Replace unsupported formatter""" 37 new_str = fmt_str 38 39 for spec in ['d', 'i', 'o', 'u', 'x', 'X']: 40 # Python doesn't support %ll for integer specifiers, so remove extra 'l' 41 new_str = re.sub(r'%(\#?\d*)ll' + spec, r'%\1l' + spec, new_str) 42 43 if spec in ['x', 'X']: 44 new_str = re.sub(r'%\#(\d*)ll' + spec, r'%#\1l' + spec, new_str) 45 46 # Python doesn't support %hh for integer specifiers, so remove extra 'h' 47 new_str = re.sub(r'%(\#?\d*)hh' + spec, r'%\1h' + spec, new_str) 48 49 # No %p for pointer either, so use %x 50 new_str = new_str.replace("%p", "0x%x") 51 52 # No %z support, use %d instead 53 new_str = new_str.replace("%z", "%d") 54 55 return new_str 56 57 58class LogParser(abc.ABC): 59 """Abstract class of log parser""" 60 61 def __init__(self, database): 62 self.database = database 63 64 self.data_types = DataTypes(self.database) 65 66 @abc.abstractmethod 67 def parse_log_data(self, logdata, debug=False): 68 """Parse log data""" 69 return None 70