summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/Deprecated.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /toolkit/modules/Deprecated.sys.mjs
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.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 'toolkit/modules/Deprecated.sys.mjs')
-rw-r--r--toolkit/modules/Deprecated.sys.mjs81
1 files changed, 81 insertions, 0 deletions
diff --git a/toolkit/modules/Deprecated.sys.mjs b/toolkit/modules/Deprecated.sys.mjs
new file mode 100644
index 0000000000..c8f5aeba01
--- /dev/null
+++ b/toolkit/modules/Deprecated.sys.mjs
@@ -0,0 +1,81 @@
+/* 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/. */
+
+const PREF_DEPRECATION_WARNINGS = "devtools.errorconsole.deprecation_warnings";
+
+// A flag that indicates whether deprecation warnings should be logged.
+var logWarnings = Services.prefs.getBoolPref(PREF_DEPRECATION_WARNINGS);
+
+Services.prefs.addObserver(
+ PREF_DEPRECATION_WARNINGS,
+ function (aSubject, aTopic, aData) {
+ logWarnings = Services.prefs.getBoolPref(PREF_DEPRECATION_WARNINGS);
+ }
+);
+
+/**
+ * Build a callstack log message.
+ *
+ * @param nsIStackFrame aStack
+ * A callstack to be converted into a string log message.
+ */
+function stringifyCallstack(aStack) {
+ // If aStack is invalid, use Components.stack (ignoring the last frame).
+ if (!aStack || !(aStack instanceof Ci.nsIStackFrame)) {
+ aStack = Components.stack.caller;
+ }
+
+ let frame = aStack.caller;
+ let msg = "";
+ // Get every frame in the callstack.
+ while (frame) {
+ if (frame.filename || frame.lineNumber || frame.name) {
+ msg += frame.filename + " " + frame.lineNumber + " " + frame.name + "\n";
+ }
+ frame = frame.caller;
+ }
+ return msg;
+}
+
+export var Deprecated = {
+ /**
+ * Log a deprecation warning.
+ *
+ * @param string aText
+ * Deprecation warning text.
+ * @param string aUrl
+ * A URL pointing to documentation describing deprecation
+ * and the way to address it.
+ * @param nsIStackFrame aStack
+ * An optional callstack. If it is not provided a
+ * snapshot of the current JavaScript callstack will be
+ * logged.
+ */
+ warning(aText, aUrl, aStack) {
+ if (!logWarnings) {
+ return;
+ }
+
+ // If URL is not provided, report an error.
+ if (!aUrl) {
+ console.error(
+ "Error in Deprecated.warning: warnings must " +
+ "provide a URL documenting this deprecation."
+ );
+ return;
+ }
+
+ let textMessage =
+ "DEPRECATION WARNING: " +
+ aText +
+ "\nYou may find more details about this deprecation at: " +
+ aUrl +
+ "\n" +
+ // Append a callstack part to the deprecation message.
+ stringifyCallstack(aStack);
+
+ // Report deprecation warning.
+ console.error(textMessage);
+ },
+};