Skip to content

Commit

Permalink
Expand logging utilities per the infinite wisdom of eholum
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-bass committed Nov 3, 2024
1 parent 98a2c1d commit 7889f4e
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pyrobosim/examples/demo_pddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def start_planner(world, args):
# Wait for the GUI to load
while not world.has_gui:
time.sleep(1.0)
time.sleep(0.5) # Extra time for log messages to not interfere with prompt

if args.example == "01_simple":
# Task specification for simple example.
Expand Down Expand Up @@ -77,7 +78,6 @@ def start_planner(world, args):
print(f"Invalid example: {args.example}")
return

time.sleep(0.5)
input("Press Enter to start planning.")
robot = world.robots[0]
plan = planner.plan(
Expand Down
21 changes: 0 additions & 21 deletions pyrobosim/pyrobosim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1 @@
""" ROS 2 enabled 2D mobile robot simulator for behavior prototyping. """

import logging

# Define a general PyRoboSim logger.
global_logger = logging.getLogger("pyrobosim")
global_logger.setLevel(logging.INFO)
log_formatter = logging.Formatter("[%(name)s] %(levelname)s: %(message)s")
console_handler = logging.StreamHandler()
console_handler.setFormatter(log_formatter)
global_logger.addHandler(console_handler)
global_logger.propagate = True


def get_global_logger():
"""
Returns a global logger for PyRoboSim.
:return: The PyRoboSim global logger instance.
:rtype: :class:`logging.Logger`.
"""
return global_logger
2 changes: 1 addition & 1 deletion pyrobosim/pyrobosim/core/dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import copy
import numpy as np

from .. import get_global_logger
from ..utils.logging import get_global_logger
from ..utils.pose import Pose


Expand Down
9 changes: 2 additions & 7 deletions pyrobosim/pyrobosim/core/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ..manipulation.grasping import Grasp
from ..planning.actions import ExecutionResult, ExecutionStatus
from ..utils.knowledge import query_to_entity
from ..utils.logging import create_logger
from ..utils.polygon import sample_from_polygon, transform_polygon
from ..utils.pose import Pose

Expand Down Expand Up @@ -84,13 +85,7 @@ def __init__(
raise ValueError("Robots cannot be named 'world'.")

# Logger for this robot
self.logger = logging.getLogger(self.name)
self.logger.setLevel(logging.INFO)
log_formatter = logging.Formatter("[%(name)s] %(levelname)s: %(message)s")
console_handler = logging.StreamHandler()
console_handler.setFormatter(log_formatter)
self.logger.addHandler(console_handler)
self.logger.propagate = True
self.logger = create_logger(self.name)

# Dynamics properties
self.dynamics = RobotDynamics2D(
Expand Down
11 changes: 2 additions & 9 deletions pyrobosim/pyrobosim/core/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .robot import Robot
from ..planning.actions import ExecutionResult, ExecutionStatus
from ..utils.general import InvalidEntityCategoryException
from ..utils.logging import create_logger
from ..utils.pose import Pose
from ..utils.knowledge import (
apply_resolution_strategy,
Expand Down Expand Up @@ -42,15 +43,7 @@ def __init__(
self.name = name
self.wall_height = wall_height
self.source_file = None

# Logger for the world
self.logger = logging.getLogger(self.name)
self.logger.setLevel(logging.INFO)
log_formatter = logging.Formatter("[%(name)s] %(levelname)s: %(message)s")
console_handler = logging.StreamHandler()
console_handler.setFormatter(log_formatter)
self.logger.addHandler(console_handler)
self.logger.propagate = True
self.logger = create_logger(self.name)

# Connected apps
self.has_gui = False
Expand Down
2 changes: 1 addition & 1 deletion pyrobosim/pyrobosim/core/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from .robot import Robot
from .world import World
from .. import get_global_logger
from ..navigation import ConstantVelocityExecutor, get_planner_class
from ..planning.actions import ExecutionOptions
from ..utils.general import replace_special_yaml_tokens
from ..utils.logging import get_global_logger
from ..utils.pose import Pose


Expand Down
2 changes: 1 addition & 1 deletion pyrobosim/pyrobosim/navigation/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import threading

from ..planning.actions import ExecutionResult, ExecutionStatus
from .. import get_global_logger
from ..utils.logging import get_global_logger
from ..utils.motion import Path
from ..utils.trajectory import get_constant_speed_trajectory, interpolate_trajectory

Expand Down
2 changes: 1 addition & 1 deletion pyrobosim/pyrobosim/navigation/occupancy_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from PIL import Image
import yaml

from .. import get_global_logger
from ..utils.logging import get_global_logger


class OccupancyGrid:
Expand Down
2 changes: 1 addition & 1 deletion pyrobosim/pyrobosim/utils/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import numpy as np

from .. import get_global_logger
from ..utils.logging import get_global_logger


def apply_resolution_strategy(entity_list, resolution_strategy, robot=None):
Expand Down
55 changes: 55 additions & 0 deletions pyrobosim/pyrobosim/utils/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Logging utilities for PyRoboSim.
"""

import logging


def create_logger(name, level=logging.INFO):
"""
Defines a general logger for use with PyRoboSim.
:param name: The name of the logger.
:type name: str
:param level: The level of the logger.
:type level: int
:return: A logger instance.
:rtype: :class:`logging.Logger`.
"""
logger = logging.getLogger(name)
logger.setLevel(level)

# TODO: Consider configuring console vs. file logging at some point.
log_formatter = logging.Formatter("[%(name)s] %(levelname)s: %(message)s")
console_handler = logging.StreamHandler()
console_handler.setFormatter(log_formatter)
logger.addHandler(console_handler)

# Needed to propagate to unit tests via the caplog fixture.
logger.propagate = True

return logger


PYROBOSIM_GLOBAL_LOGGER = create_logger("pyrobosim")


def get_global_logger():
"""
Returns the global logger for PyRoboSim.
:return: The PyRoboSim global logger instance.
:rtype: :class:`logging.Logger`.
"""
return PYROBOSIM_GLOBAL_LOGGER


def set_global_logger(logger):
"""
Sets the global PyRoboSim logger to another specified logger.
:param logger: The logger to use as the new global logger.
:type logger: :class:`logging.Logger`
"""
global PYROBOSIM_GLOBAL_LOGGER
PYROBOSIM_GLOBAL_LOGGER = logger
2 changes: 1 addition & 1 deletion pyrobosim/pyrobosim/utils/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from .general import replace_special_yaml_tokens
from .pose import Pose, rot2d
from .. import get_global_logger
from ..utils.logging import get_global_logger


def add_coords(coords, offset):
Expand Down
2 changes: 1 addition & 1 deletion pyrobosim/pyrobosim/utils/search_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np

from .motion import Path
from .. import get_global_logger
from ..utils.logging import get_global_logger


class Node:
Expand Down
2 changes: 1 addition & 1 deletion pyrobosim/pyrobosim/utils/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from scipy.spatial.transform import Slerp, Rotation

from .pose import Pose, wrap_angle
from .. import get_global_logger
from ..utils.logging import get_global_logger


class Trajectory:
Expand Down
2 changes: 2 additions & 0 deletions pyrobosim_ros/pyrobosim_ros/ros_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from rclpy.node import Node

from geometry_msgs.msg import Twist
from pyrobosim.utils.logging import set_global_logger
from pyrobosim_msgs.action import (
DetectObjects,
ExecuteTaskAction,
Expand Down Expand Up @@ -169,6 +170,7 @@ def set_world(self, world):
self.world.ros_node = self
self.world.has_ros_node = True

set_global_logger(get_logger("pyrobosim"))
self.world.logger = get_logger(self.world.name)
self.world.logger.info("Configured ROS node.")

Expand Down

0 comments on commit 7889f4e

Please sign in to comment.