Go to the documentation of this file.00001
00002 import rospy
00003 import os
00004 import time
00005 import logging
00006
00007 '''
00008 Created on 02/18/2015
00009
00010 @author: Philipp Schillinger
00011 '''
00012 class StateLogger(object):
00013 '''
00014 Realizes logging of active states.
00015 '''
00016
00017 LOG_FOLDER = "~/.flexbe_logs"
00018 enabled = True
00019
00020 @staticmethod
00021 def initialize(be_name = None):
00022 if rospy.has_param("~log_folder"):
00023 log_folder = os.path.expanduser(rospy.get_param("~log_folder"))
00024 else:
00025 log_folder = os.path.expanduser(StateLogger.LOG_FOLDER)
00026
00027 if log_folder == "" or not rospy.get_param("~log_enabled", False):
00028 StateLogger.enabled = False
00029 return
00030
00031 if not os.path.exists(log_folder):
00032 os.makedirs(log_folder)
00033
00034 name = "states"
00035 if be_name is not None:
00036 name = be_name.replace(" ", "_").replace(",", "_").replace(".", "_").replace("/", "_").lower()
00037
00038 filename = os.path.join(log_folder, name + "_" + time.strftime("%Y-%m-%d-%H_%M_%S") + ".log")
00039
00040 logger = logging.getLogger('state_logger')
00041 handler = logging.FileHandler(filename)
00042 formatter = logging.Formatter('%(message)s')
00043 handler.setFormatter(formatter)
00044 logger.addHandler(handler)
00045
00046
00047 logger.info(str(rospy.get_time()) + "," + be_name + ",INIT,INIT,1,1")
00048
00049 StateLogger._logger = logger
00050 StateLogger._handler = handler
00051
00052
00053 @staticmethod
00054 def log_state_execution(statepath, stateclass, outcome, is_autonomous, is_executed):
00055 """
00056 Logs the execution of a state.
00057 Should be called once when the state returns an outcome.
00058 """
00059 if not StateLogger.enabled: return
00060 StateLogger._logger.info(
00061 str(rospy.get_time()) + ","
00062 + statepath + ","
00063 + stateclass + ","
00064 + outcome + ","
00065 + ('1' if is_autonomous else '0') + ","
00066 + ('1' if is_executed else '0')
00067 )
00068
00069
00070 @staticmethod
00071 def shutdown():
00072 if not StateLogger.enabled: return
00073 StateLogger._handler.close()
00074 StateLogger._logger.removeHandler(StateLogger._handler)