diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
commit | 9e3c08db40b8916968b9f30096c7be3f00ce9647 (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /remote/shared/Log.sys.mjs | |
parent | Initial commit. (diff) | |
download | thunderbird-9e3c08db40b8916968b9f30096c7be3f00ce9647.tar.xz thunderbird-9e3c08db40b8916968b9f30096c7be3f00ce9647.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | remote/shared/Log.sys.mjs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/remote/shared/Log.sys.mjs b/remote/shared/Log.sys.mjs new file mode 100644 index 0000000000..e79e9f640f --- /dev/null +++ b/remote/shared/Log.sys.mjs @@ -0,0 +1,73 @@ +/* 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 { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; + +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. +XPCOMUtils.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; + } +} |