blob: cfd8e295ec23e3565f60ba126d6e09cb9fc60df2 [file] [log] [blame]
Itamar Turner-Trauring8a56aef2015-08-18 15:58:32 -04001from __future__ import unicode_literals
2
3import json
4import sys
5from datetime import datetime
6
7
Itamar Turner-Trauringee34a7e2015-08-18 16:10:58 -04008def pretty_print(message, separator=True):
Itamar Turner-Trauring8a56aef2015-08-18 15:58:32 -04009 """
10 Print human-readable messages to stdout.
11 """
12 skip = {"timestamp", "task_uuid", "task_level", "action_counter",
13 "message_type", "action_type", "action_status"}
14
15 def add_field(previous, key, value):
16 return " %s: %s\n" % (key, str(value).strip("\n"))
17
18 remaining = ""
19 for field in ["action_type", "message_type", "action_status"]:
20 if field in message:
21 remaining += add_field(remaining, field, message[field])
22 for (key, value) in message.items():
23 if key not in skip:
24 remaining += add_field(remaining, key, value)
25
26 level = "/" + "/".join(map(str, message["task_level"]))
Itamar Turner-Trauringee34a7e2015-08-18 16:10:58 -040027 output = "%s%s %sZ\n%s" % (
Itamar Turner-Trauring8a56aef2015-08-18 15:58:32 -040028 message["task_uuid"],
29 level,
30 datetime.utcfromtimestamp(message["timestamp"]).time().isoformat(),
31 remaining,
32 )
Itamar Turner-Trauringee34a7e2015-08-18 16:10:58 -040033 if separator:
34 output += "\n"
Itamar Turner-Trauring8a56aef2015-08-18 15:58:32 -040035 sys.stdout.write(output)
36
37
38def main():
Itamar Turner-Trauringee34a7e2015-08-18 16:10:58 -040039 cached_start_messages = {}
40
Itamar Turner-Trauring8a56aef2015-08-18 15:58:32 -040041 for line in sys.stdin:
42 try:
43 message = json.loads(line)
44 except ValueError:
45 # Stupid systemd/journald corrupted the log message
46 continue
Itamar Turner-Trauringee34a7e2015-08-18 16:10:58 -040047 if message.get("message_type") == "eliot:traceback":
Itamar Turner-Trauring8a56aef2015-08-18 15:58:32 -040048 pretty_print(message)
Itamar Turner-Trauringee34a7e2015-08-18 16:10:58 -040049 elif message.get("action_type"):
50 action_type = message["action_type"]
51 task_uuid = message["task_uuid"]
52 task_level = tuple(message["task_level"][:-1])
53 status = message["action_status"]
54 key = (action_type, task_uuid, task_level)
55 if status == "started":
56 cached_start_messages[key] = message
57 else:
58 try:
59 start_message = cached_start_messages.pop(key)
60 except KeyError:
61 start_message = None
62 if status == "failed":
63 if start_message:
64 pretty_print(start_message, separator=False)
65 pretty_print(message)
Itamar Turner-Trauring8a56aef2015-08-18 15:58:32 -040066
67
68main()
69