Skip to content

Commit 345d41e

Browse files
committed
Create JaxSimWarning class and register custom warning
1 parent c28e89a commit 345d41e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/jaxsim/logging.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,45 @@
11
import enum
2+
import inspect
23
import logging
4+
import os
5+
import warnings
36

47
import coloredlogs
58

9+
10+
class JaxSimWarning(UserWarning):
11+
pass
12+
13+
14+
_original_showwarning = warnings.showwarning
15+
_jaxsim_root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
16+
17+
18+
def pretty_jaxsim_warning(message, category, filename, lineno, file=None, line=None):
19+
try:
20+
caller_frame = inspect.stack()[2]
21+
caller_file = caller_frame.filename
22+
except Exception:
23+
caller_file = filename
24+
25+
if caller_file.startswith(_jaxsim_root_dir):
26+
print(f"\033[93m⚠️ {category.__name__}:\033[0m {message}")
27+
print(f" → {filename}:{lineno}")
28+
else:
29+
_original_showwarning(message, category, filename, lineno, file, line)
30+
31+
32+
# Register filter & formatter only for JaxSimWarning
33+
# and configure it to show each warning only once
34+
warnings.showwarning = pretty_jaxsim_warning
35+
warnings.filterwarnings("once")
36+
37+
38+
# Utility function to issue a JaxSim warning
39+
def jaxsim_warn(msg):
40+
warnings.warn(msg, category=JaxSimWarning, stacklevel=2)
41+
42+
643
LOGGER_NAME = "jaxsim"
744

845

0 commit comments

Comments
 (0)