From a77b078f4c76c2e5ab52f4460837c95656cc1c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20M=C3=A4der?= <maeder@phys.ethz.ch> Date: Thu, 12 Apr 2018 14:50:54 +0200 Subject: [PATCH] Add exception handling --- bin/deltalogcat.py | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/bin/deltalogcat.py b/bin/deltalogcat.py index 47aa1e4..267bdf2 100755 --- a/bin/deltalogcat.py +++ b/bin/deltalogcat.py @@ -113,15 +113,22 @@ def tail_file(log_file): fin = subprocess.Popen(['tail', '-F', log_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE) poll = select.poll() poll.register(fin.stdout) - - while True: - start_time = time.perf_counter() - if poll.poll(1): - line = fin.stdout.readline().decode('utf-8') - parse(line) + try: + while True: + start_time = time.perf_counter() + + if poll.poll(1): + line = fin.stdout.readline().decode('utf-8') + + parse(line) + + sleep(start_time) + + except KeyboardInterrupt: + sys.stdout.flush() + pass - sleep(start_time) def read_file(log_file): @@ -129,19 +136,29 @@ def read_file(log_file): with open(log_file, 'r') as fin: while True: line = fin.readline() + if not line: break + parse(line) def read_stdin(): """Read from stdin""" fin = sys.stdin - while True: - line = fin.readline() - if not line: - break - parse(line) + + try: + while True: + line = fin.readline() + + if not line: + break + + parse(line) + + except KeyboardInterrupt: + sys.stdout.flush() + pass def main(): -- GitLab