forked from mozilla/autophone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autophonelogserver.py
66 lines (56 loc) · 2.22 KB
/
autophonelogserver.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 Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
# Modeled after the example https://docs.python.org/2.7/howto/logging-cookbook.html#sending-and-receiving-logging-events-across-a-network
import SocketServer
import logging
import logging.handlers
import pickle
import struct
import utils
class LogRecordHandler(SocketServer.StreamRequestHandler):
def handle(self):
try:
data = ''
while True:
data = self.connection.recv(4)
if len(data) < 4:
break
pickle_len = struct.unpack('>L', data)[0]
data = self.connection.recv(pickle_len)
while len(data) < pickle_len:
data = data + self.connection.recv(pickle_len - len(data))
record = logging.makeLogRecord(pickle.loads(data))
logger = utils.getLogger(record.name)
logger.handle(record)
except:
logger = utils.getLogger()
logger.exception("Error receiving log record: data: %s", data)
class LogRecordServer(SocketServer.ThreadingTCPServer):
allow_reuse_address = True
request_queue_size = 100
def __init__(self,
autophone=None,
host='localhost',
port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
handler=LogRecordHandler):
SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler)
self.autophone = autophone
self.shutdown_requested = False
self.timeout = 1
def serve_forever(self):
import select
while not self.shutdown_requested:
try:
rlist, wlist, xlist = select.select(
[self.socket.fileno()],
[],
[],
self.timeout)
if rlist:
self.handle_request()
except:
logger = utils.getLogger()
logger.exception("Error selecting log record socket")
def shutdown(self):
self.shutdown_requested = True