2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2013-08-26 22:55:58 +04:00
|
|
|
|
2013-06-21 01:00:14 +04:00
|
|
|
"use strict";
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["Log"];
|
2007-12-15 05:07:25 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2019-01-04 11:41:49 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"Services",
|
|
|
|
"resource://gre/modules/Services.jsm"
|
|
|
|
);
|
2014-04-29 18:09:29 +04:00
|
|
|
const INTERNAL_FIELDS = new Set(["_level", "_message", "_time", "_namespace"]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump a message everywhere we can if we have a failure.
|
|
|
|
*/
|
|
|
|
function dumpError(text) {
|
|
|
|
dump(text + "\n");
|
|
|
|
Cu.reportError(text);
|
|
|
|
}
|
2011-06-13 22:39:37 +04:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var Log = {
|
2008-03-25 23:55:34 +03:00
|
|
|
Level: {
|
|
|
|
Fatal: 70,
|
|
|
|
Error: 60,
|
|
|
|
Warn: 50,
|
|
|
|
Info: 40,
|
|
|
|
Config: 30,
|
|
|
|
Debug: 20,
|
|
|
|
Trace: 10,
|
2014-09-24 08:38:34 +04:00
|
|
|
All: -1, // We don't want All to be falsy.
|
2008-03-25 23:55:34 +03:00
|
|
|
Desc: {
|
|
|
|
70: "FATAL",
|
|
|
|
60: "ERROR",
|
|
|
|
50: "WARN",
|
|
|
|
40: "INFO",
|
|
|
|
30: "CONFIG",
|
|
|
|
20: "DEBUG",
|
|
|
|
10: "TRACE",
|
2014-09-24 08:38:34 +04:00
|
|
|
"-1": "ALL",
|
2013-07-13 03:39:43 +04:00
|
|
|
},
|
|
|
|
Numbers: {
|
|
|
|
FATAL: 70,
|
|
|
|
ERROR: 60,
|
|
|
|
WARN: 50,
|
|
|
|
INFO: 40,
|
|
|
|
CONFIG: 30,
|
|
|
|
DEBUG: 20,
|
|
|
|
TRACE: 10,
|
2014-09-24 08:38:34 +04:00
|
|
|
ALL: -1,
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2008-03-25 23:55:34 +03:00
|
|
|
},
|
|
|
|
|
2008-11-04 01:48:53 +03:00
|
|
|
get repository() {
|
2013-08-26 22:55:58 +04:00
|
|
|
delete Log.repository;
|
|
|
|
Log.repository = new LoggerRepository();
|
|
|
|
return Log.repository;
|
2008-05-30 04:56:52 +04:00
|
|
|
},
|
2008-11-04 01:48:53 +03:00
|
|
|
set repository(value) {
|
2013-08-26 22:55:58 +04:00
|
|
|
delete Log.repository;
|
|
|
|
Log.repository = value;
|
2008-11-04 01:48:53 +03:00
|
|
|
},
|
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
_formatError(e) {
|
|
|
|
let result = String(e);
|
2014-04-29 18:09:27 +04:00
|
|
|
if (e.fileName) {
|
2018-08-02 09:23:34 +03:00
|
|
|
let loc = [e.fileName];
|
2014-04-29 18:09:27 +04:00
|
|
|
if (e.lineNumber) {
|
2018-08-02 09:23:34 +03:00
|
|
|
loc.push(e.lineNumber);
|
2014-04-29 18:09:27 +04:00
|
|
|
}
|
|
|
|
if (e.columnNumber) {
|
2018-08-02 09:23:34 +03:00
|
|
|
loc.push(e.columnNumber);
|
2014-04-29 18:09:27 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
result += `(${loc.join(":")})`;
|
2014-04-29 18:09:27 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
return `${result} ${Log.stackTrace(e)}`;
|
2014-04-29 18:09:27 +04:00
|
|
|
},
|
|
|
|
|
2014-04-29 18:09:29 +04:00
|
|
|
// This is for back compatibility with services/common/utils.js; we duplicate
|
2014-04-29 18:09:27 +04:00
|
|
|
// some of the logic in ParameterFormatter
|
2018-08-02 09:23:34 +03:00
|
|
|
exceptionStr(e) {
|
2014-04-29 18:09:27 +04:00
|
|
|
if (!e) {
|
2018-08-02 09:23:34 +03:00
|
|
|
return String(e);
|
2014-04-29 18:09:27 +04:00
|
|
|
}
|
|
|
|
if (e instanceof Ci.nsIException) {
|
2018-08-02 09:23:34 +03:00
|
|
|
return `${e} ${Log.stackTrace(e)}`;
|
2016-12-31 05:47:25 +03:00
|
|
|
} else if (isError(e)) {
|
2014-04-29 18:09:27 +04:00
|
|
|
return Log._formatError(e);
|
|
|
|
}
|
|
|
|
// else
|
2018-08-02 09:41:01 +03:00
|
|
|
let message = e.message || e;
|
2018-08-02 09:23:34 +03:00
|
|
|
return `${message} ${Log.stackTrace(e)}`;
|
2014-04-29 18:09:27 +04:00
|
|
|
},
|
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
stackTrace(e) {
|
2014-04-29 18:09:27 +04:00
|
|
|
// Wrapped nsIException
|
|
|
|
if (e.location) {
|
|
|
|
let frame = e.location;
|
|
|
|
let output = [];
|
|
|
|
while (frame) {
|
|
|
|
// Works on frames or exceptions, munges file:// URIs to shorten the paths
|
|
|
|
// FIXME: filename munging is sort of hackish, might be confusing if
|
|
|
|
// there are multiple extensions with similar filenames
|
|
|
|
let str = "<file:unknown>";
|
|
|
|
|
|
|
|
let file = frame.filename || frame.fileName;
|
|
|
|
if (file) {
|
|
|
|
str = file.replace(/^(?:chrome|file):.*?([^\/\.]+\.\w+)$/, "$1");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.lineNumber) {
|
|
|
|
str += ":" + frame.lineNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.name) {
|
|
|
|
str = frame.name + "()@" + str;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str) {
|
|
|
|
output.push(str);
|
|
|
|
}
|
|
|
|
frame = frame.caller;
|
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
return `Stack trace: ${output.join("\n")}`;
|
2014-04-29 18:09:27 +04:00
|
|
|
}
|
|
|
|
// Standard JS exception
|
|
|
|
if (e.stack) {
|
2017-06-22 13:51:43 +03:00
|
|
|
let stack = e.stack;
|
|
|
|
return (
|
|
|
|
"JS Stack trace: " +
|
2018-02-18 06:27:01 +03:00
|
|
|
stack.trim().replace(/@[^@]*?([^\/\.]+\.\w+:)/g, "@$1")
|
|
|
|
);
|
2014-04-29 18:09:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return "No traceback available";
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2007-10-10 13:09:28 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2007-12-11 06:47:11 +03:00
|
|
|
* LogMessage
|
|
|
|
* Encapsulates a single log event's data
|
2007-10-10 13:09:28 +04:00
|
|
|
*/
|
2018-08-02 09:23:34 +03:00
|
|
|
class LogMessage {
|
|
|
|
constructor(loggerName, level, message, params) {
|
|
|
|
this.loggerName = loggerName;
|
|
|
|
this.level = level;
|
|
|
|
/*
|
|
|
|
* Special case to handle "log./level/(object)", for example logging a caught exception
|
|
|
|
* without providing text or params like: catch(e) { logger.warn(e) }
|
|
|
|
* Treating this as an empty text with the object in the 'params' field causes the
|
|
|
|
* object to be formatted properly by BasicFormatter.
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
!params &&
|
|
|
|
message &&
|
|
|
|
typeof message == "object" &&
|
|
|
|
typeof message.valueOf() != "string"
|
|
|
|
) {
|
|
|
|
this.message = null;
|
|
|
|
this.params = message;
|
|
|
|
} else {
|
|
|
|
// If the message text is empty, or a string, or a String object, normal handling
|
|
|
|
this.message = message;
|
|
|
|
this.params = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The _structured field will correspond to whether this message is to
|
|
|
|
// be interpreted as a structured message.
|
|
|
|
this._structured = this.params && this.params.action;
|
|
|
|
this.time = Date.now();
|
|
|
|
}
|
|
|
|
|
2007-12-11 06:47:11 +03:00
|
|
|
get levelDesc() {
|
2013-08-26 22:55:58 +04:00
|
|
|
if (this.level in Log.Level.Desc) {
|
|
|
|
return Log.Level.Desc[this.level];
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2007-12-11 06:47:11 +03:00
|
|
|
return "UNKNOWN";
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-12-11 06:47:11 +03:00
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
toString() {
|
|
|
|
let msg = `${this.time} ${this.level} ${this.message}`;
|
2013-07-13 03:39:43 +04:00
|
|
|
if (this.params) {
|
2018-08-02 09:23:34 +03:00
|
|
|
msg += ` ${JSON.stringify(this.params)}`;
|
2013-07-13 03:39:43 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
return `LogMessage [${msg}]`;
|
2007-12-11 06:47:11 +03:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-12-11 06:47:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Logger
|
|
|
|
* Hierarchical version. Logs to all appenders, assigned or inherited
|
|
|
|
*/
|
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
class Logger {
|
|
|
|
constructor(name, repository) {
|
|
|
|
if (!repository) {
|
|
|
|
repository = Log.repository;
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
this._name = name;
|
|
|
|
this.children = [];
|
|
|
|
this.ownAppenders = [];
|
|
|
|
this.appenders = [];
|
|
|
|
this._repository = repository;
|
|
|
|
|
|
|
|
this._levelPrefName = null;
|
|
|
|
this._levelPrefValue = null;
|
|
|
|
this._level = null;
|
|
|
|
this._parent = null;
|
|
|
|
}
|
2017-12-18 09:48:41 +03:00
|
|
|
|
2008-11-04 01:48:53 +03:00
|
|
|
get name() {
|
|
|
|
return this._name;
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2008-11-04 01:48:53 +03:00
|
|
|
|
2007-12-11 06:47:11 +03:00
|
|
|
get level() {
|
2017-12-18 09:48:41 +03:00
|
|
|
if (this._levelPrefName) {
|
|
|
|
// We've been asked to use a preference to configure the logs. If the
|
|
|
|
// pref has a value we use it, otherwise we continue to use the parent.
|
|
|
|
const lpv = this._levelPrefValue;
|
|
|
|
if (lpv) {
|
|
|
|
const levelValue = Log.Level[lpv];
|
|
|
|
if (levelValue) {
|
|
|
|
// stash it in _level just in case a future value of the pref is
|
|
|
|
// invalid, in which case we end up continuing to use this value.
|
|
|
|
this._level = levelValue;
|
|
|
|
return levelValue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// in case the pref has transitioned from a value to no value, we reset
|
|
|
|
// this._level and fall through to using the parent.
|
|
|
|
this._level = null;
|
|
|
|
}
|
|
|
|
}
|
2007-12-11 06:47:11 +03:00
|
|
|
if (this._level != null) {
|
|
|
|
return this._level;
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2007-12-11 06:47:11 +03:00
|
|
|
if (this.parent) {
|
|
|
|
return this.parent.level;
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2014-04-29 18:09:29 +04:00
|
|
|
dumpError("Log warning: root logger configuration error: no level defined");
|
2013-08-26 22:55:58 +04:00
|
|
|
return Log.Level.All;
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-12-11 06:47:11 +03:00
|
|
|
set level(level) {
|
2017-12-18 09:48:41 +03:00
|
|
|
if (this._levelPrefName) {
|
|
|
|
// I guess we could honor this by nuking this._levelPrefValue, but it
|
|
|
|
// almost certainly implies confusion, so we'll warn and ignore.
|
|
|
|
dumpError(
|
|
|
|
`Log warning: The log '${this.name}' is configured to use ` +
|
|
|
|
`the preference '${this._levelPrefName}' - you must adjust ` +
|
|
|
|
`the level by setting this preference, not by using the ` +
|
|
|
|
`level setter`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-11 06:47:11 +03:00
|
|
|
this._level = level;
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2015-09-24 15:20:04 +03:00
|
|
|
get parent() {
|
|
|
|
return this._parent;
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2011-02-26 01:42:59 +03:00
|
|
|
set parent(parent) {
|
|
|
|
if (this._parent == parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Remove ourselves from parent's children
|
|
|
|
if (this._parent) {
|
|
|
|
let index = this._parent.children.indexOf(this);
|
|
|
|
if (index != -1) {
|
|
|
|
this._parent.children.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._parent = parent;
|
|
|
|
parent.children.push(this);
|
|
|
|
this.updateAppenders();
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2011-02-26 01:42:59 +03:00
|
|
|
|
2017-12-18 09:48:41 +03:00
|
|
|
manageLevelFromPref(prefName) {
|
|
|
|
if (prefName == this._levelPrefName) {
|
|
|
|
// We've already configured this log with an observer for that pref.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this._levelPrefName) {
|
|
|
|
dumpError(
|
|
|
|
`The log '${this.name}' is already configured with the ` +
|
|
|
|
`preference '${this._levelPrefName}' - ignoring request to ` +
|
|
|
|
`also use the preference '${prefName}'`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._levelPrefName = prefName;
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(this, "_levelPrefValue", prefName);
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2017-12-18 09:48:41 +03:00
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
updateAppenders() {
|
2011-02-26 01:42:59 +03:00
|
|
|
if (this._parent) {
|
|
|
|
let notOwnAppenders = this._parent.appenders.filter(function(appender) {
|
2018-02-01 22:45:22 +03:00
|
|
|
return !this.ownAppenders.includes(appender);
|
2011-02-26 01:42:59 +03:00
|
|
|
}, this);
|
|
|
|
this.appenders = notOwnAppenders.concat(this.ownAppenders);
|
|
|
|
} else {
|
|
|
|
this.appenders = this.ownAppenders.slice();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update children's appenders.
|
|
|
|
for (let i = 0; i < this.children.length; i++) {
|
|
|
|
this.children[i].updateAppenders();
|
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
addAppender(appender) {
|
2018-02-01 22:45:22 +03:00
|
|
|
if (this.ownAppenders.includes(appender)) {
|
2011-02-26 01:42:59 +03:00
|
|
|
return;
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2011-02-26 01:42:59 +03:00
|
|
|
this.ownAppenders.push(appender);
|
|
|
|
this.updateAppenders();
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
removeAppender(appender) {
|
2011-02-26 01:42:59 +03:00
|
|
|
let index = this.ownAppenders.indexOf(appender);
|
|
|
|
if (index == -1) {
|
|
|
|
return;
|
2009-02-20 12:54:45 +03:00
|
|
|
}
|
2011-02-26 01:42:59 +03:00
|
|
|
this.ownAppenders.splice(index, 1);
|
|
|
|
this.updateAppenders();
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2009-02-20 12:54:45 +03:00
|
|
|
|
2018-04-15 21:53:28 +03:00
|
|
|
_unpackTemplateLiteral(string, params) {
|
|
|
|
if (!Array.isArray(params)) {
|
|
|
|
// Regular log() call.
|
|
|
|
return [string, params];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(string)) {
|
|
|
|
// Not using template literal. However params was packed into an array by
|
|
|
|
// the this.[level] call, so we need to unpack it here.
|
|
|
|
return [string, params[0]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're using template literal format (logger.warn `foo ${bar}`). Turn the
|
|
|
|
// template strings into one string containing "${0}"..."${n}" tokens, and
|
|
|
|
// feed it to the basic formatter. The formatter will treat the numbers as
|
|
|
|
// indices into the params array, and convert the tokens to the params.
|
|
|
|
|
|
|
|
if (!params.length) {
|
|
|
|
// No params; we need to set params to undefined, so the formatter
|
|
|
|
// doesn't try to output the params array.
|
|
|
|
return [string[0], undefined];
|
|
|
|
}
|
|
|
|
|
|
|
|
let concat = string[0];
|
|
|
|
for (let i = 0; i < params.length; i++) {
|
|
|
|
concat += `\${${i}}${string[i + 1]}`;
|
|
|
|
}
|
|
|
|
return [concat, params];
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2018-04-15 21:53:28 +03:00
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
log(level, string, params) {
|
2011-02-26 01:42:59 +03:00
|
|
|
if (this.level > level) {
|
2007-10-10 13:09:28 +04:00
|
|
|
return;
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2011-02-26 01:42:59 +03:00
|
|
|
|
|
|
|
// Hold off on creating the message object until we actually have
|
|
|
|
// an appender that's responsible.
|
|
|
|
let message;
|
2007-12-11 06:47:11 +03:00
|
|
|
let appenders = this.appenders;
|
2013-06-21 01:00:14 +04:00
|
|
|
for (let appender of appenders) {
|
|
|
|
if (appender.level > level) {
|
2011-02-26 01:42:59 +03:00
|
|
|
continue;
|
2013-06-21 01:00:14 +04:00
|
|
|
}
|
|
|
|
if (!message) {
|
2018-04-15 21:53:28 +03:00
|
|
|
[string, params] = this._unpackTemplateLiteral(string, params);
|
2013-07-13 03:39:43 +04:00
|
|
|
message = new LogMessage(this._name, level, string, params);
|
2013-06-21 01:00:14 +04:00
|
|
|
}
|
2011-02-26 01:42:59 +03:00
|
|
|
appender.append(message);
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2018-04-15 21:53:28 +03:00
|
|
|
fatal(string, ...params) {
|
2013-08-26 22:55:58 +04:00
|
|
|
this.log(Log.Level.Fatal, string, params);
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2018-04-15 21:53:28 +03:00
|
|
|
error(string, ...params) {
|
2013-08-26 22:55:58 +04:00
|
|
|
this.log(Log.Level.Error, string, params);
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2018-04-15 21:53:28 +03:00
|
|
|
warn(string, ...params) {
|
2013-08-26 22:55:58 +04:00
|
|
|
this.log(Log.Level.Warn, string, params);
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2018-04-15 21:53:28 +03:00
|
|
|
info(string, ...params) {
|
2013-08-26 22:55:58 +04:00
|
|
|
this.log(Log.Level.Info, string, params);
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2018-04-15 21:53:28 +03:00
|
|
|
config(string, ...params) {
|
2013-08-26 22:55:58 +04:00
|
|
|
this.log(Log.Level.Config, string, params);
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2018-04-15 21:53:28 +03:00
|
|
|
debug(string, ...params) {
|
2013-08-26 22:55:58 +04:00
|
|
|
this.log(Log.Level.Debug, string, params);
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2018-04-15 21:53:28 +03:00
|
|
|
trace(string, ...params) {
|
2013-08-26 22:55:58 +04:00
|
|
|
this.log(Log.Level.Trace, string, params);
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* LoggerRepository
|
|
|
|
* Implements a hierarchy of Loggers
|
|
|
|
*/
|
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
class LoggerRepository {
|
|
|
|
constructor() {
|
|
|
|
this._loggers = {};
|
|
|
|
this._rootLogger = null;
|
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
|
|
|
get rootLogger() {
|
|
|
|
if (!this._rootLogger) {
|
|
|
|
this._rootLogger = new Logger("root", this);
|
2013-08-26 22:55:58 +04:00
|
|
|
this._rootLogger.level = Log.Level.All;
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
|
|
|
return this._rootLogger;
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2011-02-26 01:42:59 +03:00
|
|
|
set rootLogger(logger) {
|
2019-04-16 22:30:27 +03:00
|
|
|
throw new Error("Cannot change the root logger");
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
_updateParents(name) {
|
2017-01-17 18:48:17 +03:00
|
|
|
let pieces = name.split(".");
|
2007-10-10 13:09:28 +04:00
|
|
|
let cur, parent;
|
|
|
|
|
|
|
|
// find the closest parent
|
2008-12-02 19:12:20 +03:00
|
|
|
// don't test for the logger name itself, as there's a chance it's already
|
|
|
|
// there in this._loggers
|
|
|
|
for (let i = 0; i < pieces.length - 1; i++) {
|
2007-10-10 13:09:28 +04:00
|
|
|
if (cur) {
|
2017-01-17 18:48:17 +03:00
|
|
|
cur += "." + pieces[i];
|
2007-10-10 13:09:28 +04:00
|
|
|
} else {
|
|
|
|
cur = pieces[i];
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
if (cur in this._loggers) {
|
|
|
|
parent = cur;
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
|
|
|
|
2008-12-02 19:12:20 +03:00
|
|
|
// if we didn't assign a parent above, there is no parent
|
|
|
|
if (!parent) {
|
2007-10-10 13:09:28 +04:00
|
|
|
this._loggers[name].parent = this.rootLogger;
|
|
|
|
} else {
|
|
|
|
this._loggers[name].parent = this._loggers[parent];
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
|
|
|
// trigger updates for any possible descendants of this logger
|
|
|
|
for (let logger in this._loggers) {
|
2008-12-02 19:12:20 +03:00
|
|
|
if (logger != name && logger.indexOf(name) == 0) {
|
2007-10-10 13:09:28 +04:00
|
|
|
this._updateParents(logger);
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2014-03-28 22:36:37 +04:00
|
|
|
/**
|
|
|
|
* Obtain a named Logger.
|
|
|
|
*
|
|
|
|
* The returned Logger instance for a particular name is shared among
|
|
|
|
* all callers. In other words, if two consumers call getLogger("foo"),
|
|
|
|
* they will both have a reference to the same object.
|
|
|
|
*
|
|
|
|
* @return Logger
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
getLogger(name) {
|
2008-11-04 01:48:53 +03:00
|
|
|
if (name in this._loggers) {
|
|
|
|
return this._loggers[name];
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2008-11-04 01:48:53 +03:00
|
|
|
this._loggers[name] = new Logger(name, this);
|
|
|
|
this._updateParents(name);
|
2007-10-10 13:09:28 +04:00
|
|
|
return this._loggers[name];
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2014-03-28 22:36:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain a Logger that logs all string messages with a prefix.
|
|
|
|
*
|
|
|
|
* A common pattern is to have separate Logger instances for each instance
|
|
|
|
* of an object. But, you still want to distinguish between each instance.
|
|
|
|
* Since Log.repository.getLogger() returns shared Logger objects,
|
|
|
|
* monkeypatching one Logger modifies them all.
|
|
|
|
*
|
|
|
|
* This function returns a new object with a prototype chain that chains
|
|
|
|
* up to the original Logger instance. The new prototype has log functions
|
|
|
|
* that prefix content to each message.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* (string) The Logger to retrieve.
|
|
|
|
* @param prefix
|
|
|
|
* (string) The string to prefix each logged message with.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
getLoggerWithMessagePrefix(name, prefix) {
|
2014-03-28 22:36:37 +04:00
|
|
|
let log = this.getLogger(name);
|
|
|
|
|
2014-08-15 00:30:54 +04:00
|
|
|
let proxy = Object.create(log);
|
2018-04-15 21:53:28 +03:00
|
|
|
proxy.log = (level, string, params) => {
|
|
|
|
if (Array.isArray(string) && Array.isArray(params)) {
|
|
|
|
// Template literal.
|
|
|
|
// We cannot change the original array, so create a new one.
|
|
|
|
string = [prefix + string[0]].concat(string.slice(1));
|
|
|
|
} else {
|
|
|
|
string = prefix + string; // Regular string.
|
|
|
|
}
|
|
|
|
return log.log(level, string, params);
|
|
|
|
};
|
2014-03-28 22:36:37 +04:00
|
|
|
return proxy;
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
|
|
|
/*
|
2007-12-11 06:47:11 +03:00
|
|
|
* Formatters
|
2013-07-13 03:39:43 +04:00
|
|
|
* These massage a LogMessage into whatever output is desired.
|
2007-10-10 13:09:28 +04:00
|
|
|
*/
|
|
|
|
|
2013-07-13 03:39:43 +04:00
|
|
|
// Basic formatter that doesn't do anything fancy.
|
2018-08-02 09:23:34 +03:00
|
|
|
class BasicFormatter {
|
|
|
|
constructor(dateFormat) {
|
|
|
|
if (dateFormat) {
|
|
|
|
this.dateFormat = dateFormat;
|
|
|
|
}
|
|
|
|
this.parameterFormatter = new ParameterFormatter();
|
2014-04-29 18:09:29 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
|
2014-04-29 18:09:29 +04:00
|
|
|
/**
|
|
|
|
* Format the text of a message with optional parameters.
|
|
|
|
* If the text contains ${identifier}, replace that with
|
|
|
|
* the value of params[identifier]; if ${}, replace that with
|
|
|
|
* the entire params object. If no params have been substituted
|
|
|
|
* into the text, format the entire object and append that
|
|
|
|
* to the message.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
formatText(message) {
|
2014-04-29 18:09:29 +04:00
|
|
|
let params = message.params;
|
2014-09-24 08:38:43 +04:00
|
|
|
if (typeof params == "undefined") {
|
2014-04-29 18:09:29 +04:00
|
|
|
return message.message || "";
|
|
|
|
}
|
|
|
|
// Defensive handling of non-object params
|
|
|
|
// We could add a special case for NSRESULT values here...
|
2017-01-17 18:48:17 +03:00
|
|
|
let pIsObject = typeof params == "object" || typeof params == "function";
|
2014-04-29 18:09:29 +04:00
|
|
|
|
|
|
|
// if we have params, try and find substitutions.
|
2014-09-24 08:38:43 +04:00
|
|
|
if (this.parameterFormatter) {
|
2014-04-29 18:09:29 +04:00
|
|
|
// have we successfully substituted any parameters into the message?
|
|
|
|
// in the log message
|
|
|
|
let subDone = false;
|
2018-04-15 21:53:28 +03:00
|
|
|
let regex = /\$\{(\S*?)\}/g;
|
2014-04-29 18:09:29 +04:00
|
|
|
let textParts = [];
|
|
|
|
if (message.message) {
|
|
|
|
textParts.push(
|
|
|
|
message.message.replace(regex, (_, sub) => {
|
|
|
|
// ${foo} means use the params['foo']
|
|
|
|
if (sub) {
|
|
|
|
if (pIsObject && sub in message.params) {
|
|
|
|
subDone = true;
|
|
|
|
return this.parameterFormatter.format(message.params[sub]);
|
2019-07-05 12:15:43 +03:00
|
|
|
}
|
2017-01-17 18:48:17 +03:00
|
|
|
return "${" + sub + "}";
|
2014-04-29 18:09:29 +04:00
|
|
|
}
|
|
|
|
// ${} means use the entire params object.
|
|
|
|
subDone = true;
|
|
|
|
return this.parameterFormatter.format(message.params);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!subDone) {
|
|
|
|
// There were no substitutions in the text, so format the entire params object
|
|
|
|
let rest = this.parameterFormatter.format(message.params);
|
|
|
|
if (rest !== null && rest != "{}") {
|
|
|
|
textParts.push(rest);
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 18:48:17 +03:00
|
|
|
return textParts.join(": ");
|
2014-04-29 18:09:29 +04:00
|
|
|
}
|
2016-04-05 21:33:48 +03:00
|
|
|
return undefined;
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2014-04-29 18:09:29 +04:00
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
format(message) {
|
2013-06-21 01:00:14 +04:00
|
|
|
return (
|
|
|
|
message.time +
|
|
|
|
"\t" +
|
|
|
|
message.loggerName +
|
|
|
|
"\t" +
|
|
|
|
message.levelDesc +
|
2019-07-05 12:15:43 +03:00
|
|
|
"\t" +
|
2014-04-29 18:09:29 +04:00
|
|
|
this.formatText(message)
|
|
|
|
);
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2014-04-29 18:09:29 +04:00
|
|
|
/**
|
|
|
|
* Test an object to see if it is a Mozilla JS Error.
|
|
|
|
*/
|
|
|
|
function isError(aObj) {
|
2017-01-17 18:48:17 +03:00
|
|
|
return (
|
|
|
|
aObj &&
|
|
|
|
typeof aObj == "object" &&
|
|
|
|
"name" in aObj &&
|
|
|
|
"message" in aObj &&
|
2014-04-29 18:09:29 +04:00
|
|
|
"fileName" in aObj &&
|
|
|
|
"lineNumber" in aObj &&
|
|
|
|
"stack" in aObj
|
|
|
|
);
|
2016-02-04 04:27:36 +03:00
|
|
|
}
|
2014-04-29 18:09:29 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parameter Formatters
|
|
|
|
* These massage an object used as a parameter for a LogMessage into
|
|
|
|
* a string representation of the object.
|
|
|
|
*/
|
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
class ParameterFormatter {
|
|
|
|
constructor() {
|
|
|
|
this._name = "ParameterFormatter";
|
|
|
|
}
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
format(ob) {
|
2014-04-29 18:09:29 +04:00
|
|
|
try {
|
|
|
|
if (ob === undefined) {
|
|
|
|
return "undefined";
|
|
|
|
}
|
|
|
|
if (ob === null) {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
// Pass through primitive types and objects that unbox to primitive types.
|
|
|
|
if (
|
|
|
|
(typeof ob != "object" || typeof ob.valueOf() != "object") &&
|
|
|
|
typeof ob != "function"
|
|
|
|
) {
|
|
|
|
return ob;
|
|
|
|
}
|
|
|
|
if (ob instanceof Ci.nsIException) {
|
2018-08-02 09:23:34 +03:00
|
|
|
return `${ob} ${Log.stackTrace(ob)}`;
|
2016-12-31 05:47:25 +03:00
|
|
|
} else if (isError(ob)) {
|
2014-04-29 18:09:29 +04:00
|
|
|
return Log._formatError(ob);
|
|
|
|
}
|
|
|
|
// Just JSONify it. Filter out our internal fields and those the caller has
|
|
|
|
// already handled.
|
|
|
|
return JSON.stringify(ob, (key, val) => {
|
|
|
|
if (INTERNAL_FIELDS.has(key)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
});
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2018-08-02 09:23:34 +03:00
|
|
|
dumpError(
|
|
|
|
`Exception trying to format object for log message: ${Log.exceptionStr(
|
2019-07-05 12:15:43 +03:00
|
|
|
e
|
2018-08-02 09:23:34 +03:00
|
|
|
)}`
|
|
|
|
);
|
2014-04-29 18:09:29 +04:00
|
|
|
}
|
|
|
|
// Fancy formatting failed. Just toSource() it - but even this may fail!
|
|
|
|
try {
|
|
|
|
return ob.toSource();
|
|
|
|
} catch (_) {}
|
|
|
|
try {
|
2018-08-02 09:23:34 +03:00
|
|
|
return String(ob);
|
2014-04-29 18:09:29 +04:00
|
|
|
} catch (_) {
|
2017-10-15 21:50:30 +03:00
|
|
|
return "[object]";
|
2014-04-29 18:09:29 +04:00
|
|
|
}
|
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2014-04-29 18:09:29 +04:00
|
|
|
|
2007-10-10 13:09:28 +04:00
|
|
|
/*
|
|
|
|
* Appenders
|
|
|
|
* These can be attached to Loggers to log to different places
|
|
|
|
* Simply subclass and override doAppend to implement a new one
|
|
|
|
*/
|
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
class Appender {
|
|
|
|
constructor(formatter) {
|
|
|
|
this.level = Log.Level.All;
|
|
|
|
this._name = "Appender";
|
|
|
|
this._formatter = formatter || new BasicFormatter();
|
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
append(message) {
|
2013-06-21 01:00:14 +04:00
|
|
|
if (message) {
|
|
|
|
this.doAppend(this._formatter.format(message));
|
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return `${this._name} [level=${this.level}, formatter=${this._formatter}]`;
|
|
|
|
}
|
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* DumpAppender
|
|
|
|
* Logs to standard out
|
|
|
|
*/
|
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
class DumpAppender extends Appender {
|
|
|
|
constructor(formatter) {
|
|
|
|
super(formatter);
|
|
|
|
this._name = "DumpAppender";
|
|
|
|
}
|
2008-11-04 01:48:53 +03:00
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
doAppend(formatted) {
|
2014-04-29 18:09:25 +04:00
|
|
|
dump(formatted + "\n");
|
2007-10-11 04:08:58 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ConsoleAppender
|
|
|
|
* Logs to the javascript console
|
|
|
|
*/
|
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
class ConsoleAppender extends Appender {
|
|
|
|
constructor(formatter) {
|
|
|
|
super(formatter);
|
|
|
|
this._name = "ConsoleAppender";
|
|
|
|
}
|
2008-11-04 01:48:53 +03:00
|
|
|
|
2014-04-29 18:09:25 +04:00
|
|
|
// XXX this should be replaced with calls to the Browser Console
|
2018-08-02 09:23:34 +03:00
|
|
|
append(message) {
|
2014-04-29 18:09:25 +04:00
|
|
|
if (message) {
|
|
|
|
let m = this._formatter.format(message);
|
|
|
|
if (message.level > Log.Level.Warn) {
|
|
|
|
Cu.reportError(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.doAppend(m);
|
2007-10-11 04:08:58 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
2014-04-29 18:09:25 +04:00
|
|
|
|
2018-08-02 09:23:34 +03:00
|
|
|
doAppend(formatted) {
|
2017-11-09 19:36:57 +03:00
|
|
|
Services.console.logStringMessage(formatted);
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2018-08-02 09:23:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(Log, {
|
|
|
|
LogMessage,
|
|
|
|
Logger,
|
|
|
|
LoggerRepository,
|
|
|
|
|
|
|
|
BasicFormatter,
|
|
|
|
|
|
|
|
Appender,
|
|
|
|
DumpAppender,
|
|
|
|
ConsoleAppender,
|
|
|
|
|
|
|
|
ParameterFormatter,
|
|
|
|
});
|