log-search: Handle ^C more gracefully.

This commit is contained in:
Alex Vandiver 2022-05-04 17:23:39 -07:00 committed by Tim Abbott
parent da4ae3ff24
commit fe17a4d6d0
1 changed files with 25 additions and 21 deletions

View File

@ -4,6 +4,7 @@ import argparse
import gzip import gzip
import os import os
import re import re
import signal
import sys import sys
from enum import Enum, auto from enum import Enum, auto
from typing import Callable, TextIO from typing import Callable, TextIO
@ -215,29 +216,32 @@ def main() -> None:
raise RuntimeError(f"Can't parse {filter} as an IP or hostname.") raise RuntimeError(f"Can't parse {filter} as an IP or hostname.")
assert filter_type is not None assert filter_type is not None
for logfile_name in reversed(logfile_names): try:
with maybe_gzip(logfile_name) as logfile: for logfile_name in reversed(logfile_names):
for logline in logfile: with maybe_gzip(logfile_name) as logfile:
# As a performance optimization, just do a substring for logline in logfile:
# check before we parse the line fully # As a performance optimization, just do a substring
if filter not in logline.lower(): # check before we parse the line fully
continue if filter not in logline.lower():
continue
if args.nginx:
match = NGINX_LOG_LINE_RE.match(logline)
else:
match = PYTHON_LOG_LINE_RE.match(logline)
if match is None:
# We expect other types of loglines in the Python logfiles
if args.nginx: if args.nginx:
print(f"! Failed to parse:\n{logline}", file=sys.stderr) match = NGINX_LOG_LINE_RE.match(logline)
continue else:
if passes_filters(string_filter, match, args): match = PYTHON_LOG_LINE_RE.match(logline)
print_line( if match is None:
match, # We expect other types of loglines in the Python logfiles
args, if args.nginx:
filter_type=filter_type, print(f"! Failed to parse:\n{logline}", file=sys.stderr)
) continue
if passes_filters(string_filter, match, args):
print_line(
match,
args,
filter_type=filter_type,
)
except KeyboardInterrupt:
sys.exit(signal.SIGINT + 128)
def passes_filters( def passes_filters(