Bug 1869043 add a main thread record of track audio outputs r=padenot
[gecko.git] / remote / shared / Log.sys.mjs
blobf1b3706391ecd352ce7e23f6ebe3eff4720da1da
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/. */
5 import { Log as StdLog } from "resource://gre/modules/Log.sys.mjs";
7 const PREF_REMOTE_LOG_LEVEL = "remote.log.level";
9 const lazy = {};
11 // Lazy getter which returns a cached value of the remote log level. Should be
12 // used for static getters used to guard hot paths for logging, eg
13 // isTraceLevelOrMore.
14 ChromeUtils.defineLazyGetter(lazy, "logLevel", () =>
15   Services.prefs.getCharPref(PREF_REMOTE_LOG_LEVEL, StdLog.Level.Fatal)
18 /** E10s compatible wrapper for the standard logger from Log.sys.mjs. */
19 export class Log {
20   static TYPES = {
21     CDP: "CDP",
22     MARIONETTE: "Marionette",
23     REMOTE_AGENT: "RemoteAgent",
24     WEBDRIVER_BIDI: "WebDriver BiDi",
25   };
27   /**
28    * Get a logger instance. For each provided type, a dedicated logger instance
29    * will be returned, but all loggers are relying on the same preference.
30    *
31    * @param {string} type
32    *     The type of logger to use. Protocol-specific modules should use the
33    *     corresponding logger type. Eg. files under /marionette should use
34    *     Log.TYPES.MARIONETTE.
35    */
36   static get(type = Log.TYPES.REMOTE_AGENT) {
37     const logger = StdLog.repository.getLogger(type);
38     if (!logger.ownAppenders.length) {
39       logger.addAppender(new StdLog.DumpAppender());
40       logger.manageLevelFromPref(PREF_REMOTE_LOG_LEVEL);
41     }
42     return logger;
43   }
45   /**
46    * Check if the current log level matches the Debug log level, or any level
47    * above that. This should be used to guard logger.debug calls and avoid
48    * instanciating logger instances unnecessarily.
49    */
50   static get isDebugLevelOrMore() {
51     // Debug is assigned 20, more verbose log levels have lower values.
52     return StdLog.Level[lazy.logLevel] <= StdLog.Level.Debug;
53   }
55   /**
56    * Check if the current log level matches the Trace log level, or any level
57    * above that. This should be used to guard logger.trace calls and avoid
58    * instanciating logger instances unnecessarily.
59    */
60   static get isTraceLevelOrMore() {
61     // Trace is assigned 10, more verbose log levels have lower values.
62     return StdLog.Level[lazy.logLevel] <= StdLog.Level.Trace;
63   }
65   static get verbose() {
66     // we can't use Preferences.sys.mjs before first paint,
67     // see ../browser/base/content/test/performance/browser_startup.js
68     const level = Services.prefs.getStringPref(PREF_REMOTE_LOG_LEVEL, "Info");
69     return StdLog.Level[level] >= StdLog.Level.Info;
70   }