summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/Deprecated.jsm
blob: 981cd13ab0c97ec23c0cbfed0490057e0a83a37f (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* 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/. */

"use strict";

this.EXPORTED_SYMBOLS = [ "Deprecated" ];

const Cu = Components.utils;
const Ci = Components.interfaces;
const PREF_DEPRECATION_WARNINGS = "devtools.errorconsole.deprecation_warnings";
const PREF_PERFORMANCE_WARNINGS = "devtools.errorconsole.performance_warnings";

Cu.import("resource://gre/modules/Services.jsm");

// A flag that indicates whether deprecation warnings should be logged.
var logDepWarnings = Services.prefs.getBoolPref(PREF_DEPRECATION_WARNINGS);
var logPerfWarnings = Services.prefs.getBoolPref(PREF_PERFORMANCE_WARNINGS);

Services.prefs.addObserver(PREF_DEPRECATION_WARNINGS,
  function (aSubject, aTopic, aData) {
    logDepWarnings = Services.prefs.getBoolPref(PREF_DEPRECATION_WARNINGS);
  }, false);
Services.prefs.addObserver(PREF_PERFORMANCE_WARNINGS,
  function (aSubject, aTopic, aData) {
    logPerfWarnings = Services.prefs.getBoolPref(PREF_PERFORMANCE_WARNINGS);
  }, false);

/**
 * 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) {
    msg += frame.filename + " " + frame.lineNumber +
      " " + frame.name + "\n";
    frame = frame.caller;
  }
  return msg;
}

this.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: function (aText, aUrl, aStack) {
    if (!logDepWarnings) {
      return;
    }

    // If URL is not provided, report an error.
    if (!aUrl) {
      Cu.reportError("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 + "\nCallstack:\n" +
      // Append a callstack part to the deprecation message.
      stringifyCallstack(aStack);

    // Report deprecation warning.
    Cu.reportError(textMessage);
  },

  /**
   * Log a performance warning.
   *
   * @param string aText
   *        Performance issue warning text.
   * @param string aUrl
   *        A URL pointing to documentation describing performance
   *        issue 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.
   */
  perfWarning: function (aText, aUrl, aStack) {
    if (!logPerfWarnings) {
      return;
    }

    // If URL is not provided, report an error.
    if (!aUrl) {
      Cu.reportError("Error in Deprecated.perfWarning: warnings must " +
        "provide a URL documenting this performance issue.");
      return;
    }

    let textMessage = "PERFORMANCE WARNING: " + aText +
      "\nYou may find more details about this problem at: " +
      aUrl + "\nCallstack:\n" +
      // Append a callstack part to the deprecation message.
      stringifyCallstack(aStack);

    // Report deprecation warning.
    Cu.reportError(textMessage);
  }
};