tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_console_to_mozlog.js (2382B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 */
      5 
      6 /* Use console API to log via MOZ_LOG to stdout/file/profiler */
      7 
      8 // Use background task in order to control MOZ_LOG env variable passed to another gecko run
      9 const { BackgroundTasksTestUtils } = ChromeUtils.importESModule(
     10  "resource://testing-common/BackgroundTasksTestUtils.sys.mjs"
     11 );
     12 BackgroundTasksTestUtils.init(this);
     13 const do_backgroundtask = BackgroundTasksTestUtils.do_backgroundtask.bind(
     14  BackgroundTasksTestUtils
     15 );
     16 
     17 add_task(async function test_console_to_mozlog() {
     18  const lines = [];
     19  const promise = do_backgroundtask("console", {
     20    onStdoutLine: (line, _proc) => {
     21      lines.push(line);
     22    },
     23    extraEnv: {
     24      MOZ_LOG: "console:5,my-prefix:2,PageMessages:5",
     25    },
     26  });
     27  const exitCode = await promise;
     28  is(exitCode, 0);
     29 
     30  const pidLine = lines.find(line => line.includes("CONSOLE-PID"));
     31  ok(pidLine, "Found the line where the parent process PID is logged");
     32  const [, pid] = pidLine.split(":");
     33  ok(pid, "Got the pid out of the PID line");
     34 
     35  // Each MOZ_LOG / console api call starts with a description of the process and thread where it is logged
     36  const threadPrefix = `[Parent ${pid}: Main Thread]: `;
     37 
     38  const expectedLogs = [
     39    // MOZ_LOG=console:5
     40    `I/console log: "foo"`,
     41    `D/console debug: "bar"`,
     42 
     43    // MOZ_LOG=my-prefix:2
     44    // Bug 1923985: For now, the console API level isn't synchronized with MOZ_LOG one.
     45    // shouldLogLog should be false because of my-prefix set to level 2.
     46    `E/my-prefix error: ({shouldLogError:true, shouldLogLog:true})`,
     47    `W/my-prefix warn: "warning"`,
     48 
     49    // MOZ_LOG=PageMessages:5
     50    `I/PageMessages String message`,
     51    `E/PageMessages [JavaScript Error: "Error: Async exception" {file: "resource://testing-common/backgroundtasks/BackgroundTask_console.sys.mjs" line: 28}]`,
     52  ];
     53 
     54  for (const expected of expectedLogs) {
     55    ok(
     56      lines.some(line => line.includes(`${threadPrefix}${expected}`)),
     57      `Found ${expected}`
     58    );
     59  }
     60 
     61  // The console.log call with my-prefix isn't logged because of log level set to "2" for my-prefix
     62  ok(
     63    !lines.some(line => line.includes("not-logged")),
     64    "Logs blocked by too verbose level aren't visible in stdout"
     65  );
     66 });