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";
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. */
22 MARIONETTE: "Marionette",
23 REMOTE_AGENT: "RemoteAgent",
24 WEBDRIVER_BIDI: "WebDriver BiDi",
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.
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.
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);
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.
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;
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.
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;
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;