summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/SimpleTest/MozillaLogger.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /testing/mochitest/tests/SimpleTest/MozillaLogger.js
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/mochitest/tests/SimpleTest/MozillaLogger.js')
-rw-r--r--testing/mochitest/tests/SimpleTest/MozillaLogger.js105
1 files changed, 105 insertions, 0 deletions
diff --git a/testing/mochitest/tests/SimpleTest/MozillaLogger.js b/testing/mochitest/tests/SimpleTest/MozillaLogger.js
new file mode 100644
index 0000000000..c184b48cf1
--- /dev/null
+++ b/testing/mochitest/tests/SimpleTest/MozillaLogger.js
@@ -0,0 +1,105 @@
+/**
+ * MozillaLogger, a base class logger that just logs to stdout.
+ */
+
+"use strict";
+
+function formatLogMessage(msg) {
+ return msg.info.join(" ") + "\n";
+}
+
+function importJSM(jsm) {
+ if (typeof ChromeUtils === "object") {
+ return ChromeUtils.import(jsm);
+ }
+ /* globals SpecialPowers */
+ let obj = {};
+ SpecialPowers.Cu.import(jsm, obj);
+ return SpecialPowers.wrap(obj);
+}
+
+// When running in release builds, we get a fake Components object in
+// web contexts, so we need to check that the Components object is sane,
+// too, not just that it exists.
+let haveComponents =
+ typeof Components === "object" &&
+ typeof Components.Constructor === "function";
+
+let CC = (haveComponents
+ ? Components
+ : SpecialPowers.wrap(SpecialPowers.Components)
+).Constructor;
+
+let ConverterOutputStream = CC(
+ "@mozilla.org/intl/converter-output-stream;1",
+ "nsIConverterOutputStream",
+ "init"
+);
+
+class MozillaLogger {
+ get logCallback() {
+ return msg => {
+ this.log(formatLogMessage(msg));
+ };
+ }
+
+ log(msg) {
+ dump(msg);
+ }
+
+ close() {}
+}
+
+/**
+ * MozillaFileLogger, a log listener that can write to a local file.
+ * intended to be run from chrome space
+ */
+
+/** Init the file logger with the absolute path to the file.
+ It will create and append if the file already exists **/
+class MozillaFileLogger extends MozillaLogger {
+ constructor(aPath) {
+ super();
+
+ const { FileUtils } = importJSM("resource://gre/modules/FileUtils.jsm");
+
+ this._file = FileUtils.File(aPath);
+ this._foStream = FileUtils.openFileOutputStream(
+ this._file,
+ FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_APPEND
+ );
+
+ this._converter = ConverterOutputStream(this._foStream, "UTF-8");
+ }
+
+ get logCallback() {
+ return msg => {
+ if (this._converter) {
+ var data = formatLogMessage(msg);
+ this.log(data);
+
+ if (data.includes("SimpleTest FINISH")) {
+ this.close();
+ }
+ }
+ };
+ }
+
+ log(msg) {
+ if (this._converter) {
+ this._converter.writeString(msg);
+ }
+ }
+
+ close() {
+ this._converter.flush();
+ this._converter.close();
+
+ this._foStream = null;
+ this._converter = null;
+ this._file = null;
+ }
+}
+
+this.MozillaLogger = MozillaLogger;
+this.MozillaFileLogger = MozillaFileLogger;