2024-01-30 17:22:11 -06:00
|
|
|
##############################################################################bl
|
|
|
|
|
# MIT License
|
|
|
|
|
#
|
|
|
|
|
# Copyright (c) 2021 - 2024 Advanced Micro Devices, Inc. All Rights Reserved.
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
# SOFTWARE.
|
|
|
|
|
##############################################################################el
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
from utils.utils import trace_logger
|
|
|
|
|
|
|
|
|
|
# Define the colors
|
|
|
|
|
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
|
|
|
|
|
RESET_SEQ = "\033[0m"
|
|
|
|
|
COLOR_SEQ = "\033[1;%dm"
|
|
|
|
|
|
|
|
|
|
COLORS = {
|
2024-03-04 12:57:25 -06:00
|
|
|
"WARNING": YELLOW,
|
|
|
|
|
"INFO": GREEN,
|
|
|
|
|
"DEBUG": BLUE,
|
|
|
|
|
"CRITICAL": YELLOW,
|
|
|
|
|
"ERROR": RED,
|
2024-01-30 17:22:11 -06:00
|
|
|
}
|
|
|
|
|
|
2024-03-04 12:57:25 -06:00
|
|
|
|
2024-01-30 17:22:11 -06:00
|
|
|
# Define the formatter
|
|
|
|
|
class ColoredFormatter(logging.Formatter):
|
|
|
|
|
def format(self, record):
|
|
|
|
|
levelname = record.levelname
|
|
|
|
|
if levelname in COLORS:
|
|
|
|
|
levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + levelname + RESET_SEQ
|
|
|
|
|
record.levelname = levelname_color
|
|
|
|
|
return logging.Formatter.format(self, record)
|
|
|
|
|
|
2024-03-04 12:57:25 -06:00
|
|
|
|
2024-01-30 17:22:11 -06:00
|
|
|
# Setup logger
|
2024-02-08 12:23:20 -06:00
|
|
|
def setup_logging(verbose):
|
2024-01-30 17:22:11 -06:00
|
|
|
# register a trace level logger
|
|
|
|
|
logging.TRACE = logging.DEBUG - 5
|
|
|
|
|
logging.addLevelName(logging.TRACE, "TRACE")
|
|
|
|
|
setattr(logging, "TRACE", logging.TRACE)
|
|
|
|
|
setattr(logging, "trace", trace_logger)
|
|
|
|
|
|
2024-02-08 12:23:20 -06:00
|
|
|
# default: set loglevel based on selected verbosity
|
|
|
|
|
levels = [logging.INFO, logging.DEBUG, logging.TRACE]
|
|
|
|
|
loglevel = levels[min(verbose, len(levels) - 1)] # cap to last level index
|
|
|
|
|
# optional: override of default loglevel via env variable
|
2024-01-30 17:22:11 -06:00
|
|
|
if "OMNIPERF_LOGLEVEL" in os.environ.keys():
|
2024-03-04 12:57:25 -06:00
|
|
|
loglevel = os.environ["OMNIPERF_LOGLEVEL"]
|
|
|
|
|
if loglevel in {"DEBUG", "debug"}:
|
2024-01-30 17:22:11 -06:00
|
|
|
loglevel = logging.DEBUG
|
2024-03-04 12:57:25 -06:00
|
|
|
elif loglevel in {"TRACE", "trace"}:
|
2024-01-30 17:22:11 -06:00
|
|
|
loglevel = logging.TRACE
|
2024-03-04 12:57:25 -06:00
|
|
|
elif loglevel in {"INFO", "info"}:
|
2024-01-30 17:22:11 -06:00
|
|
|
loglevel = logging.INFO
|
2024-03-04 12:57:25 -06:00
|
|
|
elif loglevel in {"ERROR", "error"}:
|
2024-01-30 17:22:11 -06:00
|
|
|
loglevel = logging.ERROR
|
|
|
|
|
else:
|
|
|
|
|
print("Ignoring unsupported OMNIPERF_LOGLEVEL setting (%s)" % loglevel)
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
2024-03-04 12:57:25 -06:00
|
|
|
formatter = ColoredFormatter("%(levelname)s - %(message)s")
|
2024-01-30 17:22:11 -06:00
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
|
|
|
handler.setFormatter(formatter)
|
2024-03-04 12:57:25 -06:00
|
|
|
logging.basicConfig(level=loglevel, handlers=[handler])
|