2012-05-21 11:12:37 +00: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 18:55:58 +00:00
|
|
|
|
2013-06-20 21:00:14 +00:00
|
|
|
"use strict";
|
2007-10-10 09:09:28 +00:00
|
|
|
|
2013-08-26 18:55:58 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["Log"];
|
2007-12-15 02:07:25 +00:00
|
|
|
|
2012-04-06 06:26:06 +00:00
|
|
|
const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
|
2007-10-10 09:09:28 +00:00
|
|
|
|
2008-01-25 01:41:36 +00:00
|
|
|
const ONE_BYTE = 1;
|
|
|
|
const ONE_KILOBYTE = 1024 * ONE_BYTE;
|
|
|
|
const ONE_MEGABYTE = 1024 * ONE_KILOBYTE;
|
2007-12-15 02:07:25 +00:00
|
|
|
|
2011-06-13 18:39:37 +00:00
|
|
|
const STREAM_SEGMENT_SIZE = 4096;
|
|
|
|
const PR_UINT32_MAX = 0xffffffff;
|
|
|
|
|
2013-07-26 21:16:55 +00:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "OS",
|
|
|
|
"resource://gre/modules/osfile.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Task",
|
|
|
|
"resource://gre/modules/Task.jsm");
|
2014-04-29 14:09:29 +00: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 18:39:37 +00:00
|
|
|
|
2013-08-26 18:55:58 +00:00
|
|
|
this.Log = {
|
2008-03-25 20:55:34 +00:00
|
|
|
Level: {
|
|
|
|
Fatal: 70,
|
|
|
|
Error: 60,
|
|
|
|
Warn: 50,
|
|
|
|
Info: 40,
|
|
|
|
Config: 30,
|
|
|
|
Debug: 20,
|
|
|
|
Trace: 10,
|
2014-09-24 04:38:34 +00:00
|
|
|
All: -1, // We don't want All to be falsy.
|
2008-03-25 20:55:34 +00:00
|
|
|
Desc: {
|
|
|
|
70: "FATAL",
|
|
|
|
60: "ERROR",
|
|
|
|
50: "WARN",
|
|
|
|
40: "INFO",
|
|
|
|
30: "CONFIG",
|
|
|
|
20: "DEBUG",
|
|
|
|
10: "TRACE",
|
2014-09-24 04:38:34 +00:00
|
|
|
"-1": "ALL",
|
2013-07-12 23:39:43 +00:00
|
|
|
},
|
|
|
|
Numbers: {
|
|
|
|
"FATAL": 70,
|
|
|
|
"ERROR": 60,
|
|
|
|
"WARN": 50,
|
|
|
|
"INFO": 40,
|
|
|
|
"CONFIG": 30,
|
|
|
|
"DEBUG": 20,
|
|
|
|
"TRACE": 10,
|
2014-09-24 04:38:34 +00:00
|
|
|
"ALL": -1,
|
2008-03-25 20:55:34 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-11-03 22:48:53 +00:00
|
|
|
get repository() {
|
2013-08-26 18:55:58 +00:00
|
|
|
delete Log.repository;
|
|
|
|
Log.repository = new LoggerRepository();
|
|
|
|
return Log.repository;
|
2008-05-30 00:56:52 +00:00
|
|
|
},
|
2008-11-03 22:48:53 +00:00
|
|
|
set repository(value) {
|
2013-08-26 18:55:58 +00:00
|
|
|
delete Log.repository;
|
|
|
|
Log.repository = value;
|
2008-11-03 22:48:53 +00:00
|
|
|
},
|
|
|
|
|
2011-06-13 18:39:14 +00:00
|
|
|
LogMessage: LogMessage,
|
|
|
|
Logger: Logger,
|
|
|
|
LoggerRepository: LoggerRepository,
|
2008-05-30 00:56:52 +00:00
|
|
|
|
2011-06-13 18:39:14 +00:00
|
|
|
Formatter: Formatter,
|
|
|
|
BasicFormatter: BasicFormatter,
|
2014-03-28 18:36:37 +00:00
|
|
|
MessageOnlyFormatter: MessageOnlyFormatter,
|
2013-07-12 23:39:43 +00:00
|
|
|
StructuredFormatter: StructuredFormatter,
|
2008-11-03 22:48:53 +00:00
|
|
|
|
2011-06-13 18:39:14 +00:00
|
|
|
Appender: Appender,
|
|
|
|
DumpAppender: DumpAppender,
|
|
|
|
ConsoleAppender: ConsoleAppender,
|
2011-06-13 18:39:37 +00:00
|
|
|
StorageStreamAppender: StorageStreamAppender,
|
|
|
|
|
2011-06-13 18:39:14 +00:00
|
|
|
FileAppender: FileAppender,
|
2013-06-20 21:00:14 +00:00
|
|
|
BoundedFileAppender: BoundedFileAppender,
|
2008-08-20 00:28:02 +00:00
|
|
|
|
2014-04-29 14:09:29 +00:00
|
|
|
ParameterFormatter: ParameterFormatter,
|
2008-08-20 00:28:02 +00:00
|
|
|
// Logging helper:
|
2013-08-26 18:55:58 +00:00
|
|
|
// let logger = Log.repository.getLogger("foo");
|
|
|
|
// logger.info(Log.enumerateInterfaces(someObject).join(","));
|
|
|
|
enumerateInterfaces: function Log_enumerateInterfaces(aObject) {
|
2008-08-20 00:28:02 +00:00
|
|
|
let interfaces = [];
|
|
|
|
|
|
|
|
for (i in Ci) {
|
|
|
|
try {
|
|
|
|
aObject.QueryInterface(Ci[i]);
|
|
|
|
interfaces.push(i);
|
|
|
|
}
|
2016-08-04 07:28:58 +00:00
|
|
|
catch (ex) {}
|
2008-08-20 00:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return interfaces;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Logging helper:
|
2013-08-26 18:55:58 +00:00
|
|
|
// let logger = Log.repository.getLogger("foo");
|
|
|
|
// logger.info(Log.enumerateProperties(someObject).join(","));
|
2016-11-11 23:22:34 +00:00
|
|
|
enumerateProperties: function(aObject, aExcludeComplexTypes) {
|
2008-08-20 00:28:02 +00:00
|
|
|
let properties = [];
|
|
|
|
|
|
|
|
for (p in aObject) {
|
|
|
|
try {
|
|
|
|
if (aExcludeComplexTypes &&
|
2014-04-29 14:09:29 +00:00
|
|
|
(typeof(aObject[p]) == "object" || typeof(aObject[p]) == "function"))
|
2008-08-20 00:28:02 +00:00
|
|
|
continue;
|
|
|
|
properties.push(p + " = " + aObject[p]);
|
|
|
|
}
|
2016-08-04 07:28:58 +00:00
|
|
|
catch (ex) {
|
2008-08-20 00:28:02 +00:00
|
|
|
properties.push(p + " = " + ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return properties;
|
2014-04-29 14:09:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_formatError: function _formatError(e) {
|
|
|
|
let result = e.toString();
|
|
|
|
if (e.fileName) {
|
|
|
|
result += " (" + e.fileName;
|
|
|
|
if (e.lineNumber) {
|
|
|
|
result += ":" + e.lineNumber;
|
|
|
|
}
|
|
|
|
if (e.columnNumber) {
|
|
|
|
result += ":" + e.columnNumber;
|
|
|
|
}
|
|
|
|
result += ")";
|
|
|
|
}
|
|
|
|
return result + " " + Log.stackTrace(e);
|
|
|
|
},
|
|
|
|
|
2014-04-29 14:09:29 +00:00
|
|
|
// This is for back compatibility with services/common/utils.js; we duplicate
|
2014-04-29 14:09:27 +00:00
|
|
|
// some of the logic in ParameterFormatter
|
|
|
|
exceptionStr: function exceptionStr(e) {
|
|
|
|
if (!e) {
|
|
|
|
return "" + e;
|
|
|
|
}
|
|
|
|
if (e instanceof Ci.nsIException) {
|
|
|
|
return e.toString() + " " + Log.stackTrace(e);
|
|
|
|
}
|
|
|
|
else if (isError(e)) {
|
|
|
|
return Log._formatError(e);
|
|
|
|
}
|
|
|
|
// else
|
|
|
|
let message = e.message ? e.message : e;
|
|
|
|
return message + " " + Log.stackTrace(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
stackTrace: function stackTrace(e) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
return "Stack trace: " + output.join(" < ");
|
|
|
|
}
|
|
|
|
// Standard JS exception
|
|
|
|
if (e.stack) {
|
2015-03-11 07:21:00 +00:00
|
|
|
return "JS Stack trace: " + Task.Debugging.generateReadableStack(e.stack).trim()
|
|
|
|
.replace(/\n/g, " < ").replace(/@[^@]*?([^\/\.]+\.\w+:)/g, "@$1");
|
2014-04-29 14:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "No traceback available";
|
2008-08-20 00:28:02 +00:00
|
|
|
}
|
2007-10-10 09:09:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2007-12-11 03:47:11 +00:00
|
|
|
* LogMessage
|
|
|
|
* Encapsulates a single log event's data
|
2007-10-10 09:09:28 +00:00
|
|
|
*/
|
2013-07-12 23:39:43 +00:00
|
|
|
function LogMessage(loggerName, level, message, params) {
|
2007-12-11 03:47:11 +00:00
|
|
|
this.loggerName = loggerName;
|
|
|
|
this.level = level;
|
2014-04-29 14:09:29 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
2013-07-12 23:39:43 +00:00
|
|
|
|
|
|
|
// The _structured field will correspond to whether this message is to
|
|
|
|
// be interpreted as a structured message.
|
|
|
|
this._structured = this.params && this.params.action;
|
2007-12-11 03:47:11 +00:00
|
|
|
this.time = Date.now();
|
|
|
|
}
|
|
|
|
LogMessage.prototype = {
|
|
|
|
get levelDesc() {
|
2013-08-26 18:55:58 +00:00
|
|
|
if (this.level in Log.Level.Desc)
|
|
|
|
return Log.Level.Desc[this.level];
|
2007-12-11 03:47:11 +00:00
|
|
|
return "UNKNOWN";
|
|
|
|
},
|
|
|
|
|
2014-04-29 14:09:27 +00:00
|
|
|
toString: function LogMsg_toString() {
|
2013-07-12 23:39:43 +00:00
|
|
|
let msg = "LogMessage [" + this.time + " " + this.level + " " +
|
|
|
|
this.message;
|
|
|
|
if (this.params) {
|
|
|
|
msg += " " + JSON.stringify(this.params);
|
|
|
|
}
|
|
|
|
return msg + "]"
|
2007-12-11 03:47:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Logger
|
|
|
|
* Hierarchical version. Logs to all appenders, assigned or inherited
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Logger(name, repository) {
|
2010-02-11 23:25:31 +00:00
|
|
|
if (!repository)
|
2013-08-26 18:55:58 +00:00
|
|
|
repository = Log.repository;
|
2010-02-11 23:25:31 +00:00
|
|
|
this._name = name;
|
2011-02-25 22:42:59 +00:00
|
|
|
this.children = [];
|
|
|
|
this.ownAppenders = [];
|
|
|
|
this.appenders = [];
|
2010-02-11 23:25:31 +00:00
|
|
|
this._repository = repository;
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
2007-12-11 03:47:11 +00:00
|
|
|
Logger.prototype = {
|
2008-11-03 22:48:53 +00:00
|
|
|
get name() {
|
|
|
|
return this._name;
|
|
|
|
},
|
|
|
|
|
2007-12-11 03:47:11 +00:00
|
|
|
_level: null,
|
|
|
|
get level() {
|
|
|
|
if (this._level != null)
|
|
|
|
return this._level;
|
|
|
|
if (this.parent)
|
|
|
|
return this.parent.level;
|
2014-04-29 14:09:29 +00:00
|
|
|
dumpError("Log warning: root logger configuration error: no level defined");
|
2013-08-26 18:55:58 +00:00
|
|
|
return Log.Level.All;
|
2007-12-11 03:47:11 +00:00
|
|
|
},
|
|
|
|
set level(level) {
|
|
|
|
this._level = level;
|
2007-10-10 09:09:28 +00:00
|
|
|
},
|
|
|
|
|
2011-02-25 22:42:59 +00:00
|
|
|
_parent: null,
|
2015-09-24 12:20:04 +00:00
|
|
|
get parent() {
|
|
|
|
return this._parent;
|
|
|
|
},
|
2011-02-25 22:42:59 +00: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();
|
|
|
|
},
|
|
|
|
|
|
|
|
updateAppenders: function updateAppenders() {
|
|
|
|
if (this._parent) {
|
|
|
|
let notOwnAppenders = this._parent.appenders.filter(function(appender) {
|
|
|
|
return this.ownAppenders.indexOf(appender) == -1;
|
|
|
|
}, 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();
|
|
|
|
}
|
2007-10-10 09:09:28 +00:00
|
|
|
},
|
|
|
|
|
2007-12-11 03:47:11 +00:00
|
|
|
addAppender: function Logger_addAppender(appender) {
|
2011-02-25 22:42:59 +00:00
|
|
|
if (this.ownAppenders.indexOf(appender) != -1) {
|
|
|
|
return;
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
2011-02-25 22:42:59 +00:00
|
|
|
this.ownAppenders.push(appender);
|
|
|
|
this.updateAppenders();
|
2007-10-10 09:09:28 +00:00
|
|
|
},
|
|
|
|
|
2009-02-20 09:54:45 +00:00
|
|
|
removeAppender: function Logger_removeAppender(appender) {
|
2011-02-25 22:42:59 +00:00
|
|
|
let index = this.ownAppenders.indexOf(appender);
|
|
|
|
if (index == -1) {
|
|
|
|
return;
|
2009-02-20 09:54:45 +00:00
|
|
|
}
|
2011-02-25 22:42:59 +00:00
|
|
|
this.ownAppenders.splice(index, 1);
|
|
|
|
this.updateAppenders();
|
2009-02-20 09:54:45 +00:00
|
|
|
},
|
|
|
|
|
2013-07-12 23:39:43 +00:00
|
|
|
/**
|
|
|
|
* Logs a structured message object.
|
|
|
|
*
|
|
|
|
* @param action
|
|
|
|
* (string) A message action, one of a set of actions known to the
|
|
|
|
* log consumer.
|
|
|
|
* @param params
|
|
|
|
* (object) Parameters to be included in the message.
|
|
|
|
* If _level is included as a key and the corresponding value
|
|
|
|
* is a number or known level name, the message will be logged
|
2014-04-29 14:09:29 +00:00
|
|
|
* at the indicated level. If _message is included as a key, the
|
|
|
|
* value is used as the descriptive text for the message.
|
2013-07-12 23:39:43 +00:00
|
|
|
*/
|
2016-11-11 23:22:34 +00:00
|
|
|
logStructured: function(action, params) {
|
2013-07-12 23:39:43 +00:00
|
|
|
if (!action) {
|
|
|
|
throw "An action is required when logging a structured message.";
|
|
|
|
}
|
|
|
|
if (!params) {
|
2016-04-05 18:33:48 +00:00
|
|
|
this.log(this.level, undefined, {"action": action});
|
|
|
|
return;
|
2013-07-12 23:39:43 +00:00
|
|
|
}
|
2014-04-29 14:09:29 +00:00
|
|
|
if (typeof(params) != "object") {
|
2013-07-12 23:39:43 +00:00
|
|
|
throw "The params argument is required to be an object.";
|
|
|
|
}
|
|
|
|
|
2014-04-29 14:09:25 +00:00
|
|
|
let level = params._level;
|
|
|
|
if (level) {
|
|
|
|
let ulevel = level.toUpperCase();
|
|
|
|
if (ulevel in Log.Level.Numbers) {
|
|
|
|
level = Log.Level.Numbers[ulevel];
|
|
|
|
}
|
2014-04-29 14:09:29 +00:00
|
|
|
} else {
|
2014-04-29 14:09:25 +00:00
|
|
|
level = this.level;
|
2013-07-12 23:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params.action = action;
|
|
|
|
this.log(level, params._message, params);
|
|
|
|
},
|
|
|
|
|
2016-11-11 23:22:34 +00:00
|
|
|
log: function(level, string, params) {
|
2011-02-25 22:42:59 +00:00
|
|
|
if (this.level > level)
|
2007-10-10 09:09:28 +00:00
|
|
|
return;
|
2011-02-25 22:42:59 +00:00
|
|
|
|
|
|
|
// Hold off on creating the message object until we actually have
|
|
|
|
// an appender that's responsible.
|
|
|
|
let message;
|
2007-12-11 03:47:11 +00:00
|
|
|
let appenders = this.appenders;
|
2013-06-20 21:00:14 +00:00
|
|
|
for (let appender of appenders) {
|
|
|
|
if (appender.level > level) {
|
2011-02-25 22:42:59 +00:00
|
|
|
continue;
|
2013-06-20 21:00:14 +00:00
|
|
|
}
|
|
|
|
if (!message) {
|
2013-07-12 23:39:43 +00:00
|
|
|
message = new LogMessage(this._name, level, string, params);
|
2013-06-20 21:00:14 +00:00
|
|
|
}
|
2011-02-25 22:42:59 +00:00
|
|
|
appender.append(message);
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-11 23:22:34 +00:00
|
|
|
fatal: function(string, params) {
|
2013-08-26 18:55:58 +00:00
|
|
|
this.log(Log.Level.Fatal, string, params);
|
2007-12-11 03:47:11 +00:00
|
|
|
},
|
2016-11-11 23:22:34 +00:00
|
|
|
error: function(string, params) {
|
2013-08-26 18:55:58 +00:00
|
|
|
this.log(Log.Level.Error, string, params);
|
2007-12-11 03:47:11 +00:00
|
|
|
},
|
2016-11-11 23:22:34 +00:00
|
|
|
warn: function(string, params) {
|
2013-08-26 18:55:58 +00:00
|
|
|
this.log(Log.Level.Warn, string, params);
|
2007-12-11 03:47:11 +00:00
|
|
|
},
|
2016-11-11 23:22:34 +00:00
|
|
|
info: function(string, params) {
|
2013-08-26 18:55:58 +00:00
|
|
|
this.log(Log.Level.Info, string, params);
|
2007-12-11 03:47:11 +00:00
|
|
|
},
|
2016-11-11 23:22:34 +00:00
|
|
|
config: function(string, params) {
|
2013-08-26 18:55:58 +00:00
|
|
|
this.log(Log.Level.Config, string, params);
|
2007-12-11 03:47:11 +00:00
|
|
|
},
|
2016-11-11 23:22:34 +00:00
|
|
|
debug: function(string, params) {
|
2013-08-26 18:55:58 +00:00
|
|
|
this.log(Log.Level.Debug, string, params);
|
2007-12-11 03:47:11 +00:00
|
|
|
},
|
2016-11-11 23:22:34 +00:00
|
|
|
trace: function(string, params) {
|
2013-08-26 18:55:58 +00:00
|
|
|
this.log(Log.Level.Trace, string, params);
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LoggerRepository
|
|
|
|
* Implements a hierarchy of Loggers
|
|
|
|
*/
|
|
|
|
|
|
|
|
function LoggerRepository() {}
|
|
|
|
LoggerRepository.prototype = {
|
|
|
|
_loggers: {},
|
|
|
|
|
|
|
|
_rootLogger: null,
|
|
|
|
get rootLogger() {
|
|
|
|
if (!this._rootLogger) {
|
|
|
|
this._rootLogger = new Logger("root", this);
|
2013-08-26 18:55:58 +00:00
|
|
|
this._rootLogger.level = Log.Level.All;
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
|
|
|
return this._rootLogger;
|
|
|
|
},
|
2011-02-25 22:42:59 +00:00
|
|
|
set rootLogger(logger) {
|
|
|
|
throw "Cannot change the root logger";
|
|
|
|
},
|
2007-10-10 09:09:28 +00:00
|
|
|
|
|
|
|
_updateParents: function LogRep__updateParents(name) {
|
|
|
|
let pieces = name.split('.');
|
|
|
|
let cur, parent;
|
|
|
|
|
|
|
|
// find the closest parent
|
2008-12-02 16:12:20 +00: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 09:09:28 +00:00
|
|
|
if (cur)
|
|
|
|
cur += '.' + pieces[i];
|
|
|
|
else
|
|
|
|
cur = pieces[i];
|
|
|
|
if (cur in this._loggers)
|
|
|
|
parent = cur;
|
|
|
|
}
|
|
|
|
|
2008-12-02 16:12:20 +00:00
|
|
|
// if we didn't assign a parent above, there is no parent
|
|
|
|
if (!parent)
|
2007-10-10 09:09:28 +00:00
|
|
|
this._loggers[name].parent = this.rootLogger;
|
|
|
|
else
|
|
|
|
this._loggers[name].parent = this._loggers[parent];
|
|
|
|
|
|
|
|
// trigger updates for any possible descendants of this logger
|
|
|
|
for (let logger in this._loggers) {
|
2008-12-02 16:12:20 +00:00
|
|
|
if (logger != name && logger.indexOf(name) == 0)
|
2007-10-10 09:09:28 +00:00
|
|
|
this._updateParents(logger);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-28 18:36:37 +00: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-11-11 23:22:34 +00:00
|
|
|
getLogger: function(name) {
|
2008-11-03 22:48:53 +00:00
|
|
|
if (name in this._loggers)
|
|
|
|
return this._loggers[name];
|
|
|
|
this._loggers[name] = new Logger(name, this);
|
|
|
|
this._updateParents(name);
|
2007-10-10 09:09:28 +00:00
|
|
|
return this._loggers[name];
|
2014-03-28 18:36:37 +00: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-11-11 23:22:34 +00:00
|
|
|
getLoggerWithMessagePrefix: function(name, prefix) {
|
2014-03-28 18:36:37 +00:00
|
|
|
let log = this.getLogger(name);
|
|
|
|
|
2014-08-14 20:30:54 +00:00
|
|
|
let proxy = Object.create(log);
|
|
|
|
proxy.log = (level, string, params) => log.log(level, prefix + string, params);
|
2014-03-28 18:36:37 +00:00
|
|
|
return proxy;
|
|
|
|
},
|
2007-12-11 03:47:11 +00:00
|
|
|
};
|
2007-10-10 09:09:28 +00:00
|
|
|
|
|
|
|
/*
|
2007-12-11 03:47:11 +00:00
|
|
|
* Formatters
|
2013-07-12 23:39:43 +00:00
|
|
|
* These massage a LogMessage into whatever output is desired.
|
|
|
|
* BasicFormatter and StructuredFormatter are implemented here.
|
2007-10-10 09:09:28 +00:00
|
|
|
*/
|
|
|
|
|
2007-12-11 03:47:11 +00:00
|
|
|
// Abstract formatter
|
|
|
|
function Formatter() {}
|
|
|
|
Formatter.prototype = {
|
|
|
|
format: function Formatter_format(message) {}
|
|
|
|
};
|
2007-10-10 09:09:28 +00:00
|
|
|
|
2013-07-12 23:39:43 +00:00
|
|
|
// Basic formatter that doesn't do anything fancy.
|
2007-12-11 03:47:11 +00:00
|
|
|
function BasicFormatter(dateFormat) {
|
2014-04-29 14:09:29 +00:00
|
|
|
if (dateFormat) {
|
2007-12-11 03:47:11 +00:00
|
|
|
this.dateFormat = dateFormat;
|
2014-04-29 14:09:29 +00:00
|
|
|
}
|
|
|
|
this.parameterFormatter = new ParameterFormatter();
|
2007-12-11 03:47:11 +00:00
|
|
|
}
|
|
|
|
BasicFormatter.prototype = {
|
2008-11-03 22:48:53 +00:00
|
|
|
__proto__: Formatter.prototype,
|
|
|
|
|
2014-04-29 14:09:29 +00: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-11-11 23:22:34 +00:00
|
|
|
formatText: function(message) {
|
2014-04-29 14:09:29 +00:00
|
|
|
let params = message.params;
|
2014-09-24 04:38:43 +00:00
|
|
|
if (typeof(params) == "undefined") {
|
2014-04-29 14:09:29 +00:00
|
|
|
return message.message || "";
|
|
|
|
}
|
|
|
|
// Defensive handling of non-object params
|
|
|
|
// We could add a special case for NSRESULT values here...
|
|
|
|
let pIsObject = (typeof(params) == 'object' || typeof(params) == 'function');
|
|
|
|
|
|
|
|
// if we have params, try and find substitutions.
|
2014-09-24 04:38:43 +00:00
|
|
|
if (this.parameterFormatter) {
|
2014-04-29 14:09:29 +00:00
|
|
|
// have we successfully substituted any parameters into the message?
|
|
|
|
// in the log message
|
|
|
|
let subDone = false;
|
|
|
|
let regex = /\$\{(\S*)\}/g;
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
return '${' + sub + '}';
|
|
|
|
}
|
|
|
|
// ${} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return textParts.join(': ');
|
|
|
|
}
|
2016-04-05 18:33:48 +00:00
|
|
|
return undefined;
|
2014-04-29 14:09:29 +00:00
|
|
|
},
|
|
|
|
|
2007-12-11 03:47:11 +00:00
|
|
|
format: function BF_format(message) {
|
2013-06-20 21:00:14 +00:00
|
|
|
return message.time + "\t" +
|
|
|
|
message.loggerName + "\t" +
|
|
|
|
message.levelDesc + "\t" +
|
2014-04-29 14:09:29 +00:00
|
|
|
this.formatText(message);
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-28 18:36:37 +00:00
|
|
|
/**
|
|
|
|
* A formatter that only formats the string message component.
|
|
|
|
*/
|
|
|
|
function MessageOnlyFormatter() {
|
|
|
|
}
|
|
|
|
MessageOnlyFormatter.prototype = Object.freeze({
|
|
|
|
__proto__: Formatter.prototype,
|
|
|
|
|
2016-11-11 23:22:34 +00:00
|
|
|
format: function(message) {
|
2014-04-29 14:09:25 +00:00
|
|
|
return message.message;
|
2014-03-28 18:36:37 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2013-07-12 23:39:43 +00:00
|
|
|
// Structured formatter that outputs JSON based on message data.
|
|
|
|
// This formatter will format unstructured messages by supplying
|
|
|
|
// default values.
|
|
|
|
function StructuredFormatter() { }
|
|
|
|
StructuredFormatter.prototype = {
|
|
|
|
__proto__: Formatter.prototype,
|
|
|
|
|
2016-11-11 23:22:34 +00:00
|
|
|
format: function(logMessage) {
|
2013-07-12 23:39:43 +00:00
|
|
|
let output = {
|
|
|
|
_time: logMessage.time,
|
|
|
|
_namespace: logMessage.loggerName,
|
|
|
|
_level: logMessage.levelDesc
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let key in logMessage.params) {
|
|
|
|
output[key] = logMessage.params[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!output.action) {
|
|
|
|
output.action = "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!output._message && logMessage.message) {
|
|
|
|
output._message = logMessage.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 14:09:29 +00:00
|
|
|
/**
|
|
|
|
* Test an object to see if it is a Mozilla JS Error.
|
|
|
|
*/
|
|
|
|
function isError(aObj) {
|
|
|
|
return (aObj && typeof(aObj) == 'object' && "name" in aObj && "message" in aObj &&
|
|
|
|
"fileName" in aObj && "lineNumber" in aObj && "stack" in aObj);
|
2016-02-04 01:27:36 +00:00
|
|
|
}
|
2014-04-29 14:09:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parameter Formatters
|
|
|
|
* These massage an object used as a parameter for a LogMessage into
|
|
|
|
* a string representation of the object.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ParameterFormatter() {
|
|
|
|
this._name = "ParameterFormatter"
|
|
|
|
}
|
|
|
|
ParameterFormatter.prototype = {
|
|
|
|
format: function(ob) {
|
|
|
|
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) {
|
|
|
|
return ob.toString() + " " + Log.stackTrace(ob);
|
|
|
|
}
|
|
|
|
else if (isError(ob)) {
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
dumpError("Exception trying to format object for log message: " + Log.exceptionStr(e));
|
|
|
|
}
|
|
|
|
// Fancy formatting failed. Just toSource() it - but even this may fail!
|
|
|
|
try {
|
|
|
|
return ob.toSource();
|
|
|
|
} catch (_) { }
|
|
|
|
try {
|
|
|
|
return "" + ob;
|
|
|
|
} catch (_) {
|
|
|
|
return "[object]"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-10 09:09:28 +00:00
|
|
|
/*
|
|
|
|
* Appenders
|
|
|
|
* These can be attached to Loggers to log to different places
|
|
|
|
* Simply subclass and override doAppend to implement a new one
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Appender(formatter) {
|
|
|
|
this._name = "Appender";
|
2016-11-10 22:48:04 +00:00
|
|
|
this._formatter = formatter ? formatter : new BasicFormatter();
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
|
|
|
Appender.prototype = {
|
2013-08-26 18:55:58 +00:00
|
|
|
level: Log.Level.All,
|
2007-10-10 09:09:28 +00:00
|
|
|
|
|
|
|
append: function App_append(message) {
|
2013-06-20 21:00:14 +00:00
|
|
|
if (message) {
|
|
|
|
this.doAppend(this._formatter.format(message));
|
|
|
|
}
|
2007-10-10 09:09:28 +00:00
|
|
|
},
|
|
|
|
toString: function App_toString() {
|
2014-04-29 14:09:25 +00:00
|
|
|
return this._name + " [level=" + this.level +
|
2007-10-10 09:09:28 +00:00
|
|
|
", formatter=" + this._formatter + "]";
|
|
|
|
},
|
2014-04-29 14:09:25 +00:00
|
|
|
doAppend: function App_doAppend(formatted) {}
|
2007-10-10 09:09:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DumpAppender
|
|
|
|
* Logs to standard out
|
|
|
|
*/
|
|
|
|
|
|
|
|
function DumpAppender(formatter) {
|
2011-06-13 18:39:37 +00:00
|
|
|
Appender.call(this, formatter);
|
2014-02-24 22:31:41 +00:00
|
|
|
this._name = "DumpAppender";
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
2007-10-11 00:08:58 +00:00
|
|
|
DumpAppender.prototype = {
|
2008-11-03 22:48:53 +00:00
|
|
|
__proto__: Appender.prototype,
|
|
|
|
|
2014-04-29 14:09:25 +00:00
|
|
|
doAppend: function DApp_doAppend(formatted) {
|
|
|
|
dump(formatted + "\n");
|
2007-10-11 00:08:58 +00:00
|
|
|
}
|
2007-10-10 09:09:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ConsoleAppender
|
|
|
|
* Logs to the javascript console
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ConsoleAppender(formatter) {
|
2011-06-13 18:39:37 +00:00
|
|
|
Appender.call(this, formatter);
|
2014-02-24 22:31:41 +00:00
|
|
|
this._name = "ConsoleAppender";
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
2007-10-11 00:08:58 +00:00
|
|
|
ConsoleAppender.prototype = {
|
2008-11-03 22:48:53 +00:00
|
|
|
__proto__: Appender.prototype,
|
|
|
|
|
2014-04-29 14:09:25 +00:00
|
|
|
// XXX this should be replaced with calls to the Browser Console
|
|
|
|
append: function App_append(message) {
|
|
|
|
if (message) {
|
|
|
|
let m = this._formatter.format(message);
|
|
|
|
if (message.level > Log.Level.Warn) {
|
|
|
|
Cu.reportError(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.doAppend(m);
|
2007-10-11 00:08:58 +00:00
|
|
|
}
|
2014-04-29 14:09:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
doAppend: function CApp_doAppend(formatted) {
|
2007-10-11 00:08:58 +00:00
|
|
|
Cc["@mozilla.org/consoleservice;1"].
|
2014-04-29 14:09:25 +00:00
|
|
|
getService(Ci.nsIConsoleService).logStringMessage(formatted);
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-13 18:39:37 +00:00
|
|
|
/**
|
2013-06-20 21:00:14 +00:00
|
|
|
* Append to an nsIStorageStream
|
2012-04-06 06:26:06 +00:00
|
|
|
*
|
2013-06-20 21:00:14 +00:00
|
|
|
* This writes logging output to an in-memory stream which can later be read
|
|
|
|
* back as an nsIInputStream. It can be used to avoid expensive I/O operations
|
|
|
|
* during logging. Instead, one can periodically consume the input stream and
|
|
|
|
* e.g. write it to disk asynchronously.
|
2007-10-10 09:09:28 +00:00
|
|
|
*/
|
2013-06-20 21:00:14 +00:00
|
|
|
function StorageStreamAppender(formatter) {
|
2011-06-13 18:39:37 +00:00
|
|
|
Appender.call(this, formatter);
|
2014-02-24 22:31:41 +00:00
|
|
|
this._name = "StorageStreamAppender";
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
2013-06-20 21:00:14 +00:00
|
|
|
|
|
|
|
StorageStreamAppender.prototype = {
|
2008-11-03 22:48:53 +00:00
|
|
|
__proto__: Appender.prototype,
|
2011-06-13 18:39:37 +00:00
|
|
|
|
|
|
|
_converterStream: null, // holds the nsIConverterOutputStream
|
|
|
|
_outputStream: null, // holds the underlying nsIOutputStream
|
|
|
|
|
2013-06-20 21:00:14 +00:00
|
|
|
_ss: null,
|
|
|
|
|
2011-06-13 18:39:37 +00:00
|
|
|
get outputStream() {
|
|
|
|
if (!this._outputStream) {
|
|
|
|
// First create a raw stream. We can bail out early if that fails.
|
|
|
|
this._outputStream = this.newOutputStream();
|
|
|
|
if (!this._outputStream) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the raw stream in an nsIConverterOutputStream. We can reuse
|
|
|
|
// the instance if we already have one.
|
|
|
|
if (!this._converterStream) {
|
|
|
|
this._converterStream = Cc["@mozilla.org/intl/converter-output-stream;1"]
|
|
|
|
.createInstance(Ci.nsIConverterOutputStream);
|
|
|
|
}
|
|
|
|
this._converterStream.init(
|
|
|
|
this._outputStream, "UTF-8", STREAM_SEGMENT_SIZE,
|
2012-04-06 06:26:06 +00:00
|
|
|
Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
|
2011-06-13 18:39:37 +00:00
|
|
|
}
|
|
|
|
return this._converterStream;
|
2007-10-11 00:08:58 +00:00
|
|
|
},
|
|
|
|
|
2011-06-13 18:39:37 +00:00
|
|
|
newOutputStream: function newOutputStream() {
|
2013-06-20 21:00:14 +00:00
|
|
|
let ss = this._ss = Cc["@mozilla.org/storagestream;1"]
|
|
|
|
.createInstance(Ci.nsIStorageStream);
|
|
|
|
ss.init(STREAM_SEGMENT_SIZE, PR_UINT32_MAX, null);
|
|
|
|
return ss.getOutputStream(0);
|
|
|
|
},
|
|
|
|
|
|
|
|
getInputStream: function getInputStream() {
|
|
|
|
if (!this._ss) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this._ss.newInputStream(0);
|
2011-06-13 18:39:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
reset: function reset() {
|
|
|
|
if (!this._outputStream) {
|
|
|
|
return;
|
2009-05-26 18:51:29 +00:00
|
|
|
}
|
2011-06-13 18:39:37 +00:00
|
|
|
this.outputStream.close();
|
|
|
|
this._outputStream = null;
|
2013-06-20 21:00:14 +00:00
|
|
|
this._ss = null;
|
2007-10-11 00:08:58 +00:00
|
|
|
},
|
|
|
|
|
2016-11-11 23:22:34 +00:00
|
|
|
doAppend: function(formatted) {
|
2014-04-29 14:09:25 +00:00
|
|
|
if (!formatted) {
|
2007-10-11 00:08:58 +00:00
|
|
|
return;
|
2011-06-13 18:39:37 +00:00
|
|
|
}
|
2007-10-11 00:08:58 +00:00
|
|
|
try {
|
2014-04-29 14:09:25 +00:00
|
|
|
this.outputStream.writeString(formatted + "\n");
|
2016-08-04 07:28:58 +00:00
|
|
|
} catch (ex) {
|
2011-06-13 18:39:37 +00:00
|
|
|
if (ex.result == Cr.NS_BASE_STREAM_CLOSED) {
|
|
|
|
// The underlying output stream is closed, so let's open a new one
|
|
|
|
// and try again.
|
|
|
|
this._outputStream = null;
|
2013-06-20 21:00:14 +00:00
|
|
|
} try {
|
2014-04-29 14:09:25 +00:00
|
|
|
this.outputStream.writeString(formatted + "\n");
|
2013-06-20 21:00:14 +00:00
|
|
|
} catch (ex) {
|
|
|
|
// Ah well, we tried, but something seems to be hosed permanently.
|
2011-06-13 18:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-06-20 21:00:14 +00:00
|
|
|
* File appender
|
2012-04-06 06:26:06 +00:00
|
|
|
*
|
2013-06-20 21:00:14 +00:00
|
|
|
* Writes output to file using OS.File.
|
2011-06-13 18:39:37 +00:00
|
|
|
*/
|
2013-06-20 21:00:14 +00:00
|
|
|
function FileAppender(path, formatter) {
|
2014-02-24 22:31:41 +00:00
|
|
|
Appender.call(this, formatter);
|
2013-06-20 21:00:14 +00:00
|
|
|
this._name = "FileAppender";
|
|
|
|
this._encoder = new TextEncoder();
|
|
|
|
this._path = path;
|
|
|
|
this._file = null;
|
|
|
|
this._fileReadyPromise = null;
|
|
|
|
|
|
|
|
// This is a promise exposed for testing/debugging the logger itself.
|
|
|
|
this._lastWritePromise = null;
|
2011-06-13 18:39:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 21:00:14 +00:00
|
|
|
FileAppender.prototype = {
|
|
|
|
__proto__: Appender.prototype,
|
2011-06-13 18:39:37 +00:00
|
|
|
|
2016-11-11 23:22:34 +00:00
|
|
|
_openFile: function() {
|
2015-12-03 17:58:56 +00:00
|
|
|
return Task.spawn(function* _openFile() {
|
2013-06-20 21:00:14 +00:00
|
|
|
try {
|
|
|
|
this._file = yield OS.File.open(this._path,
|
|
|
|
{truncate: true});
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof OS.File.Error) {
|
|
|
|
this._file = null;
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2007-10-11 00:08:58 +00:00
|
|
|
},
|
|
|
|
|
2013-06-20 21:00:14 +00:00
|
|
|
_getFile: function() {
|
|
|
|
if (!this._fileReadyPromise) {
|
|
|
|
this._fileReadyPromise = this._openFile();
|
|
|
|
}
|
2011-06-13 18:39:37 +00:00
|
|
|
|
2014-05-16 23:45:04 +00:00
|
|
|
return this._fileReadyPromise;
|
2013-06-20 21:00:14 +00:00
|
|
|
},
|
2011-06-13 18:39:37 +00:00
|
|
|
|
2016-11-11 23:22:34 +00:00
|
|
|
doAppend: function(formatted) {
|
2014-04-29 14:09:25 +00:00
|
|
|
let array = this._encoder.encode(formatted + "\n");
|
2013-06-20 21:00:14 +00:00
|
|
|
if (this._file) {
|
|
|
|
this._lastWritePromise = this._file.write(array);
|
|
|
|
} else {
|
|
|
|
this._lastWritePromise = this._getFile().then(_ => {
|
|
|
|
this._fileReadyPromise = null;
|
|
|
|
if (this._file) {
|
|
|
|
return this._file.write(array);
|
|
|
|
}
|
2016-04-05 18:33:48 +00:00
|
|
|
return undefined;
|
2013-06-20 21:00:14 +00:00
|
|
|
});
|
2007-10-11 00:08:58 +00:00
|
|
|
}
|
2008-03-29 02:36:11 +00:00
|
|
|
},
|
|
|
|
|
2016-11-11 23:22:34 +00:00
|
|
|
reset: function() {
|
2013-06-20 21:00:14 +00:00
|
|
|
let fileClosePromise = this._file.close();
|
|
|
|
return fileClosePromise.then(_ => {
|
|
|
|
this._file = null;
|
|
|
|
return OS.File.remove(this._path);
|
|
|
|
});
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-13 18:39:37 +00:00
|
|
|
/**
|
2013-06-20 21:00:14 +00:00
|
|
|
* Bounded File appender
|
2012-04-06 06:26:06 +00:00
|
|
|
*
|
2013-06-20 21:00:14 +00:00
|
|
|
* Writes output to file using OS.File. After the total message size
|
2014-04-29 14:09:25 +00:00
|
|
|
* (as defined by formatted.length) exceeds maxSize, existing messages
|
2013-06-20 21:00:14 +00:00
|
|
|
* will be discarded, and subsequent writes will be appended to a new log file.
|
2007-10-10 09:09:28 +00:00
|
|
|
*/
|
2016-11-10 22:48:04 +00:00
|
|
|
function BoundedFileAppender(path, formatter, maxSize = 2 * ONE_MEGABYTE) {
|
2014-02-24 22:31:41 +00:00
|
|
|
FileAppender.call(this, path, formatter);
|
2013-06-20 21:00:14 +00:00
|
|
|
this._name = "BoundedFileAppender";
|
|
|
|
this._size = 0;
|
2007-10-10 09:09:28 +00:00
|
|
|
this._maxSize = maxSize;
|
2013-06-20 21:00:14 +00:00
|
|
|
this._closeFilePromise = null;
|
2007-10-10 09:09:28 +00:00
|
|
|
}
|
2008-11-03 22:48:53 +00:00
|
|
|
|
2013-06-20 21:00:14 +00:00
|
|
|
BoundedFileAppender.prototype = {
|
|
|
|
__proto__: FileAppender.prototype,
|
2007-10-11 00:08:58 +00:00
|
|
|
|
2016-11-11 23:22:34 +00:00
|
|
|
doAppend: function(formatted) {
|
2013-06-20 21:00:14 +00:00
|
|
|
if (!this._removeFilePromise) {
|
|
|
|
if (this._size < this._maxSize) {
|
2014-04-29 14:09:25 +00:00
|
|
|
this._size += formatted.length;
|
|
|
|
return FileAppender.prototype.doAppend.call(this, formatted);
|
2011-06-13 18:39:37 +00:00
|
|
|
}
|
2013-06-20 21:00:14 +00:00
|
|
|
this._removeFilePromise = this.reset();
|
2007-10-11 00:08:58 +00:00
|
|
|
}
|
2013-06-20 21:00:14 +00:00
|
|
|
this._removeFilePromise.then(_ => {
|
|
|
|
this._removeFilePromise = null;
|
2014-04-29 14:09:25 +00:00
|
|
|
this.doAppend(formatted);
|
2013-06-20 21:00:14 +00:00
|
|
|
});
|
2016-04-05 18:33:48 +00:00
|
|
|
return undefined;
|
2013-06-20 21:00:14 +00:00
|
|
|
},
|
2007-10-10 09:09:28 +00:00
|
|
|
|
2016-11-11 23:22:34 +00:00
|
|
|
reset: function() {
|
2013-06-20 21:00:14 +00:00
|
|
|
let fileClosePromise;
|
|
|
|
if (this._fileReadyPromise) {
|
|
|
|
// An attempt to open the file may still be in progress.
|
|
|
|
fileClosePromise = this._fileReadyPromise.then(_ => {
|
|
|
|
return this._file.close();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
fileClosePromise = this._file.close();
|
2011-06-13 18:39:37 +00:00
|
|
|
}
|
2007-10-10 09:09:28 +00:00
|
|
|
|
2013-06-20 21:00:14 +00:00
|
|
|
return fileClosePromise.then(_ => {
|
|
|
|
this._size = 0;
|
|
|
|
this._file = null;
|
|
|
|
return OS.File.remove(this._path);
|
|
|
|
});
|
2007-10-11 00:08:58 +00:00
|
|
|
}
|
2007-10-10 09:09:28 +00:00
|
|
|
};
|
2013-06-20 21:00:14 +00:00
|
|
|
|