summaryrefslogtreecommitdiffstats
path: root/remote/shared/Log.sys.mjs
blob: f1b3706391ecd352ce7e23f6ebe3eff4720da1da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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/. */

import { Log as StdLog } from "resource://gre/modules/Log.sys.mjs";

const PREF_REMOTE_LOG_LEVEL = "remote.log.level";

const lazy = {};

// Lazy getter which returns a cached value of the remote log level. Should be
// used for static getters used to guard hot paths for logging, eg
// isTraceLevelOrMore.
ChromeUtils.defineLazyGetter(lazy, "logLevel", () =>
  Services.prefs.getCharPref(PREF_REMOTE_LOG_LEVEL, StdLog.Level.Fatal)
);

/** E10s compatible wrapper for the standard logger from Log.sys.mjs. */
export class Log {
  static TYPES = {
    CDP: "CDP",
    MARIONETTE: "Marionette",
    REMOTE_AGENT: "RemoteAgent",
    WEBDRIVER_BIDI: "WebDriver BiDi",
  };

  /**
   * Get a logger instance. For each provided type, a dedicated logger instance
   * will be returned, but all loggers are relying on the same preference.
   *
   * @param {string} type
   *     The type of logger to use. Protocol-specific modules should use the
   *     corresponding logger type. Eg. files under /marionette should use
   *     Log.TYPES.MARIONETTE.
   */
  static get(type = Log.TYPES.REMOTE_AGENT) {
    const logger = StdLog.repository.getLogger(type);
    if (!logger.ownAppenders.length) {
      logger.addAppender(new StdLog.DumpAppender());
      logger.manageLevelFromPref(PREF_REMOTE_LOG_LEVEL);
    }
    return logger;
  }

  /**
   * Check if the current log level matches the Debug log level, or any level
   * above that. This should be used to guard logger.debug calls and avoid
   * instanciating logger instances unnecessarily.
   */
  static get isDebugLevelOrMore() {
    // Debug is assigned 20, more verbose log levels have lower values.
    return StdLog.Level[lazy.logLevel] <= StdLog.Level.Debug;
  }

  /**
   * Check if the current log level matches the Trace log level, or any level
   * above that. This should be used to guard logger.trace calls and avoid
   * instanciating logger instances unnecessarily.
   */
  static get isTraceLevelOrMore() {
    // Trace is assigned 10, more verbose log levels have lower values.
    return StdLog.Level[lazy.logLevel] <= StdLog.Level.Trace;
  }

  static get verbose() {
    // we can't use Preferences.sys.mjs before first paint,
    // see ../browser/base/content/test/performance/browser_startup.js
    const level = Services.prefs.getStringPref(PREF_REMOTE_LOG_LEVEL, "Info");
    return StdLog.Level[level] >= StdLog.Level.Info;
  }
}