-
Notifications
You must be signed in to change notification settings - Fork 6
/
Logging.py
66 lines (44 loc) · 2.03 KB
/
Logging.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
"""
This file is part of ADCS.
ADCS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ADCS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ADCS. If not, see <http://www.gnu.org/licenses/>.
"""
from time import time
import os
class Log(object):
def __init__(self,data_dicts):
""" This class operates extremely fast data logging to disk.
- To be extremely fast, we hold the file pointer until we
are completely done, at which point we close.
- We also make use of join method rather than concatenate, and
only perform 1 write per commit
"""
self.data_dicts = data_dicts # take a list of pointers to data dicts
self.start = time()
logno=0
while (os.path.isfile('./logs/'+"log_"+str(logno)+".csv")):
logno+=1
self.file = open('./logs/'+"log_"+str(logno)+".csv",'a')
join_list = ['T'] # Time always goes first!
for data in self.data_dicts: # Get keys in each data_dict
join_list.extend([',%s' % key for key in data])
join_list.extend(['\n'])
self.file.write(''.join(join_list))
def Commit(self):
""" Commit the current data dictionaries to file """
join_list = [str(time()-self.start)] # Time always goes first!
for data in self.data_dicts: # Get data in each data_dict
join_list.extend([',%s' % data[key] for key in data])
join_list.extend(['\n'])
self.file.write(''.join(join_list))
def close(self):
""" Close the log file """
self.file.close()