Commit f2219b1a authored by Raine Makelainen's avatar Raine Makelainen

[embedlite-components] Pass through JS console.* messages. Fixes JB#51251

This commit obsoletes EMBEDLITE_COMPONENTS_LOGGING. Logging can
be enabled by following means:

- EMBED_CONSOLE=1
- EMBED_CONSOLE=stacktrace (also tracktraces of error)
- embedlite.console_log.enabled preference to true (same as EMBED_CONSOLE=1)
Signed-off-by: default avatarRaine Makelainen <raine.makelainen@jolla.com>
parent 87eb5cf9
/* 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/. */
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2020 Open Mobile Platform LLC.
*/
const Cc = Components.classes;
const Ci = Components.interfaces;
......@@ -17,8 +20,7 @@ Services.scriptloader.loadSubScript("chrome://embedlite/content/Logger.js");
// Common helper service
function SPConsoleListener(dumpStdOut) {
this._dumpToStdOut = dumpStdOut;
function SPConsoleListener() {
this._cacheLogs = true;
Logger.debug("JSComp: EmbedLiteConsoleListener.js loaded");
......@@ -27,10 +29,10 @@ function SPConsoleListener(dumpStdOut) {
SPConsoleListener.prototype = {
_cacheLogs: true,
_startupCachedLogs: [],
_dumpToStdOut: false,
observe: function(msg) {
if (this._dumpToStdOut) {
dump("CONSOLE: " + JSON.stringify(msg) + "\n");
if (Logger.enabled) {
Logger.debug("CONSOLE message:");
Logger.debug(msg);
} else {
if (this._cacheLogs) {
this._startupCachedLogs.push(msg);
......@@ -61,28 +63,26 @@ function EmbedLiteConsoleListener()
EmbedLiteConsoleListener.prototype = {
classID: Components.ID("{6b21b5a8-9816-11e2-86f8-fb54170a814d}"),
_enabled: false,
_listener: null,
formatStackFrame: function(aFrame) {
let functionName = aFrame.functionName || '<anonymous>';
return ' at ' + functionName +
' (' + aFrame.filename + ':' + aFrame.lineNumber +
':' + aFrame.columnNumber + ')';
},
observe: function (aSubject, aTopic, aData) {
switch(aTopic) {
// Engine DownloadManager notifications
case "app-startup": {
let dumpToStdOut = false;
var runConsoleEnv = 0;
try {
runConsoleEnv = Services.env.get('EMBED_CONSOLE');
dumpToStdOut = runConsoleEnv == 1;
} catch (e) {}
var runConsolePref = 0;
try {
runConsolePref = Services.prefs.getIntPref("embedlite.console_log.enabled");
dumpToStdOut = runConsolePref == 1;
} catch (e) {/*pref is missing*/ }
if (runConsolePref > 0 || runConsoleEnv > 0) {
this._listener = new SPConsoleListener(dumpToStdOut);
if (Logger.stackTraceEnabled)
Services.obs.addObserver(this, 'console-api-log-event', false);
if (Logger.enabled) {
this._listener = new SPConsoleListener();
Services.console.registerListener(this._listener);
this._enabled = true;
Services.obs.addObserver(this, "embedui:logger", true);
}
break;
......@@ -90,17 +90,34 @@ EmbedLiteConsoleListener.prototype = {
case "embedui:logger": {
var data = JSON.parse(aData);
if (data.enabled) {
if (this._enabled) {
if (Logger.enabled) {
this._listener.flushCache();
} else {
Services.console.registerListener(this._listener);
}
} else if (!data.enabled && this._enabled) {
} else if (!data.enabled && Logger.enabled) {
Services.console.unregisterListener(this._listener);
this._listener.clearCache();
}
break;
}
case "console-api-log-event": {
let message = aSubject.wrappedJSObject;
let args = message.arguments;
let stackTrace = '';
if (message.stacktrace &&
(message.level == 'assert' || message.level == 'error' || message.level == 'trace')) {
stackTrace = Array.map(message.stacktrace, this.formatStackFrame).join('\n');
} else {
stackTrace = this.formatStackFrame(message);
}
args.push('\n' + stackTrace);
Logger.debug("Content JS:", message.filename, "function:", message.functionName, "message:", args.join(" "));
break;
}
}
},
......
......@@ -5,39 +5,38 @@
* Copyright (c) 2020 Open Mobile Platform LLC.
*/
Components.utils.import("resource://gre/modules/Services.jsm");
let Logger = {
enabled: false,
getenv: function (name) {
_enabled: false,
_consoleEnv: null,
init: function doInit() {
try {
this._consoleEnv = Services.env.get("EMBED_CONSOLE");
} catch (e) {}
let consolePref = false;
try {
var environment = Components.classes["@mozilla.org/process/environment;1"].
getService(Components.interfaces.nsIEnvironment);
return environment.get(name);
} catch(e) {
this.debug("Logger.js getEnvironment:", e);
}
consolePref = Services.prefs.getIntPref("embedlite.console_log.enabled");
} catch (e) { /*pref is missing*/ }
this._enabled = this._consoleEnv || consolePref || false;
},
init: function doInit() {
this.enabled = this.getenv("EMBEDLITE_COMPONENTS_LOGGING") == 1 || false;
get stackTraceEnabled() {
return this._consoleEnv === "stacktrace";
},
/*
* Console printing utilities
*/
dumpf: function dumpf(str) {
let args = arguments;
let i = 1;
dump(str.replace(/%s/g, function() {
if (i >= args.length) {
throw "dumps received too many placeholders and not enough arguments";
}
return args[i++].toString();
}));
get enabled() {
return this._enabled;
},
/*
* Logger printing utilities
*/
debug: function() {
if (!this.enabled)
if (!this._enabled)
return;
var args = Array.prototype.slice.call(arguments);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment