|
| 1 | +# Copyright (C) 2025 Intel Corporation |
| 2 | +# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 | +# See LICENSE.TXT |
| 4 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | + |
| 6 | +import logging |
| 7 | +import sys |
| 8 | +from enum import Enum |
| 9 | + |
| 10 | +class LogLevel(Enum): |
| 11 | + DEBUG = logging.DEBUG |
| 12 | + INFO = logging.INFO |
| 13 | + WARNING = logging.WARNING |
| 14 | + ERROR = logging.ERROR |
| 15 | + CRITICAL = logging.CRITICAL |
| 16 | + |
| 17 | + @staticmethod |
| 18 | + def from_string(level_str): |
| 19 | + level_map = { |
| 20 | + "debug": LogLevel.DEBUG, |
| 21 | + "info": LogLevel.INFO, |
| 22 | + "warning": LogLevel.WARNING, |
| 23 | + "error": LogLevel.ERROR, |
| 24 | + "critical": LogLevel.CRITICAL |
| 25 | + } |
| 26 | + return level_map.get(level_str.lower(), LogLevel.INFO) |
| 27 | + |
| 28 | +class BenchmarkLogger: |
| 29 | + """Logger for the Unified Runtime Benchmark Runner. |
| 30 | + |
| 31 | + This logger provides different log levels (DEBUG, INFO, WARNING, ERROR, CRITICAL) |
| 32 | + that can be controlled via command-line arguments. |
| 33 | + """ |
| 34 | + |
| 35 | + _instance = None |
| 36 | + |
| 37 | + def __new__(cls): |
| 38 | + if cls._instance is None: |
| 39 | + cls._instance = super(BenchmarkLogger, cls).__new__(cls) |
| 40 | + cls._instance._initialize_logger() |
| 41 | + return cls._instance |
| 42 | + |
| 43 | + def _initialize_logger(self): |
| 44 | + """Initialize the logger with the appropriate log level.""" |
| 45 | + self.logger = logging.getLogger("ur_benchmarks") |
| 46 | + |
| 47 | + # Create console handler |
| 48 | + console_handler = logging.StreamHandler(sys.stdout) |
| 49 | + |
| 50 | + # Set default log level (INFO) |
| 51 | + self.logger.setLevel(logging.INFO) |
| 52 | + console_handler.setLevel(logging.INFO) |
| 53 | + |
| 54 | + # Create formatter |
| 55 | + formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') |
| 56 | + console_handler.setFormatter(formatter) |
| 57 | + |
| 58 | + # Add the handler to the logger |
| 59 | + self.logger.addHandler(console_handler) |
| 60 | + |
| 61 | + def debug(self, message): |
| 62 | + """Log a debug message.""" |
| 63 | + self.logger.debug(message) |
| 64 | + |
| 65 | + def info(self, message): |
| 66 | + """Log an info message.""" |
| 67 | + self.logger.info(message) |
| 68 | + |
| 69 | + def warning(self, message): |
| 70 | + """Log a warning message.""" |
| 71 | + self.logger.warning(message) |
| 72 | + |
| 73 | + def error(self, message): |
| 74 | + """Log an error message.""" |
| 75 | + self.logger.error(message) |
| 76 | + |
| 77 | + def critical(self, message): |
| 78 | + """Log a critical message.""" |
| 79 | + self.logger.critical(message) |
| 80 | + |
| 81 | +# Global logger instance |
| 82 | +log = BenchmarkLogger() |
0 commit comments