-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathJsInterpreterLogger.js
More file actions
55 lines (49 loc) · 1.6 KB
/
Copy pathJsInterpreterLogger.js
File metadata and controls
55 lines (49 loc) · 1.6 KB
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
/** @file Observes a JSInterpreter and logs to the designated browser console. */
var Observer = require('./Observer');
/**
* Observer responsible for logging to the provided browser console when
* the interpreter it is observing raises log-worthy events.
* @constructor
* @implements LogTarget
* @param {Console} window console API
*/
var JsInterpreterLogger = (module.exports = function (outputConsole) {
/** @private {Console} */
this.outputConsole_ = outputConsole;
/** @private {Observer} */
this.observer_ = new Observer();
});
/**
* Attach the logger to a particular JSInterpreter instance.
* @param {JSInterpreter} jsInterpreter
*/
JsInterpreterLogger.prototype.attachTo = function (jsInterpreter) {
this.observer_.observe(jsInterpreter.onExecutionWarning, this.log.bind(this));
};
/**
* Detach the logger from whatever interpreter instance it is currently
* attached to, unregistering handlers.
* Safe to call when the logger is already detached.
*/
JsInterpreterLogger.prototype.detach = function () {
this.observer_.unobserveAll();
};
/**
* Log to the console object we were constructed with.
* @param {*} arguments...
* @see Console.log
*/
JsInterpreterLogger.prototype.log = function () {
if (!IN_UNIT_TEST && this.outputConsole_ && this.outputConsole_.log) {
this.outputConsole_.log.apply(this.outputConsole_, arguments);
}
};
/**
* Clear the console object we were constructed with
* @see Console.clear
*/
JsInterpreterLogger.prototype.clear = function () {
if (!IN_UNIT_TEST && this.outputConsole_ && this.outputConsole_.clear) {
this.outputConsole_.clear();
}
};