forked from nod-ai/shark-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libshortfin] Move the logging setup
This patch moves logging functions from nod-ai#181
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
libshortfin/python/shortfin/interop/support/logging_setup.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2024 Advanced Micro Devices, Inc. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
import logging | ||
import sys | ||
|
||
from _shortfin import lib as _sfl | ||
|
||
_LOG_FUNCTIONS = { | ||
logging.DEBUG: _sfl.log_debug, | ||
logging.INFO: _sfl.log_info, | ||
logging.WARNING: _sfl.log_warn, | ||
logging.ERROR: _sfl.log_error, | ||
logging.CRITICAL: _sfl.log_error, | ||
} | ||
|
||
logger = logging.getLogger("shortfin") | ||
logger.propagate = False | ||
|
||
|
||
class NativeHandler(logging.Handler): | ||
def emit(self, record): | ||
formatted = self.format(record) | ||
f = _LOG_FUNCTIONS.get(record.levelno) | ||
if f is not None: | ||
f(formatted) | ||
|
||
|
||
class NativeFormatter(logging.Formatter): | ||
def __init__(self): | ||
super().__init__("[%(filename)s:%(lineno)d] %(message)s") | ||
|
||
|
||
native_handler = NativeHandler() | ||
native_handler.setFormatter(NativeFormatter()) | ||
|
||
# TODO: Source from env vars. | ||
logger.setLevel(logging.DEBUG) | ||
logger.addHandler(native_handler) | ||
|
||
|
||
def configure_main_logger(module_suffix: str = "__main__") -> logging.Logger: | ||
"""Configures logging from a main entrypoint. | ||
Returns a logger that can be used for the main module itself. | ||
""" | ||
logging.root.addHandler(native_handler) | ||
logging.root.setLevel(logging.DEBUG) # TODO: source from env vars | ||
main_module = sys.modules["__main__"] | ||
return logging.getLogger(f"{main_module.__package__}.{module_suffix}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters