-
Notifications
You must be signed in to change notification settings - Fork 15
/
mux_logger.py
executable file
·111 lines (82 loc) · 1.89 KB
/
mux_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
#! /usr/bin/env python
import os, sys
import socket
import optparse
import time
# Option parsing, duh
parser = optparse.OptionParser()
parser.add_option('-p', '--port',
help = 'Host port',
dest = 'port',
type = 'int',
default = 23200)
parser.add_option('-f', '--file',
help = 'Output file',
dest = 'file',
type = 'string')
(opts, args) = parser.parse_args()
# Helpers
def flush():
sys.stdout.flush()
def _write_simple(x):
sys.stdout.write(x)
def _write_log(x):
sys.stdout.write(x)
log.write(x)
# Setup log file writing
if opts.file:
logname = opts.file
log = open(logname, 'w')
print >>sys.stderr, 'MUX > Logging output to', logname
write = _write_log
else:
write = _write_simple
# Setup client
server_address = ('localhost', opts.port)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(server_address)
print >>sys.stderr, 'MUX > Connected to %s:%d' % server_address
print >>sys.stderr, 'MUX > format: [date time elapsed delta] line'
print >>sys.stderr, 'MUX > Use ctrl+c to stop...\n'
# Init line catcher
base_t = 0
line_t = 0
prev_t = 0
newline = True
current_line = ''
##### MAIN
while True:
try:
# Read 1 char
x = s.recv(1)
# Ignore carriage returns
if x == '\r':
continue
# Set base_t to when first char is received
if not base_t:
base_t = time.time()
if newline:
line_t = time.time()
date = time.localtime(line_t)
elapsed = line_t - base_t
delta = elapsed - prev_t
write('[%04d-%02d-%02d %02d:%02d:%02d %4.3f %4.3f] '
% (date.tm_year, date.tm_mon, date.tm_mday,
date.tm_hour, date.tm_min, date.tm_sec,
elapsed, delta))
prev_t = elapsed
newline = False
# Print it!
write(x)
current_line += x
if x == '\n':
newline = True
current_line = ''
flush()
except:
break
print >>sys.stderr, '\nMUX > Closing...'
s.close()
if opts.file:
log.close()
print >>sys.stderr, 'MUX > Done! =)'