Package rospy :: Package impl :: Module rosout

Source Code for Module rospy.impl.rosout

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2008, 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  # Revision $Id: rosout.py 12050 2010-11-09 08:13:53Z kwc $ 
 34   
 35  """Internal use: support for /rosout logging in rospy""" 
 36   
 37  import logging 
 38  import sys 
 39  import traceback 
 40   
 41  import rospy.names  
 42   
 43  from rospy.core import add_log_handler, get_caller_id 
 44  from rospy.exceptions import ROSException 
 45  from rospy.topics import Publisher, Subscriber 
 46  from rospy.rostime import Time 
 47   
 48  from rospy.impl.registration import get_topic_manager 
 49   
 50  #Log message for rosout 
 51  from rosgraph_msgs.msg import Log 
 52   
 53  _ROSOUT = '/rosout' 
 54  _rosout_pub = None 
 55   
56 -def init_rosout():
57 logger = logging.getLogger("rospy.rosout") 58 try: 59 global _rosout_pub 60 if _rosout_pub is None: 61 logger.info("initializing %s core topic"%_ROSOUT) 62 _rosout_pub = Publisher(_ROSOUT, Log, latch=True) 63 logger.info("connected to core topic %s"%_ROSOUT) 64 return True 65 except Exception, e: 66 logger.error("Unable to initialize %s: %s\n%s", _ROSOUT, e, traceback.format_exc()) 67 return False
68 69 _in_rosout = False 70 ## log an error to the /rosout topic
71 -def _rosout(level, msg):
72 global _in_rosout 73 try: 74 if _rosout_pub is not None: 75 # protect against infinite recursion 76 if not _in_rosout: 77 try: 78 _in_rosout = True 79 msg = str(msg) 80 topics = get_topic_manager().get_topics() 81 l = Log(level=level, name=str(rospy.names.get_caller_id()), msg=str(msg), topics=topics) 82 l.header.stamp = Time.now() 83 _rosout_pub.publish(l) 84 finally: 85 _in_rosout = False 86 except Exception, e: 87 #traceback.print_exc() 88 # don't use logerr in this case as that is recursive here 89 logger = logging.getLogger("rospy.rosout") 90 logger.error("Unable to report rosout: %s\n%s", e, traceback.format_exc()) 91 return False
92
93 -def _rosout_debug(msg):
94 _rosout(Log.DEBUG, msg)
95 -def _rosout_info(msg):
96 _rosout(Log.INFO, msg)
97 -def _rosout_warn(msg):
98 _rosout(Log.WARN, msg)
99 -def _rosout_error(msg):
100 _rosout(Log.ERROR, msg)
101 -def _rosout_fatal(msg):
102 _rosout(Log.FATAL, msg)
103 104 ## Load loggers for publishing to /rosout 105 ## @param level int: Log level. Loggers >= level will be loaded.
106 -def load_rosout_handlers(level):
107 if Log.DEBUG >= level: 108 add_log_handler(Log.DEBUG, _rosout_debug) 109 if Log.INFO >= level: 110 add_log_handler(Log.INFO, _rosout_info) 111 if Log.WARN >= level: 112 add_log_handler(Log.WARN, _rosout_warn) 113 if Log.ERROR >= level: 114 add_log_handler(Log.ERROR, _rosout_error) 115 if Log.FATAL >= level: 116 add_log_handler(Log.FATAL, _rosout_fatal)
117