def configure_logging(logname, level=logging.INFO, filename=None, env=None): """ Configure Python logging package to send log files to ROS-specific log directory :param logname str: name of logger, ``str`` :param filename: filename to log to. If not set, a log filename will be generated using logname, ``str`` :param env: override os.environ dictionary, ``dict`` :returns: log file name, ``str`` :raises: :exc:`LoggingException` If logging cannot be configured as specified """ if env is None: env = os.environ logname = logname or 'unknown' log_dir = rospkg.get_log_dir(env=env) # if filename is not explicitly provided, generate one using logname if not filename: log_filename = os.path.join(log_dir, '%s-%s.log'%(logname, os.getpid())) else: log_filename = os.path.join(log_dir, filename) logfile_dir = os.path.dirname(log_filename) if not os.path.exists(logfile_dir): try: makedirs_with_parent_perms(logfile_dir) except OSError: # cannot print to screen because command-line tools with output use this sys.stderr.write("WARNING: cannot create log directory [%s]. Please set %s to a writable location.\n"%(logfile_dir, ROS_LOG_DIR)) return None elif os.path.isfile(logfile_dir): raise LoggingException("Cannot save log files: file [%s] is in the way"%logfile_dir) if 'ROS_PYTHON_LOG_CONFIG_FILE' in os.environ: config_file = os.environ['ROS_PYTHON_LOG_CONFIG_FILE'] else: # search for logging config file in /etc/. If it's not there, # look for it package-relative. fname = 'python_logging.conf' rosgraph_d = rospkg.RosPack().get_path('rosgraph') for f in [os.path.join(rospkg.get_ros_home(), 'config', fname), '/etc/ros/%s'%(fname), os.path.join(rosgraph_d, 'conf', fname)]: if os.path.isfile(f): config_file = f break else: config_file = None if config_file is None or not os.path.isfile(config_file): # logging is considered soft-fail sys.stderr.write("WARNING: cannot load logging configuration file, logging is disabled\n") logging.getLogger(logname).setLevel(logging.CRITICAL) return log_filename # pass in log_filename as argument to pylogging.conf os.environ['ROS_LOG_FILENAME'] = log_filename # #3625: disabling_existing_loggers=False logging.config.fileConfig(config_file, disable_existing_loggers=False) return log_filename
def configure_logging(logname, level=logging.INFO, filename=None, env=None): """ Configure Python logging package to send log files to ROS-specific log directory :param logname str: name of logger, ``str`` :param filename: filename to log to. If not set, a log filename will be generated using logname, ``str`` :param env: override os.environ dictionary, ``dict`` :returns: log file name, ``str`` :raises: :exc:`LoggingException` If logging cannot be configured as specified """ if env is None: env = os.environ logname = logname or 'unknown' log_dir = rospkg.get_log_dir(env=env) # if filename is not explicitly provided, generate one using logname if not filename: log_filename = os.path.join(log_dir, '%s-%s.log'%(logname, os.getpid())) else: log_filename = os.path.join(log_dir, filename) logfile_dir = os.path.dirname(log_filename) if not os.path.exists(logfile_dir): try: makedirs_with_parent_perms(logfile_dir) except OSError: # cannot print to screen because command-line tools with output use this sys.stderr.write("WARNING: cannot create log directory [%s]. Please set %s to a writable location.\n"%(logfile_dir, ROS_LOG_DIR)) return None elif os.path.isfile(logfile_dir): raise LoggingException("Cannot save log files: file [%s] is in the way"%logfile_dir) if 'ROS_PYTHON_LOG_CONFIG_FILE' in os.environ: config_file = os.environ['ROS_PYTHON_LOG_CONFIG_FILE'] else: # search for logging config file in /etc/. If it's not there, # look for it package-relative. fname = 'python_logging.conf' rosgraph_d = rospkg.RosPack().get_path('rosgraph') for f in [os.path.join(rospkg.get_ros_home(), 'config', fname), '/etc/ros/%s'%(fname), os.path.join(rosgraph_d, 'conf', fname)]: if os.path.isfile(f): config_file = f break else: config_file = None if config_file is None or not os.path.isfile(config_file): # logging is considered soft-fail sys.stderr.write("WARNING: cannot load logging configuration file, logging is disabled\n") logging.getLogger(logname).setLevel(logging.CRITICAL) return log_filename # pass in log_filename as argument to pylogging.conf os.environ['ROS_LOG_FILENAME'] = log_filename # #3625: disabling_existing_loggers=False logging.config.fileConfig(config_file, disable_existing_loggers=False) return log_filename
def log_dir_size(): ''' :return: Disk usage in bytes for ROS log directory. :rtype: int ''' d = rospkg.get_log_dir() disk_usage = get_disk_usage(d) return disk_usage
def log_init(self, log_path): log_dir = rospkg.get_log_dir() + "/latest" log_dir = os.path.realpath(log_dir) log_file = log_dir + "/" + log_path log_handle = open(log_file, 'w') headers = ','.join( ["time", "prob_red_light", "prob_EMA_red_light", "red_light_id"]) log_handle.write(headers + '\n') return log_handle
def test_get_log_dir(): from rospkg import get_log_dir, get_ros_root base = tempfile.gettempdir() ros_log_dir = os.path.join(base, 'ros_log_dir') ros_home_dir = os.path.join(base, 'ros_home_dir') home_dir = os.path.expanduser('~') # ROS_LOG_DIR has precedence env = {'ROS_ROOT': get_ros_root(), 'ROS_LOG_DIR': ros_log_dir, 'ROS_HOME': ros_home_dir} assert ros_log_dir == get_log_dir(env=env) env = {'ROS_ROOT': get_ros_root(), 'ROS_HOME': ros_home_dir} assert os.path.join(ros_home_dir, 'log') == get_log_dir(env=env) env = {'ROS_ROOT': get_ros_root()} assert os.path.join(home_dir, '.ros', 'log') == get_log_dir(env=env) # test default assignment of env. Don't both checking return value as we would duplicate get_log_dir assert get_log_dir() is not None
def test_get_log_dir(): from rospkg import get_log_dir, get_ros_root base = tempfile.gettempdir() ros_log_dir = os.path.join(base, 'ros_log_dir') ros_home_dir = os.path.join(base, 'ros_home_dir') home_dir = os.path.expanduser('~') # ROS_LOG_DIR has precedence env = {'ROS_ROOT': get_ros_root(), 'ROS_LOG_DIR': ros_log_dir, 'ROS_HOME': ros_home_dir } assert ros_log_dir == get_log_dir(env=env) env = {'ROS_ROOT': get_ros_root(), 'ROS_HOME': ros_home_dir } assert os.path.join(ros_home_dir, 'log') == get_log_dir(env=env) env = {'ROS_ROOT': get_ros_root()} assert os.path.join(home_dir, '.ros', 'log') == get_log_dir(env=env) # test default assignment of env. Don't both checking return value as we would duplicate get_log_dir assert get_log_dir() is not None
def _configure_logging(self): """ Configure logging of node's log file and stdout/stderr @return: stdout log file name, stderr log file name. Values are None if stdout/stderr are not logged. @rtype: str, str """ log_dir = rospkg.get_log_dir(env=os.environ) if self.run_id: log_dir = os.path.join(log_dir, self.run_id) if not os.path.exists(log_dir): try: os.makedirs(log_dir) except OSError as e: if e.errno == 13: raise RLException( "unable to create directory for log file [%s].\nPlease check permissions." % log_dir) else: raise RLException( "unable to create directory for log file [%s]: %s" % (log_dir, e.strerror)) # #973: save log dir for error messages self.log_dir = log_dir # send stdout/stderr to file. in the case of respawning, we have to # open in append mode # note: logfileerr: disabling in favor of stderr appearing in the console. # will likely reinstate once roserr/rosout is more properly used. logfileout = logfileerr = None logfname = self._log_name() if self.log_output: outf, errf = [ os.path.join(log_dir, '%s-%s.log' % (logfname, n)) for n in ['stdout', 'stderr'] ] if self.respawn: mode = 'a' else: mode = 'w' logfileout = open(outf, mode) if is_child_mode(): logfileerr = open(errf, mode) # #986: pass in logfile name to node node_log_file = log_dir if self.is_node: # #1595: on respawn, these keep appending self.args = _cleanup_remappings(self.args, '__log:=') self.args.append("__log:=%s" % os.path.join(log_dir, "%s.log" % (logfname))) return logfileout, logfileerr
def log_init(self, log_path): log_dir = rospkg.get_log_dir() + "/latest" log_dir = os.path.realpath(log_dir) log_file = log_dir + "/" + log_path log_handle = open(log_file, 'w') headers = ','.join([ "time", "car_x", "car_y", "closest_idx_waypoint", "waypoint_x", "waypoint_y" ]) log_handle.write(headers + '\n') return log_handle
def __init__(self, regman, param_server): global CHKPT_PATH print("ROSRescue ENABLED") env = os.environ log_dir = rospkg.get_log_dir(env=env) CHKPT_PATH = os.path.join(log_dir, "latest-chkpt.yaml") self.regman = regman self.param_server = param_server self.parameters = param_server.parameters self.recovering = False return
def __init__(self, *args, **kwargs): rospy.init_node(*args, **kwargs) self._topics = rospy.get_param('~topics', []) self._topics = [ rospy.resolve_name(topic_name) for topic_name in self._topics ] self._check_period = rospy.get_param('~check_period', 0.1) self._log_directory = rospy.get_param('~log_directory', rospkg.get_log_dir()) self._lock = threading.Lock() self._mission_recordings = {}
def configure_logging(logname, level=logging.INFO, filename=None, env=None): """ Configure Python logging package to send log files to ROS-specific log directory @param logname str: name of logger @type logname: str @param filename: filename to log to. If not set, a log filename will be generated using logname @type filename: str @param env: override os.environ dictionary @type env: dict @return: log file name @rtype: str @raise roslib.exceptions.ROSLibException: if logging cannot be configured as specified """ if env is None: env = os.environ logname = logname or 'unknown' log_dir = get_log_dir(env=env) # if filename is not explicitly provided, generate one using logname if not filename: log_filename = os.path.join(log_dir, '%s-%s.log'%(logname, os.getpid())) else: log_filename = os.path.join(log_dir, filename) logfile_dir = os.path.dirname(log_filename) if not os.path.exists(logfile_dir): try: makedirs_with_parent_perms(logfile_dir) except OSError: # cannot print to screen because command-line tools with output use this sys.stderr.write("WARNING: cannot create log directory [%s]. Please set %s to a writable location.\n"%(logfile_dir, ROS_LOG_DIR)) return None elif os.path.isfile(logfile_dir): raise roslib.exceptions.ROSLibException("Cannot save log files: file [%s] is in the way"%logfile_dir) if 'ROS_PYTHON_LOG_CONFIG_FILE' in os.environ: config_file = os.environ['ROS_PYTHON_LOG_CONFIG_FILE'] else: config_file = os.path.join(get_ros_root(env=env), 'config', 'python_logging.conf') if not os.path.isfile(config_file): # logging is considered soft-fail sys.stderr.write("WARNING: cannot load logging configuration file, logging is disabled\n") return log_filename # pass in log_filename as argument to pylogging.conf os.environ['ROS_LOG_FILENAME'] = log_filename # #3625: disabling_existing_loggers=False logging.config.fileConfig(config_file, disable_existing_loggers=False) return log_filename
def check_log_disk_usage(): """ Check size of log directory. If high, print warning to user """ try: d = rospkg.get_log_dir() roslaunch.core.printlog("Checking log directory for disk usage. This may take awhile.\nPress Ctrl-C to interrupt") disk_usage = rosclean.get_disk_usage(d) # warn if over a gig if disk_usage > 1073741824: roslaunch.core.printerrlog("WARNING: disk usage in log directory [%s] is over 1GB.\nIt's recommended that you use the 'rosclean' command."%d) else: roslaunch.core.printlog("Done checking log file disk usage. Usage is <1GB.") except: pass
def check_log_disk_usage(): """ Check size of log directory. If high, print warning to user """ try: d = rospkg.get_log_dir() roslaunch.core.printlog("Checking log directory for disk usage. This may take awhile.\nPress Ctrl-C to interrupt") disk_usage = rosclean.get_disk_usage(d) # warn if over a gig if disk_usage > 1073741824: roslaunch.core.printerrlog("WARNING: disk usage in log directory [%s] is over 1GB.\nIt's recommended that you use the 'rosclean' command."%d) else: roslaunch.core.printlog("Done checking log file disk usage. Usage is <1GB.") except: pass
def rosclean(): ''' Removes the content of the ROS-log directory. We didn't use rosclean purge because it removes the log-directory. This needs restart of ROS nodes or recreate log directory to get log again. ''' d = rospkg.get_log_dir() if d and d != os.path.sep: ps = SupervisedPopen(['rm -fr %s/*' % d], stdout=subprocess.PIPE, shell=True, object_id='rosclean') output_err = ps.stderr.read() if output_err: raise Exception(output_err)
def log_init(self, log_path): log_dir = rospkg.get_log_dir() + "/latest" log_dir = os.path.realpath(log_dir) log_file = log_dir + "/" + log_path log_handle = open(log_file, 'w') headers = ','.join([ "throttle_p_effort", "throttle_i_effort", "throttle_d_effort", "brake_p_effort", "brake_i_effort", "brake_d_effort", "velocity_error", "DT", "latchBrake", "dbw_time", "target_linear_velocity", "target_angular_velocity", "current_linear_velocity", "current_angular_velocity", "dbw_status", "throttle", "brake", "steering" ]) log_handle.write(headers + '\n') return log_handle
def _configure_logging(self): """ Configure logging of node's log file and stdout/stderr @return: stdout log file name, stderr log file name. Values are None if stdout/stderr are not logged. @rtype: str, str """ log_dir = rospkg.get_log_dir(env=os.environ) if self.run_id: log_dir = os.path.join(log_dir, self.run_id) if not os.path.exists(log_dir): try: os.makedirs(log_dir) except OSError as e: if e.errno == 13: raise RLException("unable to create directory for log file [%s].\nPlease check permissions."%log_dir) else: raise RLException("unable to create directory for log file [%s]: %s"%(log_dir, e.strerror)) # #973: save log dir for error messages self.log_dir = log_dir # send stdout/stderr to file. in the case of respawning, we have to # open in append mode # note: logfileerr: disabling in favor of stderr appearing in the console. # will likely reinstate once roserr/rosout is more properly used. logfileout = logfileerr = None logfname = self._log_name() if self.log_output: outf, errf = [os.path.join(log_dir, '%s-%s.log'%(logfname, n)) for n in ['stdout', 'stderr']] if self.respawn: mode = 'a' else: mode = 'w' logfileout = open(outf, mode) if is_child_mode(): logfileerr = open(errf, mode) # #986: pass in logfile name to node node_log_file = log_dir if self.is_node: # #1595: on respawn, these keep appending self.args = _cleanup_remappings(self.args, '__log:=') self.args.append("__log:=%s"%os.path.join(log_dir, "%s.log"%(logfname))) return logfileout, logfileerr
def __init__(self, config_file="", is_master=True): self.noverify = False self.master = set() self.aliases = dict() self.subscribers = dict() self.publishers = dict() self.providers = dict() self.requesters = dict() self.methods = dict() self.nodes = dict() self.peer_publishers = dict() self.setters = dict() self.getters = dict() self.ip_addresses = set() self.logger = getLogger() """ Set ROS master IP address """ if is_master: if os.environ.has_key("ROS_MASTER_URI"): self.master = set( uri_to_ip_address_list(os.environ.get("ROS_MASTER_URI"))) else: raise RuntimeError( "ROS_MASTER_URI environment variable not set") self.master.update(resolve_ip_address("localhost")) self.logger.info("Setting master from ROS_MASTER_URI: %s" % self.master) """ ROS authorization configuration file is stored in environment variable """ if config_file == "": if os.environ.has_key("ROS_AUTH_FILE"): config_file = os.environ.get("ROS_AUTH_FILE") if config_file == "": self.noverify = True self.logger.warn( "Authorization configuration file not specified. Secure ROS is disabled!" ) elif not os.path.exists(config_file): raise RuntimeError( "Authorization configuration file %s does not exist" % config_file) else: self.logger.info("Loading authorization from %s" % config_file) self.load(config_file) output_file = "%s/ros_auth_full.yaml" % rospkg.get_log_dir( env=os.environ) self.logger.info("Writing full authorization to %s" % output_file) self.save(output_file)
def _configure_logging(self): """ Configure logging of node's log file and stdout/stderr @return: stdout log file name, stderr log file name. Values are None if stdout/stderr are not logged. @rtype: str, str """ log_dir = rospkg.get_log_dir(env=os.environ) if self.run_id: log_dir = os.path.join(log_dir, self.run_id) if not os.path.exists(log_dir): try: os.makedirs(log_dir) except OSError, (errno, msg): if errno == 13: raise RLException("unable to create directory for log file [%s].\nPlease check permissions."%log_dir) else: raise RLException("unable to create directory for log file [%s]: %s"%(log_dir, msg))
def _configure_logging(self): """ Configure logging of node's log file and stdout/stderr @return: stdout log file name, stderr log file name. Values are None if stdout/stderr are not logged. @rtype: str, str """ log_dir = rospkg.get_log_dir(env=os.environ) if self.run_id: log_dir = os.path.join(log_dir, self.run_id) if not os.path.exists(log_dir): try: os.makedirs(log_dir) except OSError, (errno, msg): if errno == 13: raise RLException("unable to create directory for log file [%s].\nPlease check permissions."%log_dir) else: raise RLException("unable to create directory for log file [%s]: %s"%(log_dir, msg))
def logs_main(): from optparse import OptionParser parser = OptionParser(usage="usage: %prog", prog=NAME) options, args = parser.parse_args() if args: parser.error("%s takes no arguments"%NAME) log_dir = rospkg.get_log_dir() if not log_dir: print >> sys.stderr, "Cannot determine ROS log directory" sys.exit(1) run_id = get_run_id() if not run_id: # go ahead and print the log directory print("No active roscore", file=sys.stderr) print(log_dir) sys.exit(2) print(os.path.join(log_dir, run_id))
def logs_main(): from optparse import OptionParser parser = OptionParser(usage="usage: %prog", prog=NAME) options, args = parser.parse_args() if args: parser.error("%s takes no arguments" % NAME) log_dir = rospkg.get_log_dir() if not log_dir: print("Cannot determine ROS log directory", file=sys.stderr) sys.exit(1) run_id = get_run_id() if not run_id: # go ahead and print the log directory print("No active roscore", file=sys.stderr) print(log_dir) sys.exit(2) print(os.path.join(log_dir, run_id))
def output_xml(self): """ Simply outputs the topic and node metadata to XML. Runs atexit. ROSpalantir files are saved as ~/.ros/log/rospalantir-*. """ #Opt-In Level: level = os.environ['ROS_PALANTIR'] if level == "None": return user_name_string = os.environ['ROS_PALANTIR_USERNAME'] password_string = os.environ['ROS_PALANTIR_PASSWORD'] self.preprocess_xml() doc = Document() root = doc.createElement('root') doc.appendChild(root) #User Info user = doc.createElement('user') root.appendChild(user) #if level == "1": # mac = doc.createElement('mac') # mac_content = doc.createTextNode(str(get_mac())) # mac.appendChild(mac_content) # user.appendChild(mac) user_name = doc.createElement('user_name') user_name_content = doc.createTextNode(user_name_string) user_name.appendChild(user_name_content) user.appendChild(user_name) password = doc.createElement('password') password_content = doc.createTextNode(password_string) password.appendChild(password_content) user.appendChild(password) #if level == "1" or level == "2": #Get IP #Msgs msgs = doc.createElement('msgs') root.appendChild(msgs) for msg in self.registeredList: rospy.logdebug(msg.process_name) indvidual_msg = doc.createElement('msg') name = doc.createElement('name') name_content = doc.createTextNode(msg.name) name.appendChild(name_content) indvidual_msg.appendChild(name) msg_type = doc.createElement('msg_type') msg_type_content = doc.createTextNode(msg.msg_type) msg_type.appendChild(msg_type_content) indvidual_msg.appendChild(msg_type) topic_type = doc.createElement('topic_type') topic_type_content = doc.createTextNode(msg.topic_type) topic_type.appendChild(topic_type_content) indvidual_msg.appendChild(topic_type) node_name = doc.createElement('node_name') node_name_content = doc.createTextNode(self.processNameToNode[msg.process_name.replace('/','',1)]) node_name.appendChild(node_name_content) indvidual_msg.appendChild(node_name) duration = doc.createElement('duration') duration_content = doc.createTextNode(str(msg.duration)) duration.appendChild(duration_content) indvidual_msg.appendChild(duration) msgs.appendChild(indvidual_msg) #Launches launches = doc.createElement('launches') root.appendChild(launches) for msg in self.launchList: indvidual_msg = doc.createElement('launch') package = doc.createElement('package') package_content = doc.createTextNode(msg.package) package.appendChild(package_content) indvidual_msg.appendChild(package) node_name = doc.createElement('node_name') node_name_content = doc.createTextNode(msg.node_name) node_name.appendChild(node_name_content) indvidual_msg.appendChild(node_name) process_name = doc.createElement('process_name') process_name_content = doc.createTextNode(msg.process_name) process_name.appendChild(process_name_content) indvidual_msg.appendChild(process_name) desktop_session = doc.createElement('desktop_session') desktop_session_content = doc.createTextNode(msg.desktop_session) desktop_session.appendChild(desktop_session_content) indvidual_msg.appendChild(desktop_session) master_uri = doc.createElement('master_uri') master_uri_content = doc.createTextNode(msg.master_uri) master_uri.appendChild(master_uri_content) indvidual_msg.appendChild(master_uri) ros_distro = doc.createElement('ros_distro') ros_distro_content = doc.createTextNode(msg.ros_distro) ros_distro.appendChild(ros_distro_content) indvidual_msg.appendChild(ros_distro) duration = doc.createElement('duration') duration_content = doc.createTextNode(str(msg.duration)) duration.appendChild(duration_content) indvidual_msg.appendChild(duration) launches.appendChild(indvidual_msg) log_dir = rospkg.get_log_dir() date_time = time.strftime("%Y-%j-%H-%M-%S") file_dir = log_dir + "/rospalantir-" + date_time + ".xml" f = open(file_dir,'a') f.write(doc.toprettyxml(indent=" ", encoding="utf-8")) f.close() response = os.system('ping -c 1 google.com') if response == 0: rp = rospkg.RosPack() path = rp.get_path('rospalantir') import subprocess subprocess.call(["php", path + "/src/rospalantir/rospalantir_to_server.php", log_dir + "/"])
def get_logs(loglevel): """ Collect the logs from the ros log files. If there is no roscore process running, then it displays the logs from the last run. """ from roslaunch.roslaunch_logs import get_run_id import rospkg import glob import re logger.info('get logs: log level {}'.format(loglevel)) log_cursors = request.form.copy() logger.debug('cursors: {}'.format(log_cursors)) log_root = rospkg.get_log_dir() run_id = get_run_id() roscore_running = True if not run_id: roscore_running = False subdirs = [os.path.join(log_root, d) for d in os.listdir(log_root) if os.path.isdir(os.path.join(log_root, d))] if subdirs: run_id = max(subdirs, key=os.path.getmtime) else: run_id = '' # some extra log files that not created by roslaunch extra_log_files = [os.path.join(log_root, name) for name in [ 'ros_motors_webui.log', 'sophia_Eva_Behavior.log', 'blender_api.log']] extra_log_files = [f for f in extra_log_files if os.path.isfile(f)] log_dir = os.path.join(log_root, run_id) log_files = glob.glob(os.path.join(log_dir, '*.log')) log_files += extra_log_files # ignore stdout log files log_files = [log_file for log_file in log_files if ('stdout' not in log_file) and (not os.path.basename(log_file).startswith('roslaunch'))] log_files = sorted(log_files, key=lambda f: os.path.basename(f)) logger.debug('get log files: {}'.format('\n'.join(log_files))) logs = [] # log format [%(name)s][%(levelname)s] %(asctime)s: %(message)s pattern = r'\[(?P<name>\S+)\]\[(?P<levelname>\S+)\] (?P<asctime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}): (?P<message>.*)' p = re.compile(pattern) loglevels = { 'debug': 0, 'info': 1, 'warn': 2, 'warning': 2, 'error': 3, 'fatal': 4, } def escape(s): if isinstance(s, list): return [escape(i) for i in s] s = s.replace("&", "&") # Must be done first! s = s.replace("<", "<") s = s.replace(">", ">") s = s.replace('"', """) return s def get_log(log_file, loglevel, cursor, message_length=120): """parse log files from the start cursor""" _log = [] loglevel = loglevels[loglevel.lower()] with open(log_file) as f: for _ in xrange(cursor): try: next(f) except StopIteration: pass logrecord = None consume = 0 for line in f: m = p.match(line) if m: if logrecord: if loglevels[logrecord['levelname'].lower()] >= loglevel: _log.append(logrecord) cursor += consume consume = 0 logrecord = None name, levelname, asctime, message = map( m.group, ['name', 'levelname', 'asctime', 'message']) consume += 1 extra = [] if len(message) > message_length: extra.append(message[message_length:]) message = message[:message_length] logrecord = { 'name': name, 'levelname': levelname, 'asctime': asctime, 'message': escape(message), 'extra': escape(extra), } # Append message that doesn't match the log format to the # previous matched log record elif logrecord: consume += 1 logrecord['extra'].append(line) # Append the remaining log record if logrecord: if loglevels[logrecord['levelname'].lower()] >= loglevel: _log.append(logrecord) cursor += consume consume = 0 logrecord = None _log.reverse() return cursor, _log[:LOG_TO_SHOW] def isint(i): try: int(i) except: return False return True def parse_node_name(log_file): base = os.path.splitext(os.path.basename(log_file))[0] tokens = base.split('-') if 'roslaunch' in base: return base try: idx = map(isint, tokens).index(True) node = '/'.join(tokens[:idx]) except: node = '/'.join(tokens) return node for log_file in log_files: node = parse_node_name(log_file) cursor = int(log_cursors.get(log_file, 0)) cursor, log = get_log(log_file, loglevel, cursor) log_cursors[log_file] = cursor if log: logs.append({ 'node': node, 'log': log, 'log_file': log_file, 'log_to_show': LOG_TO_SHOW, }) result = { 'logs': logs, 'cursors': log_cursors } return json_encode(result)
def get_logs(loglevel): """ Collect the logs from the ros log files. If there is no roscore process running, then it displays the logs from the last run. """ from roslaunch.roslaunch_logs import get_run_id import rospkg import glob import re logger.debug('get logs: log level {}'.format(loglevel)) log_cursors = request.form.copy() logger.debug('cursors: {}'.format(log_cursors)) log_root = rospkg.get_log_dir() run_id = get_run_id() roscore_running = True if not run_id: roscore_running = False subdirs = [ os.path.join(log_root, d) for d in os.listdir(log_root) if os.path.isdir(os.path.join(log_root, d)) ] if subdirs: run_id = max(subdirs, key=os.path.getmtime) else: run_id = '' # some extra log files that not created by roslaunch extra_log_files = [] log_dir = os.path.join(log_root, run_id) log_files = glob.glob(os.path.join(log_dir, '*.log')) log_files += extra_log_files # ignore stdout log files log_files = [ log_file for log_file in log_files if ('stdout' not in log_file) and ( not os.path.basename(log_file).startswith('roslaunch')) ] log_files = sorted(log_files, key=lambda f: os.path.basename(f)) logger.debug('get log files: {}'.format('\n'.join(log_files))) logs = [] # log format [%(name)s][%(levelname)s] %(asctime)s: %(message)s pattern = r'\[(?P<name>\S+)\]\[(?P<levelname>\S+)\] (?P<asctime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}): (?P<message>.*)' p = re.compile(pattern) loglevels = { 'debug': 0, 'info': 1, 'warn': 2, 'warning': 2, 'error': 3, 'fatal': 4, } def escape(s): if isinstance(s, list): return [escape(i) for i in s] s = s.replace("&", "&") # Must be done first! s = s.replace("<", "<") s = s.replace(">", ">") s = s.replace('"', """) return s def get_log(log_file, loglevel, cursor, message_length=120): """parse log files from the start cursor""" _log = [] loglevel = loglevels[loglevel.lower()] with open(log_file) as f: for _ in xrange(cursor): try: next(f) except StopIteration: pass logrecord = None consume = 0 for line in f: m = p.match(line) if m: if logrecord: if loglevels[ logrecord['levelname'].lower()] >= loglevel: _log.append(logrecord) cursor += consume consume = 0 logrecord = None name, levelname, asctime, message = map( m.group, ['name', 'levelname', 'asctime', 'message']) consume += 1 extra = [] if len(message) > message_length: extra.append(message[message_length:]) message = message[:message_length] logrecord = { 'name': name, 'levelname': levelname, 'asctime': asctime, 'message': escape(message), 'extra': escape(extra), } # Append message that doesn't match the log format to the # previous matched log record elif logrecord: consume += 1 logrecord['extra'].append(line) # Append the remaining log record if logrecord: if loglevels[logrecord['levelname'].lower()] >= loglevel: _log.append(logrecord) cursor += consume consume = 0 logrecord = None _log.reverse() return cursor, _log[:LOG_TO_SHOW] def isint(i): try: int(i) except: return False return True def parse_node_name(log_file): base = os.path.splitext(os.path.basename(log_file))[0] tokens = base.split('-') if 'roslaunch' in base: return base try: idx = map(isint, tokens).index(True) node = '/'.join(tokens[:idx]) except: node = '/'.join(tokens) return node for log_file in log_files: node = parse_node_name(log_file) cursor = int(log_cursors.get(log_file, 0)) cursor, log = get_log(log_file, loglevel, cursor) log_cursors[log_file] = cursor if log: logs.append({ 'node': node, 'log': log, 'log_file': log_file, 'log_to_show': LOG_TO_SHOW, }) result = {'logs': logs, 'cursors': log_cursors} return json_encode(result)
def _get_check_dirs(): home_dir = rospkg.get_ros_home() log_dir = rospkg.get_log_dir() dirs = [(log_dir, "ROS node logs"), (os.path.join(home_dir, "rosmake"), "rosmake logs")] return [x for x in dirs if os.path.isdir(x[0])]
def _get_check_dirs(): home_dir = rospkg.get_ros_home() log_dir = rospkg.get_log_dir() dirs = [(log_dir, 'ROS node logs'), (os.path.join(home_dir, 'rosmake'), 'rosmake logs')] return [x for x in dirs if os.path.isdir(x[0])]
def _configure_logging(self): """ Configure logging of node's log file and stdout/stderr @return: stdout log file name, stderr log file name. Values are None if stdout/stderr are not logged. @rtype: str, str """ log_dir = rospkg.get_log_dir(env=os.environ) static_log = os.environ.get('ROS_STATIC_LOG_FILE_NAMES', 0) == '1' if self.run_id and not static_log: log_dir = os.path.join(log_dir, self.run_id) if not os.path.exists(log_dir): try: os.makedirs(log_dir) except OSError as e: if e.errno == errno.EACCES: raise RLException("unable to create directory for log file [%s].\nPlease check permissions."%log_dir) else: raise RLException("unable to create directory for log file [%s]: %s"%(log_dir, e.strerror)) # #973: save log dir for error messages self.log_dir = log_dir # send stdout/stderr to file. in the case of respawning, we have to # open in append mode # note: logfileerr: disabling in favor of stderr appearing in the console. # will likely reinstate once roserr/rosout is more properly used. # we create a logger (and we will add to it either a StreamHandler or a FileHandler logger = logging.getLogger(self.package+'.'+self.name) #if we must write on file : if self.log_output: logfname = self._log_name() outf, errf = [os.path.join(log_dir, '%s-%s.log'%(logfname, n)) for n in ['stdout', 'stderr']] if self.respawn or static_log: mode = 'a' else: mode = 'w' # we don't propagate logs into the hierarchy. If we do it, when the node will be killed, some unwanted # stdout will be displayed on the console... logger.propagate = False if self.max_logfile_size: # We create a rotating file handler if max_logfile_size was set by user in XML launch file. hdlr_out = logging.handlers.RotatingFileHandler(outf, mode=mode, maxBytes=self.max_logfile_size, backupCount=self.logfile_count) if is_child_mode(): hdlr_err = logging.handlers.RotatingFileHandler(errf, mode=mode, maxBytes=self.max_logfile_size, backupCount=self.logfile_count) else: # We create a simple file to have the same behaviour as before if there is no maxBytes attributes : hdlr_out = logging.FileHandler(outf, mode=mode) if is_child_mode(): hdlr_err = logging.FileHandler(errf, mode=mode) hdlr_out.setLevel(logging.INFO) logger.addHandler(hdlr_out) if is_child_mode(): hdlr_err.setLevel(logging.ERROR) logger.addHandler(hdlr_err) # #986: pass in logfile name to node node_log_file = log_dir if self.is_node: # #1595: on respawn, these keep appending self.args = _cleanup_remappings(self.args, '__log:=') self.args.append("__log:=%s"%os.path.join(log_dir, "%s.log"%(logfname))) # if We must log to screen if self.screen_output: # we create handlers for stdout and stderr, and we add them to the logger. hdlr_stdout = logging.StreamHandler(sys.stdout) hdlr_stdout.setLevel(logging.INFO) logger.addHandler(hdlr_stdout) if is_child_mode(): hdlr_stderr = logging.StreamHandler(sys.stderr) hdlr_stderr.setLevel(logging.ERROR) logger.addHandler(hdlr_err) return logger
import matplotlib.pyplot as plt import numpy as np import rospkg import os.path Plot_Lateral_Control = False Plot_Longitudinal_Control = True #data from waypoint updater node log_dir = rospkg.get_log_dir() + "/latest" log_dir = os.path.realpath(log_dir) log_file = log_dir + "/" + "waypoint_updater.csv" wu_time, x, y, wp, cx, cy = np.loadtxt(log_file, delimiter=',', skiprows=1, unpack=True) #data from dbw node log_file = log_dir + "/" + "dbw_node.csv" throttle_p_effort, throttle_i_effort, throttle_d_effort, brake_p_effort, brake_i_effort, brake_d_effort, velocity_error, DT, latchBrake, dbw_time, target_linear_velocity, target_angular_velocity, current_linear_velocity, current_angular_velocity, dbw_status, throttle, brake, steering, = np.loadtxt( log_file, delimiter=',', skiprows=1, unpack=True) #time align data from different ros nodes start_time = wu_time[0] dbw_time = dbw_time - start_time wu_time = wu_time - start_time dbw_time = dbw_time * 1E-9 wu_time = wu_time * 1E-9 ################ lateral controller subplot ################## if Plot_Lateral_Control: f, axx = plt.subplots(2, 3)
def output_xml(self): self.preprocess_xml() doc = Document() root = doc.createElement('root') doc.appendChild(root) msgs = doc.createElement('msgs') root.appendChild(msgs) for msg in self.registeredList: rospy.logdebug(msg.process_name) indvidual_msg = doc.createElement('msg') name = doc.createElement('name') name_content = doc.createTextNode(msg.name) name.appendChild(name_content) indvidual_msg.appendChild(name) msg_type = doc.createElement('msg_type') msg_type_content = doc.createTextNode(msg.msg_type) msg_type.appendChild(msg_type_content) indvidual_msg.appendChild(msg_type) topic_type = doc.createElement('topic_type') topic_type_content = doc.createTextNode(msg.topic_type) topic_type.appendChild(topic_type_content) indvidual_msg.appendChild(topic_type) node_name = doc.createElement('node_name') node_name_content = doc.createTextNode(self.processNameToNode[msg.process_name.replace('/','',1)]) node_name.appendChild(node_name_content) indvidual_msg.appendChild(node_name) duration = doc.createElement('duration') duration_content = doc.createTextNode(str(msg.duration)) duration.appendChild(duration_content) indvidual_msg.appendChild(duration) msgs.appendChild(indvidual_msg) launches = doc.createElement('launches') root.appendChild(launches) for msg in self.launchList: indvidual_msg = doc.createElement('launch') package = doc.createElement('package') package_content = doc.createTextNode(msg.package) package.appendChild(package_content) indvidual_msg.appendChild(package) node_name = doc.createElement('node_name') node_name_content = doc.createTextNode(msg.node_name) node_name.appendChild(node_name_content) indvidual_msg.appendChild(node_name) process_name = doc.createElement('process_name') process_name_content = doc.createTextNode(msg.process_name) process_name.appendChild(process_name_content) indvidual_msg.appendChild(process_name) desktop_session = doc.createElement('desktop_session') desktop_session_content = doc.createTextNode(msg.desktop_session) desktop_session.appendChild(desktop_session_content) indvidual_msg.appendChild(desktop_session) master_uri = doc.createElement('master_uri') master_uri_content = doc.createTextNode(msg.master_uri) master_uri.appendChild(master_uri_content) indvidual_msg.appendChild(master_uri) ros_distro = doc.createElement('ros_distro') ros_distro_content = doc.createTextNode(msg.ros_distro) ros_distro.appendChild(ros_distro_content) indvidual_msg.appendChild(ros_distro) duration = doc.createElement('duration') duration_content = doc.createTextNode(str(msg.duration)) duration.appendChild(duration_content) indvidual_msg.appendChild(duration) launches.appendChild(indvidual_msg) log_dir = rospkg.get_log_dir() date_time = time.strftime("%j-%H-%S") file_dir = log_dir + "/roskomodo-" + date_time + ".xml" f = open(file_dir,'a') f.write(doc.toprettyxml(indent=" ", encoding="utf-8"))
def configure_logging(logname, level=logging.INFO, filename=None, env=None): """ Configure Python logging package to send log files to ROS-specific log directory :param logname str: name of logger, ``str`` :param filename: filename to log to. If not set, a log filename will be generated using logname, ``str`` :param env: override os.environ dictionary, ``dict`` :returns: log file name, ``str`` :raises: :exc:`LoggingException` If logging cannot be configured as specified """ if env is None: env = os.environ logname = logname or 'unknown' log_dir = rospkg.get_log_dir(env=env) # if filename is not explicitly provided, generate one using logname if not filename: log_filename = os.path.join(log_dir, '%s-%s.log' % (logname, os.getpid())) else: log_filename = os.path.join(log_dir, filename) logfile_dir = os.path.dirname(log_filename) if not os.path.exists(logfile_dir): try: makedirs_with_parent_perms(logfile_dir) except OSError: # cannot print to screen because command-line tools with output use this if os.path.exists(logfile_dir): # We successfully created the logging folder, but could not change # permissions of the new folder to the same as the parent folder sys.stderr.write( "WARNING: Could not change permissions for folder [%s], make sure that the parent folder has correct permissions.\n" % logfile_dir) else: # Could not create folder sys.stderr.write( "WARNING: cannot create log directory [%s]. Please set %s to a writable location.\n" % (logfile_dir, ROS_LOG_DIR)) return None elif os.path.isfile(logfile_dir): raise LoggingException( "Cannot save log files: file [%s] is in the way" % logfile_dir) # the log dir itself should not be symlinked as latest if logfile_dir != log_dir: if sys.platform not in ['win32']: try: success = renew_latest_logdir(logfile_dir) if not success: sys.stderr.write( "INFO: cannot create a symlink to latest log directory\n" ) except OSError as e: sys.stderr.write( "INFO: cannot create a symlink to latest log directory: %s\n" % e) config_file = os.environ.get('ROS_PYTHON_LOG_CONFIG_FILE') if not config_file: # search for logging config file in ROS_HOME, ROS_ETC_DIR or relative # to the rosgraph package directory. rosgraph_d = rospkg.RosPack().get_path('rosgraph') for config_dir in [ os.path.join(rospkg.get_ros_home(), 'config'), rospkg.get_etc_ros_dir(), os.path.join(rosgraph_d, 'conf') ]: for extension in ('conf', 'yaml'): f = os.path.join( config_dir, 'python_logging{}{}'.format(os.path.extsep, extension)) if os.path.isfile(f): config_file = f break if config_file is not None: break if config_file is None or not os.path.isfile(config_file): # logging is considered soft-fail sys.stderr.write( "WARNING: cannot load logging configuration file, logging is disabled\n" ) logging.getLogger(logname).setLevel(logging.CRITICAL) return log_filename # pass in log_filename as argument to pylogging.conf os.environ['ROS_LOG_FILENAME'] = log_filename if config_file.endswith(('.yaml', '.yml')): with open(config_file) as f: dict_conf = yaml.load(f) dict_conf.setdefault('version', 1) logging.config.dictConfig(dict_conf) else: # #3625: disabling_existing_loggers=False logging.config.fileConfig(config_file, disable_existing_loggers=False) return log_filename