2017-12-12 23:51:50 +03:00
|
|
|
#!/usr/bin/env python2
|
2017-12-18 20:14:10 +03:00
|
|
|
# Script to extract config and shutdown information file a klippy.log file
|
2017-12-12 23:51:50 +03:00
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2018-09-28 19:04:59 +03:00
|
|
|
import sys, re, collections, ast
|
2017-12-12 23:51:50 +03:00
|
|
|
|
|
|
|
def format_comment(line_num, line):
|
|
|
|
return "# %6d: %s" % (line_num, line)
|
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Config file extraction
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
class GatherConfig:
|
|
|
|
def __init__(self, configs, line_num, recent_lines, logname):
|
|
|
|
self.configs = configs
|
|
|
|
self.line_num = line_num
|
|
|
|
self.config_num = len(configs) + 1
|
|
|
|
self.filename = "%s.config%04d.cfg" % (logname, self.config_num)
|
|
|
|
self.config_lines = []
|
|
|
|
self.comments = []
|
|
|
|
def add_line(self, line_num, line):
|
|
|
|
if line != '=======================':
|
|
|
|
self.config_lines.append(line)
|
|
|
|
return True
|
|
|
|
self.finalize()
|
|
|
|
return False
|
|
|
|
def finalize(self):
|
|
|
|
lines = tuple(self.config_lines)
|
|
|
|
ch = self.configs.get(lines)
|
|
|
|
if ch is None:
|
|
|
|
self.configs[lines] = ch = self
|
|
|
|
else:
|
|
|
|
ch.comments.extend(self.comments)
|
|
|
|
ch.comments.append(format_comment(self.line_num, "config file"))
|
|
|
|
def add_comment(self, comment):
|
|
|
|
if comment is not None:
|
|
|
|
self.comments.append(comment)
|
|
|
|
def write_file(self):
|
|
|
|
f = open(self.filename, 'wb')
|
|
|
|
f.write('\n'.join(self.comments + self.config_lines))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Shutdown extraction
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
stats_seq_s = r" send_seq=(?P<sseq>[0-9]+) receive_seq=(?P<rseq>[0-9]+) "
|
|
|
|
count_s = r"(?P<count>[0-9]+)"
|
|
|
|
time_s = r"(?P<time>[0-9]+[.][0-9]+)"
|
|
|
|
esttime_s = r"(?P<esttime>[0-9]+[.][0-9]+)"
|
|
|
|
shortseq_s = r"(?P<shortseq>[0-9a-f])"
|
2018-01-03 05:48:25 +03:00
|
|
|
stats_r = re.compile(r"^Stats " + time_s + ": ")
|
2017-12-12 23:51:50 +03:00
|
|
|
serial_dump_r = re.compile(r"^Dumping serial stats: .*" + stats_seq_s)
|
|
|
|
send_dump_r = re.compile(r"^Dumping send queue " + count_s + " messages$")
|
|
|
|
sent_r = re.compile(r"^Sent " + count_s + " " + esttime_s + " " + time_s
|
|
|
|
+ " [0-9]+: seq: 1" + shortseq_s + ",")
|
|
|
|
receive_dump_r = re.compile(r"^Dumping receive queue " + count_s + " messages$")
|
|
|
|
receive_r = re.compile(r"^Receive: " + count_s + " " + time_s + " " + esttime_s
|
|
|
|
+ " [0-9]+: seq: 1" + shortseq_s + ",")
|
2018-09-28 19:04:59 +03:00
|
|
|
gcode_r = re.compile(r"^Read " + time_s + r": (?P<gcode>['\"].*)$")
|
|
|
|
gcode_state_r = re.compile(r"^gcode state: ")
|
|
|
|
varlist_split_r = re.compile(r"([^ ]+)=")
|
2017-12-12 23:51:50 +03:00
|
|
|
clock_r = re.compile(r"^clocksync state: .* clock_est=\((?P<st>[^ ]+)"
|
|
|
|
+ r" (?P<sc>[0-9]+) (?P<f>[^ ]+)\)")
|
|
|
|
repl_seq_r = re.compile(r": seq: 1" + shortseq_s)
|
2018-06-12 17:12:02 +03:00
|
|
|
repl_clock_r = re.compile(r"clock=(?P<clock>[0-9]+)(?: |$)")
|
2018-01-12 19:57:22 +03:00
|
|
|
mcu_r = re.compile(r"MCU '(?P<mcu>[^']+)' (is_)?shutdown: (?P<reason>.*)$")
|
2017-12-12 23:51:50 +03:00
|
|
|
|
|
|
|
def add_high_bits(val, ref, mask):
|
|
|
|
half = (mask + 1) // 2
|
|
|
|
return ref + ((val - (ref & mask) + half) & mask) - half
|
|
|
|
|
|
|
|
class GatherShutdown:
|
2018-01-03 05:48:25 +03:00
|
|
|
class mcu_info:
|
|
|
|
def __init__(self):
|
|
|
|
self.sent_stream = []
|
|
|
|
self.receive_stream = []
|
|
|
|
self.sent_stream = []
|
|
|
|
self.receive_stream = []
|
|
|
|
self.sent_time_to_seq = {}
|
|
|
|
self.sent_seq_to_time = {}
|
|
|
|
self.receive_seq_to_time = {}
|
|
|
|
self.clock_est = 0., 0., 1.
|
|
|
|
self.shutdown_seq = None
|
|
|
|
self.send_count = self.receive_count = 0
|
2017-12-12 23:51:50 +03:00
|
|
|
def __init__(self, configs, line_num, recent_lines, logname):
|
|
|
|
self.shutdown_line_num = line_num
|
|
|
|
self.filename = "%s.shutdown%05d" % (logname, line_num)
|
2018-09-28 19:04:59 +03:00
|
|
|
self.gcode_filename = "%s.gcode%05d" % (logname, line_num)
|
2018-01-21 23:56:13 +03:00
|
|
|
self.comments = []
|
|
|
|
if configs:
|
|
|
|
configs_by_id = {c.config_num: c for c in configs.values()}
|
|
|
|
config = configs_by_id[max(configs_by_id.keys())]
|
|
|
|
config.add_comment(format_comment(line_num, recent_lines[-1][1]))
|
2018-01-27 06:29:27 +03:00
|
|
|
self.comments.append("# config %s" % (config.filename,))
|
2017-12-12 23:51:50 +03:00
|
|
|
self.stats_stream = []
|
|
|
|
self.gcode_stream = []
|
2018-09-28 19:04:59 +03:00
|
|
|
self.gcode_state = ''
|
2018-01-03 05:48:25 +03:00
|
|
|
self.mcus = {}
|
|
|
|
self.mcu = None
|
2017-12-12 23:51:50 +03:00
|
|
|
self.first_stat_time = self.last_stat_time = None
|
|
|
|
for line_num, line in recent_lines:
|
|
|
|
self.parse_line(line_num, line)
|
|
|
|
self.first_stat_time = self.last_stat_time
|
|
|
|
def add_comment(self, comment):
|
|
|
|
if comment is not None:
|
|
|
|
self.comments.append(comment)
|
2018-09-28 19:04:59 +03:00
|
|
|
def extract_params(self, line):
|
|
|
|
parts = varlist_split_r.split(line)
|
|
|
|
try:
|
|
|
|
return { parts[i]: ast.literal_eval(parts[i+1].strip())
|
|
|
|
for i in range(1, len(parts), 2) }
|
|
|
|
except:
|
|
|
|
return {}
|
|
|
|
def handle_gcode_state(self, line):
|
|
|
|
kv = self.extract_params(line)
|
|
|
|
out = ['; Start g-code state restore', 'G28']
|
|
|
|
if not kv['absolutecoord']:
|
|
|
|
out.append('G91')
|
|
|
|
if not kv['absoluteextrude']:
|
|
|
|
out.append('M83')
|
|
|
|
lp = kv['last_position']
|
|
|
|
out.append('G1 X%f Y%f Z%f F%f' % (
|
|
|
|
lp[0], lp[1], lp[2], kv['speed'] * 60.))
|
|
|
|
bp = kv['base_position']
|
|
|
|
if bp[:3] != [0., 0., 0.]:
|
|
|
|
out.append('; Must manually set base position...')
|
|
|
|
out.append('G92 E%f' % (lp[3] - bp[3],))
|
|
|
|
hp = kv['homing_position']
|
|
|
|
if hp != [0., 0., 0., 0.]:
|
|
|
|
out.append('; Must manually set homing position...')
|
|
|
|
if abs(kv['speed_factor'] - 1. / 60.) > .000001:
|
|
|
|
out.append('M220 S%f' % (kv['speed_factor'] * 60. * 100.,))
|
|
|
|
if kv['extrude_factor'] != 1.:
|
|
|
|
out.append('M221 S%f' % (kv['extrude_factor'] * 100.,))
|
|
|
|
out.extend(['; End of state restore', '', ''])
|
|
|
|
self.gcode_state = '\n'.join(out)
|
2018-01-03 05:48:25 +03:00
|
|
|
def check_stats_seq(self, ts, line):
|
|
|
|
# Parse stats
|
|
|
|
parts = line.split()
|
|
|
|
mcu = ""
|
|
|
|
keyparts = {}
|
|
|
|
for p in parts[2:]:
|
2018-05-24 01:19:50 +03:00
|
|
|
if '=' not in p:
|
2018-01-03 05:48:25 +03:00
|
|
|
mcu = p
|
|
|
|
continue
|
|
|
|
name, val = p.split('=', 1)
|
|
|
|
keyparts[mcu + name] = val
|
|
|
|
min_ts = 0
|
|
|
|
max_ts = 999999999999
|
|
|
|
for mcu, info in self.mcus.items():
|
|
|
|
sname = '%s:send_seq' % (mcu,)
|
|
|
|
rname = '%s:receive_seq' % (mcu,)
|
|
|
|
if sname not in keyparts:
|
|
|
|
continue
|
|
|
|
sseq = int(keyparts[sname])
|
|
|
|
rseq = int(keyparts[rname])
|
|
|
|
min_ts = max(min_ts, info.sent_seq_to_time.get(sseq-1, 0),
|
|
|
|
info.receive_seq_to_time.get(rseq, 0))
|
|
|
|
max_ts = min(max_ts, info.sent_seq_to_time.get(sseq, 999999999999),
|
|
|
|
info.receive_seq_to_time.get(rseq+1, 999999999999))
|
|
|
|
return min(max(ts, min_ts + 0.00000001), max_ts - 0.00000001)
|
2017-12-12 23:51:50 +03:00
|
|
|
def trans_clock(self, clock, ts):
|
2018-01-03 05:48:25 +03:00
|
|
|
sample_time, sample_clock, freq = self.mcu.clock_est
|
2017-12-12 23:51:50 +03:00
|
|
|
exp_clock = int(sample_clock + (ts - sample_time) * freq)
|
|
|
|
ext_clock = add_high_bits(clock, exp_clock, 0xffffffff)
|
|
|
|
return sample_time + (ext_clock - sample_clock) / freq
|
|
|
|
def annotate(self, line, seq, ts):
|
2017-12-29 23:17:11 +03:00
|
|
|
if seq is not None:
|
|
|
|
line = repl_seq_r.sub(r"\g<0>(%d)" % (seq,), line)
|
2017-12-12 23:51:50 +03:00
|
|
|
def clock_update(m):
|
|
|
|
return m.group(0)[:-1] + "(%.6f) " % (
|
|
|
|
self.trans_clock(int(m.group('clock')), ts),)
|
|
|
|
line = repl_clock_r.sub(clock_update, line)
|
|
|
|
return line
|
|
|
|
def add_line(self, line_num, line):
|
|
|
|
self.parse_line(line_num, line)
|
|
|
|
if (self.first_stat_time is not None
|
|
|
|
and self.last_stat_time > self.first_stat_time + 5.):
|
|
|
|
self.finalize()
|
|
|
|
return False
|
|
|
|
if (line.startswith('Git version') or line.startswith('Start printer at')
|
|
|
|
or line == '===== Config file ====='):
|
|
|
|
self.finalize()
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
def parse_line(self, line_num, line):
|
|
|
|
m = sent_r.match(line)
|
|
|
|
if m is not None:
|
|
|
|
shortseq = int(m.group('shortseq'), 16)
|
2018-01-03 05:48:25 +03:00
|
|
|
seq = (self.mcu.shutdown_seq + int(m.group('count'))
|
|
|
|
- self.mcu.send_count)
|
2017-12-12 23:51:50 +03:00
|
|
|
seq = add_high_bits(shortseq, seq, 0xf)
|
|
|
|
ts = float(m.group('time'))
|
|
|
|
esttime = float(m.group('esttime'))
|
2018-01-03 05:48:25 +03:00
|
|
|
self.mcu.sent_time_to_seq[(esttime, seq & 0xf)] = seq
|
|
|
|
self.mcu.sent_seq_to_time[seq] = ts
|
2017-12-12 23:51:50 +03:00
|
|
|
line = self.annotate(line, seq, ts)
|
2018-01-03 05:48:25 +03:00
|
|
|
self.mcu.sent_stream.append((ts, line_num, line))
|
2017-12-12 23:51:50 +03:00
|
|
|
return
|
|
|
|
m = receive_r.match(line)
|
|
|
|
if m is not None:
|
|
|
|
shortseq = int(m.group('shortseq'), 16)
|
|
|
|
ts = float(m.group('time'))
|
|
|
|
esttime = float(m.group('esttime'))
|
2018-01-03 05:48:25 +03:00
|
|
|
seq = self.mcu.sent_time_to_seq.get((esttime, (shortseq - 1) & 0xf))
|
2017-12-12 23:51:50 +03:00
|
|
|
if seq is not None:
|
2018-01-03 05:48:25 +03:00
|
|
|
self.mcu.receive_seq_to_time[seq + 1] = ts
|
2017-12-12 23:51:50 +03:00
|
|
|
line = self.annotate(line, seq, ts)
|
2018-01-03 05:48:25 +03:00
|
|
|
self.mcu.receive_stream.append((ts, line_num, line))
|
2017-12-12 23:51:50 +03:00
|
|
|
return
|
|
|
|
m = gcode_r.match(line)
|
|
|
|
if m is not None:
|
|
|
|
ts = float(m.group('time'))
|
2018-09-28 19:04:59 +03:00
|
|
|
self.gcode_stream.append((ts, line_num, line, m.group('gcode')))
|
|
|
|
return
|
|
|
|
m = gcode_state_r.match(line)
|
|
|
|
if m is not None:
|
|
|
|
self.handle_gcode_state(line)
|
2017-12-12 23:51:50 +03:00
|
|
|
return
|
|
|
|
m = stats_r.match(line)
|
|
|
|
if m is not None:
|
|
|
|
ts = float(m.group('time'))
|
|
|
|
self.last_stat_time = ts
|
|
|
|
if self.first_stat_time is None:
|
|
|
|
self.first_stat_time = ts
|
2018-01-03 05:48:25 +03:00
|
|
|
self.stats_stream.append((ts, line_num, line))
|
2017-12-12 23:51:50 +03:00
|
|
|
return
|
2018-01-03 05:48:25 +03:00
|
|
|
m = mcu_r.match(line)
|
|
|
|
if m is not None:
|
|
|
|
mcu = m.group('mcu')
|
|
|
|
self.mcu = self.mcus.setdefault(mcu, self.mcu_info())
|
2017-12-12 23:51:50 +03:00
|
|
|
m = clock_r.match(line)
|
|
|
|
if m is not None:
|
|
|
|
st = float(m.group('st'))
|
|
|
|
sc = int(m.group('sc'))
|
|
|
|
f = float(m.group('f'))
|
2018-01-03 05:48:25 +03:00
|
|
|
self.mcu.clock_est = (st, sc, f)
|
2017-12-12 23:51:50 +03:00
|
|
|
m = serial_dump_r.match(line)
|
|
|
|
if m is not None:
|
2018-01-03 05:48:25 +03:00
|
|
|
self.mcu.shutdown_seq = int(m.group('rseq'))
|
2017-12-12 23:51:50 +03:00
|
|
|
m = send_dump_r.match(line)
|
|
|
|
if m is not None:
|
2018-01-03 05:48:25 +03:00
|
|
|
self.mcu.send_count = int(m.group('count'))
|
2017-12-12 23:51:50 +03:00
|
|
|
m = receive_dump_r.match(line)
|
|
|
|
if m is not None:
|
2018-01-03 05:48:25 +03:00
|
|
|
self.mcu.receive_count = int(m.group('count'))
|
|
|
|
self.stats_stream.append((None, line_num, line))
|
2017-12-12 23:51:50 +03:00
|
|
|
def finalize(self):
|
|
|
|
# Ignore old stats
|
2018-01-03 05:48:25 +03:00
|
|
|
stream_ts = [i[0] for mcu in self.mcus.values()
|
|
|
|
for i in mcu.sent_stream + mcu.receive_stream]
|
2017-12-12 23:51:50 +03:00
|
|
|
if not stream_ts:
|
|
|
|
return
|
|
|
|
min_stream_ts = min(stream_ts)
|
|
|
|
max_stream_ts = max(stream_ts)
|
|
|
|
for i, info in enumerate(self.stats_stream):
|
|
|
|
if info[0] is not None and info[0] >= min_stream_ts - 5.:
|
|
|
|
del self.stats_stream[:i]
|
|
|
|
break
|
|
|
|
# Improve accuracy of stats timestamps
|
|
|
|
last_ts = self.stats_stream[0][0]
|
2018-01-03 05:48:25 +03:00
|
|
|
for i, (ts, line_num, line) in enumerate(self.stats_stream):
|
2017-12-12 23:51:50 +03:00
|
|
|
if ts is not None:
|
2018-01-03 05:48:25 +03:00
|
|
|
last_ts = self.check_stats_seq(ts, line)
|
2017-12-12 23:51:50 +03:00
|
|
|
elif line_num >= self.shutdown_line_num and last_ts <= max_stream_ts:
|
|
|
|
last_ts = max_stream_ts + 0.00000001
|
2018-01-03 05:48:25 +03:00
|
|
|
self.stats_stream[i] = (last_ts, line_num, line)
|
2017-12-12 23:51:50 +03:00
|
|
|
# Make sure no timestamp goes backwards
|
2018-01-03 05:48:25 +03:00
|
|
|
streams = ([self.stats_stream, self.gcode_stream] +
|
|
|
|
[mcu.sent_stream for mcu in self.mcus.values()] +
|
|
|
|
[mcu.receive_stream for mcu in self.mcus.values()])
|
|
|
|
for s in streams:
|
2017-12-12 23:51:50 +03:00
|
|
|
for i in range(1, len(s)):
|
|
|
|
if s[i-1][0] > s[i][0]:
|
|
|
|
s[i] = (s[i-1][0], s[i][1], s[i][2])
|
|
|
|
# Produce output sorted by timestamp
|
2018-01-03 05:48:25 +03:00
|
|
|
out = [i for s in streams for i in s]
|
2017-12-12 23:51:50 +03:00
|
|
|
out.sort()
|
|
|
|
out = [i[2] for i in out]
|
|
|
|
f = open(self.filename, 'wb')
|
|
|
|
f.write('\n'.join(self.comments + out))
|
|
|
|
f.close()
|
2018-09-28 19:04:59 +03:00
|
|
|
# Produce output gcode stream
|
|
|
|
if self.gcode_stream:
|
|
|
|
data = [ast.literal_eval(gc[3]) for gc in self.gcode_stream]
|
|
|
|
f = open(self.gcode_filename, 'wb')
|
|
|
|
f.write(self.gcode_state + ''.join(data))
|
|
|
|
f.close()
|
2017-12-12 23:51:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Startup
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def main():
|
|
|
|
logname = sys.argv[1]
|
|
|
|
last_git = last_start = None
|
|
|
|
configs = {}
|
|
|
|
handler = None
|
|
|
|
recent_lines = collections.deque([], 200)
|
|
|
|
# Parse log file
|
|
|
|
f = open(logname, 'rb')
|
|
|
|
for line_num, line in enumerate(f):
|
|
|
|
line = line.rstrip()
|
|
|
|
line_num += 1
|
|
|
|
recent_lines.append((line_num, line))
|
|
|
|
if handler is not None:
|
|
|
|
ret = handler.add_line(line_num, line)
|
|
|
|
if ret:
|
|
|
|
continue
|
|
|
|
recent_lines.clear()
|
|
|
|
handler = None
|
|
|
|
if line.startswith('Git version'):
|
|
|
|
last_git = format_comment(line_num, line)
|
|
|
|
elif line.startswith('Start printer at'):
|
|
|
|
last_start = format_comment(line_num, line)
|
|
|
|
elif line == '===== Config file =====':
|
|
|
|
handler = GatherConfig(configs, line_num, recent_lines, logname)
|
|
|
|
handler.add_comment(last_git)
|
|
|
|
handler.add_comment(last_start)
|
|
|
|
elif 'shutdown: ' in line or line.startswith('Dumping '):
|
|
|
|
handler = GatherShutdown(configs, line_num, recent_lines, logname)
|
|
|
|
handler.add_comment(last_git)
|
|
|
|
handler.add_comment(last_start)
|
|
|
|
if handler is not None:
|
|
|
|
handler.finalize()
|
|
|
|
# Write found config files
|
|
|
|
for cfg in configs.values():
|
|
|
|
cfg.write_file()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|