import sys
import logging
RICH=True
try:
from rich.style import Style
from rich.text import Text
from rich.highlighter import Highlighter
from rich.logging import RichHandler
except ImportError:
RICH=False
def _combine_regex(*regexes: str) -> str:
"""Combine a number of regexes in to a single regex.
Returns:
str: New regex with all regexes ORed together.
"""
return "|".join(regexes)
if RICH:
class AdvancedHighlighter(Highlighter):
"""Highlights the text typically produced from ``__repr__`` methods."""
base_style = "repr."
re_highlights = [
r"(?P<tag_start><)(?P<tag_name>[-\w.:|]*)(?P<tag_contents>[\w\W]*)(?P<tag_end>>)",
r"(?P<brace>[][{}()])",
r'(?P<attrib_name>\B-{1,2}[\w_-]{1,50})=(?P<attrib_value>"?[\w_+=-]+"?)?',
_combine_regex(
r"(?P<ipv4>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})",
r"(?P<ipv6>([A-Fa-f0-9]{1,4}::?){7}[A-Fa-f0-9]{1,4})",
r"(?P<eui64>(?:[0-9A-Fa-f]{1,2}-){7}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{1,2}:){7}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{4}\.){3}[0-9A-Fa-f]{4})",
r"(?P<eui48>(?:[0-9A-Fa-f]{1,2}-){5}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})",
r"(?P<uuid>[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})",
r"\b(?P<bool_true>True)\b|\b(?P<bool_false>False)\b|\b(?P<none>None)\b",
r"(?P<ellipsis>\.\.\.)",
r"(?P<number_complex>(?<!\w)(?:\-?[0-9]+\.?[0-9]*(?:e[-+]?\d+?)?)(?:[-+](?:[0-9]+\.?[0-9]*(?:e[-+]?\d+)?))?j)",
r"(?P<number>(?<!\w)\-?[0-9]+\.?[0-9]*(e[-+]?\d+?)?\b|0x[0-9a-fA-F]*)",
r"-I(?P<path>((\.\.)?|\b[-\w_]+)(/[-\w._+]+)*\/)(?P<filename>[-\w._+]*)?",
r"(?<![\\\w])(?P<str>b?'''.*?(?<!\\)'''|b?'.*?(?<!\\)'|b?\"\"\".*?(?<!\\)\"\"\"|b?\".*?(?<!\\)\")",
r"(?P<url>(file|https|http|ws|wss)://[-0-9a-zA-Z$_+!`(),.?/;:&=%#]*)",
),
]
def highlight(self, text: Text) -> None:
highlight_words = text.highlight_words
highlight_regex = text.highlight_regex
for re_highlight in self.re_highlights:
highlight_regex(re_highlight, style_prefix=self.base_style)
highlight_regex(r"\B(?P<attrib_name>-(D|f|W)[\w+_-]{1,50})", style_prefix=self.base_style)
highlight_words(["error:", "warning:", "ERROR", "invalid"], style=Style(color="bright_red", bold=True), case_sensitive=True)
highlight_words(["failed:", "failed"], style=Style(color="bright_red", bold=True), case_sensitive=False)
highlight_words(["successfully", "success", "done."], style=Style(color="bright_green", bold=True), case_sensitive=False)
highlight_words([" CC ", " CXX ", " ASM ", " AR ", " SOLINK ", " LINK ", " STAMP ", " COPY "], style=Style(color="yellow", bold=True), case_sensitive=True)
highlight_words(["[GN INFO]"], style=Style(color="blue", bold=True), case_sensitive=False)
highlight_words(["[GN DEBUG]"], style=Style(color="dark_blue", bold=True), case_sensitive=False)
highlight_words(["[GN ERROR]", "[GN WARNING]"], style=Style(color="red", bold=True), case_sensitive=False)
highlight_regex(r"\B(\^~+)", style=Style(color="red", bold=True))
highlight_regex(r"\B(\^-+)", style=Style(color="red", bold=True))
class LoggerManager:
"""
LoggerManager is a singleton.
"""
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
pass
def setup_level(self, log_level: str):
if RICH:
log_level = log_level.upper()
try:
handler = RichHandler(rich_tracebacks=True,highlighter=AdvancedHighlighter())
logging.basicConfig(
level=log_level,
format="%(message)s",
datefmt="[%X]",
handlers=[handler]
)
except:
self.get_logger().error("Invalid log level: '%s'", log_level)
sys.exit(1)
self.log_level = log_level
def add_file_logger(self, out_path, expire_time=2) -> None:
pass
def get_logger(self) -> logging.Logger:
return logging.getLogger("rich")
if RICH:
logger = LoggerManager().get_logger()
else:
class logger:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
pass
def debug(args):
print(args)
def warning(args):
print(args)
def info(args):
print(args)
def error(args):
print(args)