naoqi_logger.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (C) 2014 Aldebaran Robotics
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 
17 import logging
18 
19 from rosgraph_msgs.msg import Log
20 import rospy
21 
22 from naoqi_driver.naoqi_node import NaoqiNode
23 
24 # level 0 is actually 'silent' in NAOqi and 5 is 'verbose' but we adapt to the ROS ones
25 LEVELS = [Log.DEBUG, Log.FATAL, Log.ERROR, Log.WARN, Log.INFO, Log.DEBUG, Log.DEBUG]
26 ROSOUT_PUB = rospy.topics.Publisher('/rosout', Log, latch=True, queue_size=100)
27 
29  """
30  Given a NAOqi dict message, publish it in /rosout
31  :param msg: dict corresponding to a NAOqi log message
32  """
33  file, function, line = msg['source'].split(':')
34  if line:
35  line = int(line)
36  # adapt the category to what a topic is (kindof).
37  l = Log(level=LEVELS[msg['level']], name=str(msg['category']), msg=msg['message'], file=file, line=line, function=function)
38  l.header.stamp = rospy.Time(msg['timestamp']['tv_sec'], msg['timestamp']['tv_usec'])
39  ROSOUT_PUB.publish(l)
40 
42  #This should be treated as a constant
43  NODE_NAME = 'nao_logger'
44 
45  def __init__( self ):
46  #Initialization
47  NaoqiNode.__init__( self, self.NODE_NAME )
48 
49  from distutils.version import LooseVersion
50  if self.get_version() < LooseVersion('2.0.0'):
51  rospy.loginfo('The NAOqi version is inferior to 2.0, hence no log bridge possible')
52  exit(0)
53 
54  rospy.init_node( self.NODE_NAME )
55 
56  # the log manager is only avaiable through a session (NAOqi 2.0)
57  import qi
58  self.session = qi.Session()
59  self.session.connect("tcp://%s:%s" % (self.pip, self.pport))
60  self.logManager = self.session.service("LogManager")
61 
62  self.listener = self.logManager.getListener()
63  self.listener.onLogMessage.connect(onMessageCallback)
64  rospy.loginfo('Logger initialized')
65 
66 if __name__ == '__main__':
67  try:
68  nao_logger = NaoqiLogger()
69  except RuntimeError as e:
70  rospy.logerr('Something went wrong: %s' % str(e) )
71 
72  rospy.spin()
def onMessageCallback(msg)
Definition: naoqi_logger.py:28


naoqi_driver_py
Author(s): Armin Hornung, Armin Hornung, Stefan Osswald, Daniel Maier, Miguel Sarabia, Severin Lemaignan
autogenerated on Thu Jul 16 2020 03:18:30