]> gitweb.michael.orlitzky.com - djbdns-logparse.git/blobdiff - bin/djbdns-logparse
*/*: refactor into a driver program and a library.
[djbdns-logparse.git] / bin / djbdns-logparse
diff --git a/bin/djbdns-logparse b/bin/djbdns-logparse
new file mode 100755 (executable)
index 0000000..7808fbd
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/python3
+r"""
+Convert tinydns and dnscache logs to human-readable form
+"""
+from argparse import ArgumentParser, FileType
+from djbdns.io import parse_logfile
+
+# Create an argument parser using the file's docsctring as its
+# description.
+parser = ArgumentParser(description = __doc__)
+
+# Parse zero or more positional arguments into a list of
+# "logfiles". If none are given, read from stdin instead.
+from sys import stdin
+parser.add_argument("logfiles",
+                    metavar="LOGFILE",
+                    type=FileType("r"),
+                    nargs="*",
+                    default=[stdin],
+                    help="djbdns logfile to process (default: stdin)")
+
+# Warning: argparse automatically opens its file arguments here,
+# and they only get closed when the program terminates. There's no
+# real benefit to closing them one-at-a-time after calling
+# parse_logfile(), because the "scarce" resource of open file
+# descriptors gets consumed immediately, before any processing has
+# happened. In other words, if you're going to run out of file
+# descriptors, it's going to happen right now.
+#
+# So anyway, don't run this on several million logfiles.
+args = parser.parse_args()
+for f in args.logfiles:
+    parse_logfile(f)