-
Notifications
You must be signed in to change notification settings - Fork 8
/
logger.py
128 lines (111 loc) · 3.54 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import socket
from shutil import copyfile
from pathlib import Path
import threading
import time
import datetime
# UDP Socket bound to the Port 10247
logger = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
logger.bind(("127.0.0.1", 10247))
# Initializing default log directory and file name
log_name = "log.txt"
defaultPath = Path("log.txt")
class Logger():
counter = 30
projectPath = Path("log.txt")
log_file = open(log_name, "w")
defaultReceiveMode = False
def resetCounter(self):
self.counter = 30
def counterTimedOut(self):
return self.counter == 0
def setReceiveDefaultMode(self, value):
"""
Turn defaultReceiveMode on or off
"""
self.defaultReceiveMode = value
def pathIsDefault(self):
return self.projectPath == defaultPath
def changeProjectPath(self, newPath):
"""
Change the log directory to specified newPath, and create a new log file.
Returns the new projectPath
"""
if newPath == defaultPath:
self.closeLog()
if self.pathIsDefault():
self.log_file = self.projectPath.open('a')
else:
self.log_file = newPath.open('w')
elif not newPath == self.projectPath:
self.closeLog()
copyfile(str(self.projectPath), str(newPath))
self.log_file = newPath.open('a')
self.projectPath = newPath
return self.projectPath
def defaultReceive(self, message):
"""
Receive method used when defaultReceiveMode == True,
"""
self.log_file = defaultPath.open('w')
self.projectPath = defaultPath
self.resetCounter()
print(message)
self.log_file.write(message + "\n")
return message.split(' ')
def receiveMessage(self):
"""
Receives message from the socket and writes to the file.
Counter is reset on each call.
Returns the message as an array of strings.
"""
message = bytes.decode(logger.recv(99999))
if self.defaultReceiveMode:
self.setReceiveDefaultMode(False)
return self.defaultReceive(message)
else:
self.resetCounter()
print(message)
self.log_file.write(message + "\n")
return message.split(' ')
def closeLog(self):
"""
Close the log file.
"""
self.log_file.close()
log = Logger()
def check_timeout():
"""
Thread to check if a timeout has occured.
Log directory is changed to default on timeout.
Helps in closing the file every 30 seconds to update it.
"""
while True:
if not log.counterTimedOut():
time.sleep(1)
log.counter -= 1
else:
log.resetCounter()
if not log.pathIsDefault():
log.closeLog()
log.setReceiveDefaultMode(True)
else:
log.changeProjectPath(defaultPath)
t = threading.Thread(target=check_timeout)
log.receiveMessage()
t.start()
try:
while True:
"""
Infinite Loop to receive message, and change the directory of log on a 'projectPath' message.
"""
message = log.receiveMessage()
if message[0] == 'projectPath':
message.pop(0)
log_name = "log [" + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + "].txt"
log.changeProjectPath(Path(' '.join(message)) / log_name)
except Exception as e:
print(e)
finally:
logger.close()
log.closeLog()