Bug 1021117 - Add legacy tbpl formatter to mozlog.structured.formatters. r=jgraham
authorChris Manchester <cmanchester@mozilla.com>
Fri, 06 Jun 2014 11:36:28 -0400
changeset 206546 d7c75329ae849e1314dbd22690c093706ac88443
parent 206545 262aa76f4bd2d7d61d20b29c1fd1b87a10e3ab09
child 206547 7b5f9225d255cc67c715f62116e2c08db2f2e8f4
push id3741
push userasasaki@mozilla.com
push dateMon, 21 Jul 2014 20:25:18 +0000
treeherdermozilla-beta@4d6f46f5af68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjgraham
bugs1021117
milestone32.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1021117 - Add legacy tbpl formatter to mozlog.structured.formatters. r=jgraham
testing/mozbase/mozlog/mozlog/structured/commandline.py
testing/mozbase/mozlog/mozlog/structured/formatters/__init__.py
testing/mozbase/mozlog/mozlog/structured/formatters/tbplformatter.py
--- a/testing/mozbase/mozlog/mozlog/structured/commandline.py
+++ b/testing/mozbase/mozlog/mozlog/structured/commandline.py
@@ -11,16 +11,17 @@ import formatters
 
 log_formatters = {
     'raw': (formatters.JSONFormatter, "Raw structured log messages"),
     'unittest': (formatters.UnittestFormatter, "Unittest style output"),
     'xunit': (formatters.XUnitFormatter, "xUnit compatible XML"),
     'html': (formatters.HTMLFormatter, "HTML report"),
     'mach': (formatters.MachFormatter, "Uncolored mach-like output"),
     'mach_terminal': (formatters.MachTerminalFormatter, "Colored mach-like output for use in a tty"),
+    'tbpl': (formatters.TbplFormatter, "TBPL style log format"),
 }
 
 
 def log_file(name):
     if name == "-":
         return sys.stdout
     else:
         return open(name, "w")
--- a/testing/mozbase/mozlog/mozlog/structured/formatters/__init__.py
+++ b/testing/mozbase/mozlog/mozlog/structured/formatters/__init__.py
@@ -2,11 +2,12 @@
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 import json
 from unittest import UnittestFormatter
 from xunit import XUnitFormatter
 from html import HTMLFormatter
 from machformatter import MachFormatter, MachTerminalFormatter
+from tbplformatter import TbplFormatter
 
 def JSONFormatter():
     return lambda x: json.dumps(x) + "\n"
new file mode 100644
--- /dev/null
+++ b/testing/mozbase/mozlog/mozlog/structured/formatters/tbplformatter.py
@@ -0,0 +1,71 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from machformatter import BaseMachFormatter
+
+class TbplFormatter(BaseMachFormatter):
+    """Formatter that formats logs in the legacy formatting format used by TBPL
+    This is intended to be used to preserve backward compatibility with existing tools
+    hand-parsing this format.
+    """
+    def __init__(self):
+        self.suite_start_time = None
+        self.test_start_times = {}
+
+    def __call__(self, data):
+        return getattr(self, data["action"])(data)
+
+    def log(self, data):
+        return "%s\n" % (data["message"])
+
+    def process_output(self, data):
+        return "PROCESS | %(process)s | %(data)s\n" % data
+
+    def suite_start(self, data):
+        self.suite_start_time = data["time"]
+        return "SUITE-START | Running %i tests\n" % len(data["tests"])
+
+    def test_start(self, data):
+        self.test_start_times[self.test_id(data["test"])] = data["time"]
+
+        return "TEST-START | %s\n" % self.id_str(data["test"])
+
+    def test_status(self, data):
+        if "expected" in data:
+            return "TEST-UNEXPECTED-%s | %s | %s | expected %s | %s\n" % (
+                data["status"], self.id_str(data["test"]), data["subtest"], data["expected"],
+                data.get("message", ""))
+        else:
+            return "TEST-%s | %s | %s | %s\n" % (
+                data["status"], self.id_str(data["test"]), data["subtest"], data.get("message", ""))
+
+    def test_end(self, data):
+        start_time = self.test_start_times.pop(self.test_id(data["test"]))
+        time = data["time"] - start_time
+
+        if "expected" in data:
+            return "TEST-END UNEXPECTED-%s | %s | expected %s | %s | took %ims\n" % (
+                data["status"], self.id_str(data["test"]), data["expected"],
+                data.get("message", ""), time)
+        else:
+            return "TEST-END %s | %s | took %ims\n" % (
+                data["status"], self.id_str(data["test"]), time)
+
+    def suite_end(self, data):
+        start_time = self.suite_start_time
+        time = int((data["time"] - start_time) / 1000)
+
+        return "SUITE-END | took %is\n" % time
+
+    def test_id(self, test_id):
+        if isinstance(test_id, (str, unicode)):
+            return test_id
+        else:
+            return tuple(test_id)
+
+    def id_str(self, test_id):
+        if isinstance(test_id, (str, unicode)):
+            return test_id
+        else:
+            return " ".join(test_id)