Package rospy :: Module logger_level_service_caller

Source Code for Module rospy.logger_level_service_caller

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2015, Chris Mansley, Open Source Robotics Foundation, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32   
 33  import rosgraph 
 34  import rosnode 
 35  import rospy 
 36  import rosservice 
 37   
 38   
39 -class ROSConsoleException(Exception):
40 41 """Base exception class of rosconsole-related errors.""" 42 43 pass
44 45
46 -class LoggerLevelServiceCaller(object):
47 48 """ 49 Handles service calls for getting lists of nodes and loggers. 50 51 Also handles sending requests to change logger levels. 52 """ 53
54 - def __init__(self):
55 pass
56
57 - def get_levels(self):
58 # Declare level names lower-case, because that's how they are returned 59 # from the service call. 60 return ['debug', 'info', 'warn', 'error', 'fatal']
61
62 - def get_loggers(self, node):
63 self._refresh_loggers(node) 64 return self._current_loggers
65
66 - def get_node_names(self):
67 """ 68 Get a list of available services via a ros service call. 69 70 :returns: a list of all nodes that provide the set_logger_level service, ''list(str)'' 71 """ 72 set_logger_level_nodes = [] 73 nodes = rosnode.get_node_names() 74 for name in sorted(nodes): 75 for service in rosservice.get_service_list(name): 76 if service == name + '/set_logger_level': 77 set_logger_level_nodes.append(name) 78 return set_logger_level_nodes
79
80 - def _refresh_loggers(self, node):
81 """ 82 Store a list of loggers available for passed in node. 83 84 :param node: name of the node to query, ''str'' 85 :raises: :exc:`ROSTopicException` If topic type cannot be determined or loaded 86 """ 87 self._current_loggers = [] 88 self._current_levels = {} 89 # Construct the service name, taking into account our namespace 90 servicename = rosgraph.names.ns_join( 91 rosgraph.names.ns_join(rosgraph.names.get_ros_namespace(), node), 92 'get_loggers') 93 # Construct the service name, taking into account our namespace 94 servicename = rosgraph.names.resolve_name( 95 servicename, rosgraph.names.get_ros_namespace()) 96 try: 97 service = rosservice.get_service_class_by_name(servicename) 98 except rosservice.ROSServiceException as e: 99 raise ROSConsoleException( 100 "node '%s' doesn't exist or doesn't support query: %s" % (node, e)) 101 102 request = service._request_class() 103 proxy = rospy.ServiceProxy(str(servicename), service) 104 try: 105 response = proxy(request) 106 except rospy.ServiceException as e: 107 raise ROSConsoleException("node '%s' logger request failed: %s" % (node, e)) 108 109 if response._slot_types[0] == 'roscpp/Logger[]': 110 for logger in getattr(response, response.__slots__[0]): 111 self._current_loggers.append(logger.name) 112 self._current_levels[logger.name] = logger.level 113 else: 114 raise ROSConsoleException(repr(response))
115
116 - def send_logger_change_message(self, node, logger, level):
117 """ 118 Send a logger level change request to 'node'. 119 120 :param node: name of the node to chaange, ''str'' 121 :param logger: name of the logger to change, ''str'' 122 :param level: name of the level to change, ''str'' 123 :returns: True if the response is valid, ''bool'' 124 :returns: False if the request raises an exception or would not change the state, ''bool'' 125 """ 126 # Construct the service name, taking into account our namespace 127 servicename = rosgraph.names.ns_join( 128 rosgraph.names.ns_join(rosgraph.names.get_ros_namespace(), node), 129 'set_logger_level') 130 # Construct the service name, taking into account our namespace 131 servicename = rosgraph.names.resolve_name( 132 servicename, rosgraph.names.get_ros_namespace()) 133 if self._current_levels[logger] == level: 134 return False 135 136 service = rosservice.get_service_class_by_name(servicename) 137 request = service._request_class() 138 request.logger = logger 139 request.level = level 140 proxy = rospy.ServiceProxy(str(servicename), service) 141 try: 142 proxy(request) 143 self._current_levels[logger] = level.upper() 144 except rospy.ServiceException as e: 145 raise ROSConsoleException("node '%s' logger request failed: %s" % (node, e)) 146 147 return True
148