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