diff --git a/src/jaxsim/logging.py b/src/jaxsim/logging.py index 00dc56477..7e0e7fe8e 100644 --- a/src/jaxsim/logging.py +++ b/src/jaxsim/logging.py @@ -1,8 +1,45 @@ import enum +import inspect import logging +import os +import warnings import coloredlogs + +class JaxSimWarning(UserWarning): + pass + + +_original_showwarning = warnings.showwarning +_jaxsim_root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) + + +def pretty_jaxsim_warning(message, category, filename, lineno, file=None, line=None): + try: + caller_frame = inspect.stack()[2] + caller_file = caller_frame.filename + except Exception: + caller_file = filename + + if caller_file.startswith(_jaxsim_root_dir): + print(f"\033[93m⚠️ {category.__name__}:\033[0m {message}") + print(f" → {filename}:{lineno}") + else: + _original_showwarning(message, category, filename, lineno, file, line) + + +# Register filter & formatter only for JaxSimWarning +# and configure it to show each warning only once +warnings.showwarning = pretty_jaxsim_warning +warnings.filterwarnings("once") + + +# Utility function to issue a JaxSim warning +def jaxsim_warn(msg): + warnings.warn(msg, category=JaxSimWarning, stacklevel=2) + + LOGGER_NAME = "jaxsim" diff --git a/src/jaxsim/parsers/descriptions/model.py b/src/jaxsim/parsers/descriptions/model.py index 6b3fdb02d..714df5791 100644 --- a/src/jaxsim/parsers/descriptions/model.py +++ b/src/jaxsim/parsers/descriptions/model.py @@ -5,6 +5,7 @@ from collections.abc import Sequence from jaxsim import logging +from jaxsim.logging import jaxsim_warn from ..kinematic_graph import KinematicGraph, KinematicGraphTransforms, RootPose from .collision import CollidablePoint, CollisionShape @@ -164,7 +165,7 @@ def reduce(self, considered_joints: Sequence[str]) -> ModelDescription: A `ModelDescription` instance that only includes the considered joints. """ - logging.warning( + jaxsim_warn( "The joint order in the model description is not preserved when reducing " "the model. Consider using the `names_to_indices` method to get the correct " "order of the joints, or use the `joint_names()` method to inspect the internal joint ordering."