summaryrefslogtreecommitdiffstats
path: root/js/src/tests/lib/structuredlog.py
blob: b00914cc90d8b5918442aec75b8d87f89fc2bd9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# produce mozlog-compatible log messages, following the spec at
# https://firefox-source-docs.mozilla.org/mozbase/mozlog.html

import json
import os
from time import time


class TestLogger(object):
    def __init__(self, source, threadname="main"):
        self.template = {
            "source": source,
            "thread": threadname,
            "pid": os.getpid(),
        }
        directory = os.environ.get("MOZ_UPLOAD_DIR", ".")
        self.fh = open(os.path.join(directory, threadname + "_raw.log"), "a")

    def _record(self, **kwargs):
        record = self.template.copy()
        record.update(**kwargs)
        if "time" not in record:
            record["time"] = time()
        return record

    def _log_obj(self, obj):
        print(json.dumps(obj, sort_keys=True), file=self.fh)

    def _log(self, **kwargs):
        self._log_obj(self._record(**kwargs))

    def suite_start(self):
        self._log(action="suite_start", tests=[])

    def suite_end(self):
        self._log(action="suite_end")

    def test_start(self, testname):
        self._log(action="test_start", test=testname)

    def test_end(self, testname, status):
        self._log(action="test_end", test=testname, status=status)

    def test(self, testname, status, duration, **details):
        record = self._record(
            action="test_start", test=testname, **details.get("extra", {})
        )
        end_time = record["time"]
        record["time"] -= duration
        self._log_obj(record)

        record["action"] = "test_end"
        record["time"] = end_time
        record["status"] = status
        record.update(**details)
        self._log_obj(record)

    def log_info(self, message):
        self._log(action="log", level="INFO", message=message)