naoqi_logger.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Copyright (C) 2014 Aldebaran Robotics
00003 #
00004 # Licensed under the Apache License, Version 2.0 (the "License");
00005 # you may not use this file except in compliance with the License.
00006 # You may obtain a copy of the License at
00007 #
00008 #     http://www.apache.org/licenses/LICENSE-2.0
00009 #
00010 # Unless required by applicable law or agreed to in writing, software
00011 # distributed under the License is distributed on an "AS IS" BASIS,
00012 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 # See the License for the specific language governing permissions and
00014 # limitations under the License.
00015 #
00016 
00017 import logging
00018 
00019 from rosgraph_msgs.msg import Log
00020 import rospy
00021 
00022 from naoqi_driver.naoqi_node import NaoqiNode
00023 
00024 # level 0 is actually 'silent' in NAOqi and 5 is 'verbose' but we adapt to the ROS ones
00025 LEVELS = [Log.DEBUG, Log.FATAL, Log.ERROR, Log.WARN, Log.INFO, Log.DEBUG, Log.DEBUG]
00026 ROSOUT_PUB = rospy.topics.Publisher('/rosout', Log, latch=True, queue_size=100)
00027 
00028 def onMessageCallback(msg):
00029     """
00030     Given a NAOqi dict message, publish it in /rosout
00031     :param msg: dict corresponding to a NAOqi log message
00032     """
00033     file, function, line = msg['source'].split(':')
00034     if line:
00035         line = int(line)
00036     # adapt the category to what a topic is (kindof).
00037     l = Log(level=LEVELS[msg['level']], name=str(msg['category']), msg=msg['message'], file=file, line=line, function=function)
00038     l.header.stamp = rospy.Time(msg['timestamp']['tv_sec'], msg['timestamp']['tv_usec'])
00039     ROSOUT_PUB.publish(l)
00040 
00041 class NaoqiLogger(NaoqiNode):
00042     #This should be treated as a constant
00043     NODE_NAME = 'nao_logger'
00044 
00045     def __init__( self ):
00046         #Initialization
00047         NaoqiNode.__init__( self, self.NODE_NAME )
00048 
00049         from distutils.version import LooseVersion
00050         if self.get_version() < LooseVersion('2.0.0'):
00051             rospy.loginfo('The NAOqi version is inferior to 2.0, hence no log bridge possible')
00052             exit(0)
00053 
00054         rospy.init_node( self.NODE_NAME )
00055 
00056         # the log manager is only avaiable through a session (NAOqi 2.0)
00057         import qi
00058         self.session = qi.Session()
00059         self.session.connect("tcp://%s:%s" % (self.pip, self.pport))
00060         self.logManager = self.session.service("LogManager")
00061 
00062         self.listener = self.logManager.getListener()
00063         self.listener.onLogMessage.connect(onMessageCallback)
00064         rospy.loginfo('Logger initialized')
00065 
00066 if __name__ == '__main__':
00067     try:
00068         nao_logger = NaoqiLogger()
00069     except RuntimeError as e:
00070         rospy.logerr('Something went wrong: %s' % str(e) )
00071 
00072     rospy.spin()


naoqi_driver_py
Author(s): Armin Hornung, Armin Hornung, Stefan Osswald, Daniel Maier, Miguel Sarabia, Severin Lemaignan
autogenerated on Wed Aug 16 2017 02:28:07