Log.sys.mjs (2858B)
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 import { Log as StdLog } from "resource://gre/modules/Log.sys.mjs"; 6 7 const PREF_REMOTE_LOG_LEVEL = "remote.log.level"; 8 9 const lazy = {}; 10 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) 16 ); 17 18 /** E10s compatible wrapper for the standard logger from Log.sys.mjs. */ 19 export class Log { 20 static TYPES = { 21 MARIONETTE: "Marionette", 22 REMOTE_AGENT: "RemoteAgent", 23 WEBDRIVER_BIDI: "WebDriver BiDi", 24 }; 25 26 /** 27 * Get a logger instance. For each provided type, a dedicated logger instance 28 * will be returned, but all loggers are relying on the same preference. 29 * 30 * @param {string} type 31 * The type of logger to use. Protocol-specific modules should use the 32 * corresponding logger type. Eg. files under /marionette should use 33 * Log.TYPES.MARIONETTE. 34 */ 35 static get(type = Log.TYPES.REMOTE_AGENT) { 36 const logger = StdLog.repository.getLogger(type); 37 if (!logger.ownAppenders.length) { 38 logger.addAppender(new StdLog.DumpAppender()); 39 logger.manageLevelFromPref(PREF_REMOTE_LOG_LEVEL); 40 } 41 return logger; 42 } 43 44 /** 45 * Check if the current log level matches the Debug log level, or any level 46 * above that. This should be used to guard logger.debug calls and avoid 47 * instantiating logger instances unnecessarily. 48 */ 49 static get isDebugLevelOrMore() { 50 // Debug is assigned 20, more verbose log levels have lower values. 51 return StdLog.Level[lazy.logLevel] <= StdLog.Level.Debug; 52 } 53 54 /** 55 * Check if the current log level matches the Trace log level, or any level 56 * above that. This should be used to guard logger.trace calls and avoid 57 * instantiating logger instances unnecessarily. 58 */ 59 static get isTraceLevelOrMore() { 60 // Trace is assigned 10, more verbose log levels have lower values. 61 return StdLog.Level[lazy.logLevel] <= StdLog.Level.Trace; 62 } 63 64 /** 65 * WARNING: This helper is incorrectly implemented and probably doesn't do 66 * what you would expect. 67 * 68 * At the moment `verbose` will be true for the least verbose log levels: 69 * INFO, WARN, ERROR and FATAL. Fixing the issue would lead to too much 70 * additional log spam on CI so we will need to use another approach, and 71 * probably to decouple it from the log level. 72 * 73 * See https://bugzilla.mozilla.org/show_bug.cgi?id=1828395 74 */ 75 static get verbose() { 76 return StdLog.Level[lazy.logLevel] >= StdLog.Level.Info; 77 } 78 }