Package roslib :: Module roslogging
[frames] | no frames]

Source Code for Module roslib.roslogging

  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: roslogging.py 14773 2011-08-22 17:49:27Z kwc $ 
 34  # $Author: kwc $ 
 35   
 36  """ 
 37  Library for configuring python logging to standard ROS locations (e.g. ROS_LOG_DIR). 
 38  """ 
 39   
 40  import os 
 41  import sys 
 42  import logging 
 43  import logging.config 
 44   
 45  import roslib.rosenv 
 46  from roslib.rosenv import get_ros_root, ROS_LOG_DIR, ROS_HOME, makedirs_with_parent_perms 
 47  import roslib.exceptions 
 48   
 49  get_log_dir = roslib.rosenv.get_log_dir 
 50       
51 -def configure_logging(logname, level=logging.INFO, filename=None, env=None):
52 """ 53 Configure Python logging package to send log files to ROS-specific log directory 54 @param logname str: name of logger 55 @type logname: str 56 @param filename: filename to log to. If not set, a log filename 57 will be generated using logname 58 @type filename: str 59 @param env: override os.environ dictionary 60 @type env: dict 61 @return: log file name 62 @rtype: str 63 @raise roslib.exceptions.ROSLibException: if logging cannot be configured as specified 64 """ 65 if env is None: 66 env = os.environ 67 68 logname = logname or 'unknown' 69 log_dir = get_log_dir(env=env) 70 71 # if filename is not explicitly provided, generate one using logname 72 if not filename: 73 log_filename = os.path.join(log_dir, '%s-%s.log'%(logname, os.getpid())) 74 else: 75 log_filename = os.path.join(log_dir, filename) 76 77 logfile_dir = os.path.dirname(log_filename) 78 if not os.path.exists(logfile_dir): 79 try: 80 makedirs_with_parent_perms(logfile_dir) 81 except OSError: 82 # cannot print to screen because command-line tools with output use this 83 sys.stderr.write("WARNING: cannot create log directory [%s]. Please set %s to a writable location.\n"%(logfile_dir, ROS_LOG_DIR)) 84 return None 85 elif os.path.isfile(logfile_dir): 86 raise roslib.exceptions.ROSLibException("Cannot save log files: file [%s] is in the way"%logfile_dir) 87 88 if 'ROS_PYTHON_LOG_CONFIG_FILE' in os.environ: 89 config_file = os.environ['ROS_PYTHON_LOG_CONFIG_FILE'] 90 else: 91 config_file = os.path.join(get_ros_root(env=env), 'config', 'python_logging.conf') 92 93 if not os.path.isfile(config_file): 94 # logging is considered soft-fail 95 sys.stderr.write("WARNING: cannot load logging configuration file, logging is disabled\n") 96 return log_filename 97 98 # pass in log_filename as argument to pylogging.conf 99 os.environ['ROS_LOG_FILENAME'] = log_filename 100 # #3625: disabling_existing_loggers=False 101 logging.config.fileConfig(config_file, disable_existing_loggers=False) 102 return log_filename
103