| 111 | * @private Visible for testing only. |
| 112 | */ |
| 113 | export class Log { |
| 114 | /** |
| 115 | * opt_suffix will be appended to error message to identify the type of the |
| 116 | * error message. We can't rely on the error object to pass along the type |
| 117 | * because some browsers do not have this param in its window.onerror API. |
| 118 | * See: |
| 119 | * https://blog.sentry.io/2016/01/04/client-javascript-reporting-window-onerror.html |
| 120 | * |
| 121 | * @param {!Window} win |
| 122 | * @param {function(number, boolean):!LogLevel_Enum} levelFunc |
| 123 | * @param {string=} opt_suffix |
| 124 | */ |
| 125 | constructor(win, levelFunc, opt_suffix = '') { |
| 126 | /** |
| 127 | * In tests we use the main test window instead of the iframe where |
| 128 | * the tests runs because only the former is relayed to the console. |
| 129 | * @const {!Window} |
| 130 | */ |
| 131 | this.win = getMode().test && win.__AMP_TEST_IFRAME ? win.parent : win; |
| 132 | |
| 133 | /** @private @const {function(number, boolean):!LogLevel_Enum} */ |
| 134 | this.levelFunc_ = levelFunc; |
| 135 | |
| 136 | /** @private @const {!LogLevel_Enum} */ |
| 137 | this.level_ = this.defaultLevel_(); |
| 138 | |
| 139 | /** @private @const {string} */ |
| 140 | this.suffix_ = opt_suffix; |
| 141 | |
| 142 | /** @private {?JsonObject} */ |
| 143 | this.messages_ = null; |
| 144 | |
| 145 | this.fetchExternalMessagesOnce_ = once(() => { |
| 146 | win |
| 147 | .fetch(externalMessagesSimpleTableUrl()) |
| 148 | .then((response) => response.json(), noop) |
| 149 | .then((opt_messages) => { |
| 150 | if (opt_messages) { |
| 151 | this.messages_ = /** @type {!JsonObject} */ (opt_messages); |
| 152 | } |
| 153 | }); |
| 154 | }); |
| 155 | |
| 156 | // This bound assertion function is capable of handling the format used when |
| 157 | // error/assertion messages are extracted. This logic hasn't yet been |
| 158 | // migrated to an AMP-independent form for use in core. This binding allows |
| 159 | // Log assertion helpers to maintain message-extraction capabilities until |
| 160 | // that logic can be moved to core. |
| 161 | this.boundAssertFn_ = /** @type {!assertions.AssertionFunctionDef} */ ( |
| 162 | this.assert.bind(this) |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @return {!LogLevel_Enum} |
| 168 | * @private |
| 169 | */ |
| 170 | defaultLevel_() { |
nothing calls this directly
no outgoing calls
no test coverage detected