# tinydns can drop a query for one of three reasons; this dictionary
# maps the symbol that gets logged in each case to a human-readable
-# reason.
+# reason. We include the "+" case here, indicating that the query was
+# NOT dropped, to avoid a special case later on when we're formatting
+# the human-readable output.
query_drop_reason = {
+ "+": None,
"-": "no authority",
"I": "invalid query",
"C": "invalid class",
+ "/": "couldn't parse"
}
print(timestamp, end=' ')
+ reason = query_drop_reason[code]
if code == "+":
- print ("sent response to %s:%s (id %s): %s %s"
- % (ip, port, id, type, name))
- elif code in ("-", "I", "C"):
- reason = query_drop_reason[code]
- print ("dropped query (%s) from %s:%s (id %s): %s %s"
- % (reason, ip, port, id, type, name))
- elif code == "/":
- print ("dropped query (couldn't parse) from %s:%s"
- % (ip, port))
+ line_tpl = "sent response to {ip}:{port} (id {id}): {type} {name}"
else:
- print ("%s from %s:%s (id %s): %s %s"
- % (code, ip, port, id, type, name))
+ line_tpl = "dropped query ({reason}) from {ip}:{port}"
+ if code != "/":
+ # If the query can actually be parsed, the log line is a
+ # bit more informative than it would have been otherwise.
+ line_tpl += " (id {id}): {type} {name}"
+
+ print(line_tpl.format(reason=reason,
+ ip=ip,
+ port=port,
+ id=id,
+ type=type,
+ name=name))
def parse_logfile(file : typing.TextIO):