From add528532e3c1a5604d512d8411d2b454d7c8da1 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 12 Dec 2017 15:51:50 -0500 Subject: [PATCH] logextract: Rename extractconfig.py to logextract.py and add shutdown parsing Add initial support for extracting out shutdown information from a klippy.log file. The shutdown dump will be reordered into the sequence that they occurred, and timestamps/sequence numbers in the mcu message dump will be expanded. Signed-off-by: Kevin O'Connor --- scripts/extractconfig.py | 56 --------- scripts/logextract.py | 259 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 259 insertions(+), 56 deletions(-) delete mode 100755 scripts/extractconfig.py create mode 100755 scripts/logextract.py diff --git a/scripts/extractconfig.py b/scripts/extractconfig.py deleted file mode 100755 index b50afc51..00000000 --- a/scripts/extractconfig.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env python2 -# Script to extract a config file from a log -# -# Copyright (C) 2017 Kevin O'Connor -# -# This file may be distributed under the terms of the GNU GPLv3 license. -import sys - -def format_comment(line_num, line): - return "# %6d: %s" % (line_num, line) - -def main(): - logname = sys.argv[1] - f = open(logname, 'rb') - last_git = last_start = '' - config_count = config_line_num = 0 - configs = {} - config_lines = [] - info = None - # Parse log file - for line_num, line in enumerate(f): - line = line.strip() - line_num += 1 - if config_line_num: - if line == '=======================': - lines = tuple(config_lines) - info = configs.get(lines) - if info is None: - config_count += 1 - configs[lines] = info = (config_count, []) - info[1].append(format_comment(config_line_num, "config file")) - if last_git: - info[1].append(last_git) - if last_start: - info[1].append(last_start) - config_line_num = 0 - config_lines = [] - else: - config_lines.append(line) - elif 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 =====': - config_line_num = line_num - elif info is not None and ( - ' shutdown: ' in line or line.startswith('Dumping ')): - info[1].append(format_comment(line_num, line)) - # Write found config files - for lines, (count, comments) in configs.items(): - f = open("%s.config%04d.cfg" % (logname, count), 'wb') - f.write('\n'.join(tuple(comments) + lines)) - f.close() - -if __name__ == '__main__': - main() diff --git a/scripts/logextract.py b/scripts/logextract.py new file mode 100755 index 00000000..0fb9dcca --- /dev/null +++ b/scripts/logextract.py @@ -0,0 +1,259 @@ +#!/usr/bin/env python2 +# Script to extract a config file from a log +# +# Copyright (C) 2017 Kevin O'Connor +# +# This file may be distributed under the terms of the GNU GPLv3 license. +import sys, re, collections + +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[0-9]+) receive_seq=(?P[0-9]+) " +count_s = r"(?P[0-9]+)" +time_s = r"(?P