logger_level_service_caller.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, 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 rosnode
34 import rospy
35 import rosservice
36 
37 from python_qt_binding.QtCore import QObject, qWarning
38 
39 
40 class LoggerLevelServiceCaller(QObject):
41 
42  """
43  Handles service calls for getting lists of nodes and loggers
44  Also handles sending requests to change logger levels
45  """
46 
47  def __init__(self):
48  super(LoggerLevelServiceCaller, self).__init__()
49 
50  def get_levels(self):
51  return [self.tr('Debug'), self.tr('Info'), self.tr('Warn'), self.tr('Error'), self.tr('Fatal')]
52 
53  def get_loggers(self, node):
54  if self._refresh_loggers(node):
55  return self._current_loggers
56  else:
57  return []
58 
59  def get_node_names(self):
60  """
61  Gets a list of available services via a ros service call.
62  :returns: a list of all nodes that provide the set_logger_level service, ''list(str)''
63  """
64  set_logger_level_nodes = []
65  nodes = rosnode.get_node_names()
66  for name in sorted(nodes):
67  for service in rosservice.get_service_list(name):
68  if service == name + '/set_logger_level':
69  set_logger_level_nodes.append(name)
70  return set_logger_level_nodes
71 
72  def _refresh_loggers(self, node):
73  """
74  Stores a list of loggers available for passed in node
75  :param node: name of the node to query, ''str''
76  """
77  self._current_loggers = []
78  self._current_levels = {}
79  servicename = node + '/get_loggers'
80  try:
81  service = rosservice.get_service_class_by_name(servicename)
82  except rosservice.ROSServiceIOException as e:
83  qWarning('During get_service_class_by_name "%s":\n%s' % (servicename, e))
84  return False
85  request = service._request_class()
86  proxy = rospy.ServiceProxy(str(servicename), service)
87  try:
88  response = proxy(request)
89  except rospy.ServiceException as e:
90  qWarning('SetupDialog.get_loggers(): request:\n%s' % (type(request)))
91  qWarning('SetupDialog.get_loggers() "%s":\n%s' % (servicename, e))
92  return False
93 
94  if response._slot_types[0] == 'roscpp/Logger[]':
95  for logger in getattr(response, response.__slots__[0]):
96  self._current_loggers.append(getattr(logger, 'name'))
97  self._current_levels[getattr(logger, 'name')] = getattr(logger, 'level')
98  else:
99  qWarning(repr(response))
100  return False
101  return True
102 
103  def send_logger_change_message(self, node, logger, level):
104  """
105  Sends a logger level change request to 'node'.
106  :param node: name of the node to chaange, ''str''
107  :param logger: name of the logger to change, ''str''
108  :param level: name of the level to change, ''str''
109  :returns: True if the response is valid, ''bool''
110  :returns: False if the request raises an exception or would not change the cached state, ''bool''
111  """
112  servicename = node + '/set_logger_level'
113  if self._current_levels[logger].lower() == level.lower():
114  return False
115 
116  service = rosservice.get_service_class_by_name(servicename)
117  request = service._request_class()
118  setattr(request, 'logger', logger)
119  setattr(request, 'level', level)
120  proxy = rospy.ServiceProxy(str(servicename), service)
121  try:
122  proxy(request)
123  self._current_levels[logger] = level.upper()
124  except rospy.ServiceException as e:
125  qWarning('SetupDialog.level_changed(): request:\n%r' % (request))
126  qWarning('SetupDialog.level_changed() "%s":\n%s' % (servicename, e))
127  return False
128  return True


rqt_logger_level
Author(s): Aaron Blasdel
autogenerated on Sun May 24 2020 03:49:54