-
Notifications
You must be signed in to change notification settings - Fork 2
/
Logger.py
144 lines (104 loc) · 5.28 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'''
Created on Mar 8, 2016
@author: nanzhao@media.mit.edu, azaria@media.mit.edu
'''
import os
import datetime
import json
from twisted.protocols import basic
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
class LoggingWebsocketControll(WebSocketServerProtocol):
def onConnect(self, request):
print("LoggingWebsocketControll: Received Contol Connection")
def onOpen(self):
print("LoggingWebsocketControll: WebSocket connection open.")
def onMessage(self, payload, isBinary):
print("LoggingWebsocketControll: Received Command - %s" % (payload))
command = json.loads(payload)
self.handle_command(command)
def onClose(self, wasClean, code, reason):
print("LoggingWebsocketControll: WebSocket connection closed: {0}".format(reason))
def handle_command(self, command):
dispatcher = {"LOG": self.handle_log_command,
"STOP_LOG": self.handle_stop_log_command}
dispatcher[command["type"]](command)
def handle_stop_log_command(self,command):
self.logger_container.write_to_log_lock.lock_writing_to_log_file()
def handle_log_command(self, command):
log_files_prefix = "%s_%s" % (command["subject"],command["name"])
self.logger_container.write_to_log_lock.lock_writing_to_log_file()
self.logger_container.new_logging_session(log_files_prefix)
self.logger_container.write_to_log_lock.unlock_writing_to_log_file()
def set_logger_container(self, logger_container):
self.logger_container = logger_container
class LoggingWebsocketControlFactory(WebSocketServerFactory):
def buildProtocol(self, addr):
proto = LoggingWebsocketControll()
proto.set_logger_container(self.logger_container) # This part will be set externally by the user.
proto.factory = self
return proto
class LoggingUserControl(basic.LineReceiver):
# This part sets the delimiter to be the one of the OS in which the server is running.
from os import linesep as delimiter
def connectionMade(self):
self.transport.write('LoggingUserControl: Initializing Logging to OFF - Send ON to start Logging \n')
def set_write_to_log_lock(self, lock):
self.write_to_log_lock = lock
def lineReceived(self, line):
self.transport.write("LoggingUserControl: Received user request - " + line)
if "ON" in line:
self.write_to_log_lock.unlock_writing_to_log_file()
self.transport.write("LoggingUserControl: Logging enabled \n")
if "OFF" in line:
self.write_to_log_lock.lock_writing_to_log_file()
self.transport.write("LoggingUserControl: Logging disabled \n")
class WriteToLogLock(object):
def __init__(self, reactor, period):
self.is_write_locked = True
self.reactor = reactor
self.period = period
def unlock_writing_to_log_file(self):
def lock_writing_to_log_file_callback():
self.is_write_locked = True
print "Logging is paused after period timeout"
self.is_write_locked = False
if self.period != 0:
self.reactor.callLater(self.period, lock_writing_to_log_file_callback)
def lock_writing_to_log_file(self):
self.is_write_locked = True
class DataLogger(object):
def __init__(self, base_path, file_name, columns_list, write_to_log_lock):
current_time = datetime.datetime.now().strftime("%Y%m%d_%I%M%S")
self.create_directory_if_does_not_exist(base_path, current_time)
self.path = os.path.join(base_path + "_" + current_time, file_name + "_" + current_time)
self.log_file = open(self.path, "wa")
self.log_file.write(",".join(columns_list))
self.log_file.write("\r\n")
print "Logging incoming data into %s " % self.path
self.lock = write_to_log_lock
self.columns_list = columns_list
def create_directory_if_does_not_exist(self, base_path, current_time):
if os.path.exists(base_path + "_" + current_time): return
os.makedirs(base_path + "_" + current_time)
def write_tuple_to_log_file(self, values_in_tuple, show_on_screen=False):
tuple_to_list = [str(value) for value in values_in_tuple]
self.write_list_to_log_file(tuple_to_list, show_on_screen)
def write_dict_to_log_file(self, values_in_dictionary, show_on_screen=False):
dict_to_list = [values_in_dictionary[key] for key in self.columns_list]
self.write_list_to_log_file(dict_to_list, show_on_screen)
def write_list_to_log_file(self, values_in_list, show_on_screen=False):
if self.lock.is_write_locked:
return
line_to_write = ",".join(values_in_list) + "\r\n"
self.log_file.write(line_to_write)
if show_on_screen:
print line_to_write
def write_line(self, line):
if self.lock.is_write_locked:
return
self.log_file.write(line + "\r\n")
def close_log_file(self):
self.log_file.flush()
self.log_file.close()
if __name__ == "__main__":
print "hey!"