-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
90 lines (69 loc) · 3.18 KB
/
log.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 7 18:41:41 2022
@author: carlos
"""
import os
import typing
import pickle
import time
class Log:
logLevels4File = ['testing','coordinator','operation','operation-details','mqtt-details','worker','dump-population']
logLevels4Screen = ['testing','coordinator','operation','operation-details','mqtt-details','worker','dump-population']
logLevels4Screen = ['coordinator','worker','dump-population']
logLevels4Screen = ['coordinator', 'worker']
logLevels4File = ['coordinator','worker','dump-population']
frontsFileName='fronts.pkl'
def __init__(self,logname: str, messageHeader: str, executionId: str) -> None:
self.logname = logname
self.messageHeader = messageHeader
self.executionId = executionId
self.logPath = './logs/'+self.executionId+'/'
self.resultsPath = './results/'+self.executionId+'/'
if not os.path.exists(self.logPath):
os.mkdir(self.logPath)
def printf(self, str2Print: str, f: typing.TextIO, level: str = None) -> None:
if level == None or level in self.logLevels4File:
f.write(self.messageHeader + str2Print + '\n')
if level == None or level in self.logLevels4Screen:
print(self.messageHeader + str2Print)
def print(self, str2Print: str,level: str = None) -> None:
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
if level==None or level in self.logLevels4File:
f = open (self.logPath+self.logname+'.txt','a')
f.write(self.messageHeader+str2Print+'\n')
f.close()
if level==None or level in self.logLevels4Screen:
print(self.messageHeader+str2Print, flush=True)
def dumpData(self,strData: str, level: str = None) -> None:
f = open(self.logPath + self.logname + '.txt', 'a')
self.printf(strData, f, level)
f.close()
def initializeDumpFronts(self) -> None:
if not os.path.exists(self.resultsPath):
os.mkdir(self.resultsPath)
list2store = []
with open(self.resultsPath + self.frontsFileName, 'wb') as f:
[pickle.dump(elementList, f) for elementList in list2store]
f.close()
def dumpFronts(self,step: int, fronts2Store: list, distInWorkers: dict) -> None:
#TODO https://stackoverflow.com/questions/28077573/python-appending-to-a-pickled-list
with open(self.resultsPath + self.frontsFileName, 'ab') as f:
pickle.dump((step, fronts2Store, distInWorkers), f)
f.close()
def initializeDumpPathLength(self, fn: str) -> None:
if not os.path.exists(self.resultsPath):
os.mkdir(self.resultsPath)
list2store = []
with open(self.resultsPath + fn, 'wb') as f:
[pickle.dump(elementList, f) for elementList in list2store]
f.close()
def copyConfigurationFiles(self,fnList:list) -> None:
for fn in fnList:
os.system("cp "+fn + " "+ self.resultsPath)
def dumpPathLength(self, fn: str, value2append: float) -> None:
with open(self.resultsPath + fn, 'ab') as f:
pickle.dump(value2append, f)
f.close()