]> gitweb.michael.orlitzky.com - djbdns-logparse.git/blob - bin/djbdns-logparse
bin/djbdns-logparse: handle SIGINT (again).
[djbdns-logparse.git] / bin / djbdns-logparse
1 #!/usr/bin/python3
2 r"""
3 Convert tinydns and dnscache logs to human-readable form
4 """
5 from signal import signal, SIGINT
6 from sys import exit, stdin
7 from argparse import ArgumentParser, FileType
8 from djbdns.io import parse_logfile
9
10 # Create an argument parser using the file's docsctring as its
11 # description.
12 parser = ArgumentParser(description = __doc__)
13
14 # Parse zero or more positional arguments into a list of
15 # "logfiles". If none are given, read from stdin instead.
16 parser.add_argument("logfiles",
17 metavar="LOGFILE",
18 type=FileType("r"),
19 nargs="*",
20 default=[stdin],
21 help="djbdns logfile to process (default: stdin)")
22
23 # Warning: argparse automatically opens its file arguments here,
24 # and they only get closed when the program terminates. There's no
25 # real benefit to closing them one-at-a-time after calling
26 # parse_logfile(), because the "scarce" resource of open file
27 # descriptors gets consumed immediately, before any processing has
28 # happened. In other words, if you're going to run out of file
29 # descriptors, it's going to happen right now.
30 #
31 # So anyway, don't run this on several million logfiles.
32 args = parser.parse_args()
33
34 # Install a SIGINT handler so thst we don't spit out a stack trace when
35 # the user accidentally starts the program with no arguments and then
36 # hits Ctrl-C to kill it.
37 signal(SIGINT, lambda s,f: exit(0))
38
39 for f in args.logfiles:
40 parse_logfile(f)