Skip to content

Commit

Permalink
Add log level support to spawner node (ros-controls#972)
Browse files Browse the repository at this point in the history
* Add log-level support for spawner node

* Fix check for printing stdout/stderr output from load_parameter_file

* Fix pre-commit errors
  • Loading branch information
bijoua29 authored Mar 19, 2023
1 parent 839344c commit 8252a85
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions controller_manager/controller_manager/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import sys
import time
import warnings
import io
from contextlib import redirect_stdout, redirect_stderr

from controller_manager import (
configure_controller,
Expand Down Expand Up @@ -182,6 +184,13 @@ def main(args=None):
default=10,
type=int,
)
parser.add_argument(
"--log-level",
help="Log level for spawner node",
required=False,
choices=["debug", "info", "warn", "error", "fatal"],
default="info",
)

command_line_args = rclpy.utilities.remove_ros_args(args=sys.argv)[1:]
args = parser.parse_args(command_line_args)
Expand All @@ -191,6 +200,15 @@ def main(args=None):
param_file = args.param_file
controller_type = args.controller_type
controller_manager_timeout = args.controller_manager_timeout
log_level = args.log_level

loglevel_to_severity = {
"debug": rclpy.logging.LoggingSeverity.DEBUG,
"info": rclpy.logging.LoggingSeverity.INFO,
"warn": rclpy.logging.LoggingSeverity.WARN,
"error": rclpy.logging.LoggingSeverity.ERROR,
"fatal": rclpy.logging.LoggingSeverity.FATAL,
}

if param_file and not os.path.isfile(param_file):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), param_file)
Expand All @@ -200,6 +218,8 @@ def main(args=None):
prefixed_controller_name = controller_namespace + "/" + controller_name

node = Node("spawner_" + controller_name)
rclpy.logging.set_logger_level("spawner_" + controller_name, loglevel_to_severity[log_level])

if not controller_manager_name.startswith("/"):
spawner_namespace = node.get_namespace()
if spawner_namespace != "/":
Expand Down Expand Up @@ -264,12 +284,20 @@ def main(args=None):
)

if param_file:
load_parameter_file(
node=node,
node_name=prefixed_controller_name,
parameter_file=param_file,
use_wildcard=True,
)
# load_parameter_file writes to stdout/stderr. Here we capture that and use node logging instead
with redirect_stdout(io.StringIO()) as f_stdout, redirect_stderr(
io.StringIO()
) as f_stderr:
load_parameter_file(
node=node,
node_name=prefixed_controller_name,
parameter_file=param_file,
use_wildcard=True,
)
if f_stdout.getvalue():
node.get_logger().info(bcolors.OKCYAN + f_stdout.getvalue() + bcolors.ENDC)
if f_stderr.getvalue():
node.get_logger().error(bcolors.FAIL + f_stderr.getvalue() + bcolors.ENDC)
node.get_logger().info(
bcolors.OKCYAN
+ 'Loaded parameters file "'
Expand Down

0 comments on commit 8252a85

Please sign in to comment.