0
$\begingroup$

Rosanswers logo

Sometimes generating certain debug information may be computationally burdensome and therefore it may be desirable to make this dependent on the current log level.

Thus I was wondering, what would be the correct way to retrieve the loglevel from within a node using rospy (in Kinetic)? Ultimately, I would want to be able to do something like this:

if current_log_level == rospy.DEBUG:
  result = burdensome_stats_generation()
  rospy.logdebug("Very informative output: %s", res)

Originally posted by igor on ROS Answers with karma: 13 on 2018-04-24

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

This uses the python logging (https://docs.python.org/2.4/lib/module-logging.html) underneath rospy logging:

#!/usr/bin/env python

import rospy
import logging

rospy.init_node('log_test')

logger = logging.getLogger("rosout")

logger.setLevel(logging.INFO)
level = logger.getEffectiveLevel()
rospy.loginfo("info test, level = " + str(level))
rospy.logdebug("debug test, level = level " + str(level))

logger.setLevel(logging.DEBUG)
level = logger.getEffectiveLevel()
rospy.loginfo("info test, level = " + str(level))
rospy.logdebug("debug test, level = level " + str(level))

Old suggestions, maybe I'll test them out:

rospy service call to get_loggers service. (Maybe some extra things to do to properly call service from same node that is providing it)

I don't think there is anything in python like a C macro that could avoid evaluating a function parameter if debug wasn't on (does the actual C++ debug do anything like that?), it would be nice if this could be done:

rospy.logdebug("Very informative output: %s",  burdensome_stats_generation())

Originally posted by lucasw with karma: 8729 on 2018-04-24

This answer was ACCEPTED on the original site

Post score: 2

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.