Skip to content

Commit

Permalink
Merge pull request cram2#225 from AbdelrhmanBassiouny/log_level
Browse files Browse the repository at this point in the history
[LoggingLevel] Adding setting log level.
  • Loading branch information
Tigul authored Nov 25, 2024
2 parents b0a95db + ee88841 commit e75a355
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/pycram/datastructures/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ class ImageEnum(Enum):
CHAIR = 37


class LoggerLevel(Enum):
"""
Enum for the different logger levels.
"""
DEBUG = 'debug'
INFO = 'info'
WARN = 'warn'
ERROR = 'error'
FATAL = 'fatal'


class VirtualMobileBaseJointName(Enum):
"""
Enum for the joint names of the virtual mobile base.
Expand Down
49 changes: 42 additions & 7 deletions src/pycram/ros/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
from __future__ import annotations

import rospy
from rospy.logger_level_service_caller import LoggerLevelServiceCaller
import inspect
from pathlib import Path

from typing_extensions import TYPE_CHECKING

if TYPE_CHECKING:
from ..datastructures.enums import LoggerLevel


PYCRAM_LOGGER_NAME = "pycram"
logger_level_service_caller = LoggerLevelServiceCaller()


def _get_caller_method_name():
"""
Expand All @@ -12,6 +24,7 @@ def _get_caller_method_name():
"""
return inspect.stack()[2][3]


def _get_caller_method_line():
"""
Get the line of the method that called the function from which this function is called. It is intended as a helper
Expand All @@ -21,6 +34,7 @@ def _get_caller_method_line():
"""
return inspect.stack()[2][2]


def _get_caller_file_name():
"""
Get the file name of the method that called the function from which this function is called. It is intended as a helper
Expand All @@ -32,33 +46,54 @@ def _get_caller_file_name():
return path.name


def set_logger_level(level: LoggerLevel):
"""
Set the logger level for the pycram logger.
:param level: The level to set the logger to, possible values are: rospy.DEBUG, rospy.INFO, rospy.WARN, rospy.ERROR,
rospy.FATAL.
"""
logger_level_service_caller.get_loggers(rospy.get_name())
logger_level_service_caller.send_logger_change_message(rospy.get_name(),
f"rosout.{PYCRAM_LOGGER_NAME}",
level.value)


def logwarn(message: str):
rospy.logwarn(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}")
rospy.logwarn(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}",
logger_name=PYCRAM_LOGGER_NAME)


def loginfo(message: str):
rospy.loginfo(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}")
rospy.loginfo(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}",
logger_name=PYCRAM_LOGGER_NAME)


def logerr(message: str):
rospy.logerr(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}")
rospy.logerr(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}",
logger_name=PYCRAM_LOGGER_NAME)


def logdebug(message: str):
rospy.logdebug(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}")


def logwarn_once(message: str):
rospy.logwarn_once(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}")
rospy.logwarn_once(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}",
logger_name=PYCRAM_LOGGER_NAME)


def loginfo_once(message: str):
rospy.loginfo_once(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}")
rospy.loginfo_once(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}",
logger_name=PYCRAM_LOGGER_NAME)


def logerr_once(message: str):
rospy.logerr_once(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}")
rospy.logerr_once(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}",
logger_name=PYCRAM_LOGGER_NAME)


def logdebug_once(message: str):
rospy.logdebug_once(f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}")
rospy.logdebug_once(
f"[{_get_caller_file_name()}:{_get_caller_method_line()}:{_get_caller_method_name()}] {message}",
logger_name=PYCRAM_LOGGER_NAME)
14 changes: 14 additions & 0 deletions test/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from bullet_world_testcase import BulletWorldTestCase
from pycram.ros.logging import set_logger_level, logwarn, logerr
from pycram.datastructures.enums import LoggerLevel


class TestLogging(BulletWorldTestCase):
"""Testcase for the pycram logging functions."""

def test_set_logger_level(self):
logwarn("This is a warning, it should not be printed")
logerr("This is an error, it should be printed")
set_logger_level(LoggerLevel.ERROR)
logwarn("This is a warning, it should not be printed")
logerr("This is an error, it should be printed")

0 comments on commit e75a355

Please sign in to comment.