state_logger.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 import os
4 import time
5 import logging
6 
7 '''
8 Created on 02/18/2015
9 
10 @author: Philipp Schillinger
11 '''
12 class StateLogger(object):
13  '''
14  Realizes logging of active states.
15  '''
16 
17  LOG_FOLDER = "~/.flexbe_logs"
18  enabled = True
19 
20  @staticmethod
21  def initialize(be_name = None):
22  if rospy.has_param("~log_folder"):
23  log_folder = os.path.expanduser(rospy.get_param("~log_folder"))
24  else:
25  log_folder = os.path.expanduser(StateLogger.LOG_FOLDER)
26 
27  if log_folder == "" or not rospy.get_param("~log_enabled", False):
28  StateLogger.enabled = False
29  return
30 
31  if not os.path.exists(log_folder):
32  os.makedirs(log_folder)
33 
34  name = "states"
35  if be_name is not None:
36  name = be_name.replace(" ", "_").replace(",", "_").replace(".", "_").replace("/", "_").lower()
37 
38  filename = os.path.join(log_folder, name + "_" + time.strftime("%Y-%m-%d-%H_%M_%S") + ".log")
39 
40  logger = logging.getLogger('state_logger')
41  handler = logging.FileHandler(filename)
42  formatter = logging.Formatter('%(message)s')
43  handler.setFormatter(formatter)
44  logger.addHandler(handler)
45 
46  # log starting time for being able to calculate execution time of first state
47  logger.info(str(rospy.get_time()) + "," + be_name + ",INIT,INIT,1,1")
48 
49  StateLogger._logger = logger
50  StateLogger._handler = handler
51 
52 
53  @staticmethod
54  def log_state_execution(statepath, stateclass, outcome, is_autonomous, is_executed):
55  """
56  Logs the execution of a state.
57  Should be called once when the state returns an outcome.
58  """
59  if not StateLogger.enabled: return
60  StateLogger._logger.info(
61  str(rospy.get_time()) + "," # timestamp in ros time
62  + statepath + "," # path of the executed state
63  + stateclass + "," # class of the executed state
64  + outcome + "," # resulting outcome
65  + ('1' if is_autonomous else '0') + "," # Outcome triggered by behavior or operator?
66  + ('1' if is_executed else '0') # Outcome actually executed or only requested?
67  )
68 
69 
70  @staticmethod
71  def shutdown():
72  if not StateLogger.enabled: return
73  StateLogger._handler.close()
74  StateLogger._logger.removeHandler(StateLogger._handler)
def log_state_execution(statepath, stateclass, outcome, is_autonomous, is_executed)
Definition: state_logger.py:54


flexbe_core
Author(s): Philipp Schillinger
autogenerated on Wed Jun 5 2019 21:51:59