logger_level_service_caller.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 import rosnode
00034 import rospy
00035 import rosservice
00036 
00037 from python_qt_binding.QtCore import QObject, qWarning
00038 
00039 
00040 class LoggerLevelServiceCaller(QObject):
00041     """
00042     Handles service calls for getting lists of nodes and loggers
00043     Also handles sending requests to change logger levels
00044     """
00045     def __init__(self):
00046         super(LoggerLevelServiceCaller, self).__init__()
00047 
00048     def get_levels(self):
00049         return [self.tr('Debug'), self.tr('Info'), self.tr('Warn'), self.tr('Error'), self.tr('Fatal')]
00050 
00051     def get_loggers(self, node):
00052         if self._refresh_loggers(node):
00053             return self._current_loggers
00054         else:
00055             return []
00056 
00057     def get_node_names(self):
00058         """
00059         Gets a list of available services via a ros service call.
00060         :returns: a list of all nodes that provide the set_logger_level service, ''list(str)''
00061         """
00062         set_logger_level_nodes = []
00063         nodes = rosnode.get_node_names()
00064         for name in sorted(nodes):
00065             for service in rosservice.get_service_list(name):
00066                 if service == name + '/set_logger_level':
00067                     set_logger_level_nodes.append(name)
00068         return set_logger_level_nodes
00069 
00070     def _refresh_loggers(self, node):
00071         """
00072         Stores a list of loggers available for passed in node
00073         :param node: name of the node to query, ''str''
00074         """
00075         self._current_loggers = []
00076         self._current_levels = {}
00077         servicename = node + '/get_loggers'
00078         try:
00079             service = rosservice.get_service_class_by_name(servicename)
00080         except rosservice.ROSServiceIOException as e:
00081             qWarning('During get_service_class_by_name "%s":\n%s' % (servicename, e))
00082             return False
00083         request = service._request_class()
00084         proxy = rospy.ServiceProxy(str(servicename), service)
00085         try:
00086             response = proxy(request)
00087         except rospy.ServiceException as e:
00088             qWarning('SetupDialog.get_loggers(): request:\n%s' % (type(request)))
00089             qWarning('SetupDialog.get_loggers() "%s":\n%s' % (servicename, e))
00090             return False
00091 
00092         if response._slot_types[0] == 'roscpp/Logger[]':
00093             for logger in getattr(response, response.__slots__[0]):
00094                 self._current_loggers.append(getattr(logger, 'name'))
00095                 self._current_levels[getattr(logger, 'name')] = getattr(logger, 'level')
00096         else:
00097             qWarning(repr(response))
00098             return False
00099         return True
00100 
00101     def send_logger_change_message(self, node, logger, level):
00102         """
00103         Sends a logger level change request to 'node'.
00104         :param node: name of the node to chaange, ''str''
00105         :param logger: name of the logger to change, ''str''
00106         :param level: name of the level to change, ''str''
00107         :returns: True if the response is valid, ''bool''
00108         :returns: False if the request raises an exception or would not change the cached state, ''bool''
00109         """
00110         servicename = node + '/set_logger_level'
00111         if self._current_levels[logger].lower() == level.lower():
00112             return False
00113 
00114         service = rosservice.get_service_class_by_name(servicename)
00115         request = service._request_class()
00116         setattr(request, 'logger', logger)
00117         setattr(request, 'level', level)
00118         proxy = rospy.ServiceProxy(str(servicename), service)
00119         try:
00120             proxy(request)
00121             self._current_levels[logger] = level.upper()
00122         except rospy.ServiceException as e:
00123             qWarning('SetupDialog.level_changed(): request:\n%r' % (request))
00124             qWarning('SetupDialog.level_changed() "%s":\n%s' % (servicename, e))
00125             return False
00126         return True


rqt_logger_level
Author(s): Aaron Blasdel
autogenerated on Fri Jan 3 2014 11:56:39