]>
gitweb.michael.orlitzky.com - djbdns-logparse.git/blob - djbdns/io.py
2 Functions that perform input/output. This forms a layer between the
3 executable itself and the more libraryish modules.
5 from subprocess
import Popen
, PIPE
6 from typing
import TextIO
7 from djbdns
.dnscache
import handle_dnscache_log
8 from djbdns
.tinydns
import handle_tinydns_log
10 def parse_logfile(file : TextIO
):
12 Process a single log ``file``.
18 An open log file, or stdin.
23 >>> line = "@4000000063227a320c4f3114 7f000001:9d61:be69 - 0001 www.example.com\n"
24 >>> from tempfile import NamedTemporaryFile
25 >>> with NamedTemporaryFile(mode="w", delete=False) as f:
27 >>> from os import environ
28 >>> environ["TZ"] = "UTC+4"
29 >>> f = open(f.name, 'r')
31 2022-09-14 21:04:40.206516500 dropped query (no authority) from 127.0.0.1:40289 (id 48745): a www.example.com
33 >>> from os import remove
37 # Open a pipe to tai64nlocal. We'll write lines of our input file
38 # (the log file) to it, and read back the same lines but with
39 # friendly timestamps in them.
40 with Popen(["tai64nlocal"],
46 if not tai
.stdin
or not tai
.stdout
:
47 # Mypy tells me that this can happen, based on the type
48 # annotations in the standard library I guess?
53 line
= tai
.stdout
.readline()
55 friendly_line
= handle_tinydns_log(line
)
57 friendly_line
= handle_dnscache_log(line
)
63 except BrokenPipeError
:
64 # If our stdout is being piped to another process and if
65 # that process closes the pipe, this error will be raised
66 # the next time we try to write to stdout.