Michael Schaffner | 8188c6d | 2021-10-26 22:46:05 -0700 | [diff] [blame] | 1 | # Copyright lowRISC contributors. |
| 2 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | r''' |
| 5 | Message bucket class that encapsulates information such as bucket |
| 6 | category, bucket severity, the associated label for the results table, and |
| 7 | a list with message signatures. |
| 8 | ''' |
| 9 | |
| 10 | import copy |
| 11 | |
| 12 | |
| 13 | class MsgBucket(): |
| 14 | |
| 15 | # Known severity levels and color map for Dvsim summary tables. |
| 16 | SEVERITIES = { |
| 17 | 'info': 'I', |
| 18 | 'review': 'W', |
| 19 | 'warning': 'W', |
| 20 | 'error': 'E', |
| 21 | 'fatal': 'E' |
| 22 | } |
| 23 | |
| 24 | @classmethod |
| 25 | def severity_is_known(cls, severity: str) -> bool: |
| 26 | '''Returns true if the severity is known''' |
| 27 | return severity in cls.SEVERITIES |
| 28 | |
| 29 | def __init__(self, |
| 30 | category: str, |
| 31 | severity: str, |
| 32 | label: str = None) -> None: |
| 33 | if not MsgBucket.severity_is_known(severity): |
| 34 | RuntimeError(f'Unknown severity {severity}') |
| 35 | self.category = category |
| 36 | self.severity = severity |
| 37 | self.label = f'{category} {severity}s'.title() if not label else label |
| 38 | self.signatures = [] |
| 39 | |
| 40 | def clear(self) -> None: |
| 41 | '''Clears the signatures list''' |
| 42 | self.signatures = [] |
| 43 | |
| 44 | def count(self) -> int: |
| 45 | '''Return number of signatures''' |
| 46 | return len(self.signatures) |
| 47 | |
| 48 | def count_md(self, colmap: bool = True) -> str: |
| 49 | '''Return count string with optional colormap applied''' |
| 50 | countstr = str(self.count()) |
| 51 | if colmap: |
| 52 | countstr += ' ' + self.SEVERITIES[self.severity] |
| 53 | return countstr |
| 54 | |
| 55 | def merge(self, other) -> None: |
| 56 | '''Merge other bucket into this one''' |
| 57 | |
| 58 | if self.category != other.category: |
| 59 | raise RuntimeError(f'Category {other.category} does not match ' |
| 60 | f'{self.category} for bucket {self.label}') |
| 61 | |
| 62 | elif self.severity != other.severity: |
| 63 | raise RuntimeError(f'Severity {other.severity} does not match ' |
| 64 | f'{self.severity} for bucket {self.label}') |
| 65 | else: |
| 66 | self.signatures.extend(copy.deepcopy(other.signatures)) |
| 67 | |
| 68 | def __add__(self, other): |
| 69 | '''Merges two message buckets into one''' |
| 70 | mb = copy.deepcopy(self) |
| 71 | mb.merge(other) |
| 72 | return mb |