-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.py
executable file
·98 lines (69 loc) · 3.17 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import yaml, os, shelve, shutil
class Logger(object):
def __init__(self, case, visualization=None, save_video=False, prefix_folder="logs"):
self._viz = visualization
self._save_video = save_video
#Case name is assumed to be unique
self._case_name = str(case)
self._generate_filenames(prefix_folder)
if os.path.exists(self._log_folder):
shutil.rmtree(self._log_folder)
os.makedirs(self._log_folder)
self._dump_config(case.config)
if self._save_video:
self._video_folder = os.path.join(self._log_folder, "video")
os.makedirs(self._video_folder)
else:
self._video_folder = None
self._iteration = 0
self._log_delay = 1.
def set_log_delay(self, log_delay):
self._log_delay = log_delay
def _generate_filenames(self, folder):
self._log_folder = os.path.join(folder, self._case_name)
self._config_filename = os.path.join(self._log_folder, "config.txt")
self._log_filename = os.path.join(self._log_folder, "simulation.log")
def _dump_config(self, config):
with open(self._config_filename, "w") as file_handle:
yaml.dump(config, file_handle, default_flow_style=False)
def _read_config(self):
with open(self._config_filename, "r") as file_handle:
return yaml.load(file_handle)
def _read_logfile(self):
with open(self._log_filename,"r") as logfile:
data = logfile.read()
return data
def _dump_logfile(self, data):
with open(self._log_filename,"w") as logfile:
logfile.write(data)
def update(self, current_time, case):
if self._viz is not None:
self._viz.update(self._iteration, case, folder=self._video_folder)
self._simulation_log[str(self._iteration)] = case #sdata
self._iteration += 1
return [(current_time + self._log_delay, self.update)]
def __enter__(self):
self._simulation_log = shelve.DbfilenameShelf(self._log_filename, protocol=2, writeback=False)
return self
def __exit__(self, type, value, traceback):
if getattr(self, '_viz', None) is not None :
self._viz.close()
self._simulation_log.close()
def __getstate__(self):
self._simulation_log.close()
state = self.__dict__.copy()
del state["_viz"]
state["logfile_content"] = self._read_logfile()
state["configfile_content"] = self._read_config()
shutil.rmtree(self._log_folder)
return state
def __setstate__(self, state):
self.__dict__ = state
self._generate_filenames("logs")
if os.path.exists(self._log_folder):
shutil.rmtree(self._log_folder)
os.makedirs(self._log_folder)
self._dump_config(self.configfile_content)
self._dump_logfile(self.logfile_content)
del self.logfile_content
del self.configfile_content