2007-10-10 13:09:28 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2007-10-11 04:12:20 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2007-10-10 13:09:28 +04:00
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
2011-06-13 22:39:37 +04:00
|
|
|
* The Original Code is log4moz.
|
2007-10-10 13:09:28 +04:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2011-06-13 22:39:37 +04:00
|
|
|
* the Mozilla Foundation.
|
2007-10-10 13:09:28 +04:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2011-06-13 22:39:37 +04:00
|
|
|
* Michael Johnston <special.michael@gmail.com> (Original Author)
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
|
|
|
* Philipp von Weitershausen <philipp@weitershausen.de>
|
2007-10-10 13:09:28 +04:00
|
|
|
*
|
2007-10-11 04:12:20 +04:00
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
2007-10-10 13:09:28 +04:00
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2007-12-15 05:07:25 +03:00
|
|
|
const EXPORTED_SYMBOLS = ['Log4Moz'];
|
|
|
|
|
2007-10-10 13:09:28 +04:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2008-01-25 04:41:36 +03:00
|
|
|
const ONE_BYTE = 1;
|
|
|
|
const ONE_KILOBYTE = 1024 * ONE_BYTE;
|
|
|
|
const ONE_MEGABYTE = 1024 * ONE_KILOBYTE;
|
2007-12-15 05:07:25 +03:00
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
const STREAM_SEGMENT_SIZE = 4096;
|
|
|
|
const PR_UINT32_MAX = 0xffffffff;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
|
|
|
|
2008-03-25 23:55:34 +03:00
|
|
|
let Log4Moz = {
|
|
|
|
Level: {
|
|
|
|
Fatal: 70,
|
|
|
|
Error: 60,
|
|
|
|
Warn: 50,
|
|
|
|
Info: 40,
|
|
|
|
Config: 30,
|
|
|
|
Debug: 20,
|
|
|
|
Trace: 10,
|
|
|
|
All: 0,
|
|
|
|
Desc: {
|
|
|
|
70: "FATAL",
|
|
|
|
60: "ERROR",
|
|
|
|
50: "WARN",
|
|
|
|
40: "INFO",
|
|
|
|
30: "CONFIG",
|
|
|
|
20: "DEBUG",
|
|
|
|
10: "TRACE",
|
|
|
|
0: "ALL"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-11-04 01:48:53 +03:00
|
|
|
get repository() {
|
|
|
|
delete Log4Moz.repository;
|
|
|
|
Log4Moz.repository = new LoggerRepository();
|
|
|
|
return Log4Moz.repository;
|
2008-05-30 04:56:52 +04:00
|
|
|
},
|
2008-11-04 01:48:53 +03:00
|
|
|
set repository(value) {
|
|
|
|
delete Log4Moz.repository;
|
|
|
|
Log4Moz.repository = value;
|
|
|
|
},
|
|
|
|
|
2011-06-13 22:39:14 +04:00
|
|
|
LogMessage: LogMessage,
|
|
|
|
Logger: Logger,
|
|
|
|
LoggerRepository: LoggerRepository,
|
2008-05-30 04:56:52 +04:00
|
|
|
|
2011-06-13 22:39:14 +04:00
|
|
|
Formatter: Formatter,
|
|
|
|
BasicFormatter: BasicFormatter,
|
2008-11-04 01:48:53 +03:00
|
|
|
|
2011-06-13 22:39:14 +04:00
|
|
|
Appender: Appender,
|
|
|
|
DumpAppender: DumpAppender,
|
|
|
|
ConsoleAppender: ConsoleAppender,
|
2011-06-13 22:39:37 +04:00
|
|
|
BlockingStreamAppender: BlockingStreamAppender,
|
|
|
|
StorageStreamAppender: StorageStreamAppender,
|
|
|
|
|
|
|
|
// Discouraged due to blocking I/O.
|
2011-06-13 22:39:14 +04:00
|
|
|
FileAppender: FileAppender,
|
|
|
|
RotatingFileAppender: RotatingFileAppender,
|
2008-08-20 04:28:02 +04:00
|
|
|
|
|
|
|
// Logging helper:
|
2008-11-04 01:48:53 +03:00
|
|
|
// let logger = Log4Moz.repository.getLogger("foo");
|
2008-08-20 04:28:02 +04:00
|
|
|
// logger.info(Log4Moz.enumerateInterfaces(someObject).join(","));
|
2008-11-04 01:48:53 +03:00
|
|
|
enumerateInterfaces: function Log4Moz_enumerateInterfaces(aObject) {
|
2008-08-20 04:28:02 +04:00
|
|
|
let interfaces = [];
|
|
|
|
|
|
|
|
for (i in Ci) {
|
|
|
|
try {
|
|
|
|
aObject.QueryInterface(Ci[i]);
|
|
|
|
interfaces.push(i);
|
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return interfaces;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Logging helper:
|
2008-11-04 01:48:53 +03:00
|
|
|
// let logger = Log4Moz.repository.getLogger("foo");
|
2008-08-20 04:28:02 +04:00
|
|
|
// logger.info(Log4Moz.enumerateProperties(someObject).join(","));
|
2008-11-04 01:48:53 +03:00
|
|
|
enumerateProperties: function Log4Moz_enumerateProps(aObject,
|
|
|
|
aExcludeComplexTypes) {
|
2008-08-20 04:28:02 +04:00
|
|
|
let properties = [];
|
|
|
|
|
|
|
|
for (p in aObject) {
|
|
|
|
try {
|
|
|
|
if (aExcludeComplexTypes &&
|
|
|
|
(typeof aObject[p] == "object" || typeof aObject[p] == "function"))
|
|
|
|
continue;
|
|
|
|
properties.push(p + " = " + aObject[p]);
|
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
properties.push(p + " = " + ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return properties;
|
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
};
|
|
|
|
|
2008-03-25 23:55:34 +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
|
|
|
*/
|
2007-12-11 06:47:11 +03:00
|
|
|
function LogMessage(loggerName, level, message){
|
|
|
|
this.loggerName = loggerName;
|
|
|
|
this.message = message;
|
|
|
|
this.level = level;
|
|
|
|
this.time = Date.now();
|
|
|
|
}
|
|
|
|
LogMessage.prototype = {
|
|
|
|
get levelDesc() {
|
2007-12-15 05:07:25 +03:00
|
|
|
if (this.level in Log4Moz.Level.Desc)
|
|
|
|
return Log4Moz.Level.Desc[this.level];
|
2007-12-11 06:47:11 +03:00
|
|
|
return "UNKNOWN";
|
|
|
|
},
|
|
|
|
|
|
|
|
toString: function LogMsg_toString(){
|
2008-07-12 01:47:15 +04:00
|
|
|
return "LogMessage [" + this.time + " " + this.level + " " +
|
2007-12-11 06:47:11 +03:00
|
|
|
this.message + "]";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Logger
|
|
|
|
* Hierarchical version. Logs to all appenders, assigned or inherited
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Logger(name, repository) {
|
2010-02-12 02:25:31 +03:00
|
|
|
if (!repository)
|
|
|
|
repository = Log4Moz.repository;
|
|
|
|
this._name = name;
|
2011-02-26 01:42:59 +03:00
|
|
|
this.children = [];
|
|
|
|
this.ownAppenders = [];
|
|
|
|
this.appenders = [];
|
2010-02-12 02:25:31 +03:00
|
|
|
this._repository = repository;
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2007-12-11 06:47:11 +03:00
|
|
|
Logger.prototype = {
|
2008-11-04 01:48:53 +03:00
|
|
|
get name() {
|
|
|
|
return this._name;
|
|
|
|
},
|
|
|
|
|
2007-12-11 06:47:11 +03:00
|
|
|
_level: null,
|
|
|
|
get level() {
|
|
|
|
if (this._level != null)
|
|
|
|
return this._level;
|
|
|
|
if (this.parent)
|
|
|
|
return this.parent.level;
|
|
|
|
dump("log4moz warning: root logger configuration error: no level defined\n");
|
2007-12-15 05:07:25 +03:00
|
|
|
return Log4Moz.Level.All;
|
2007-12-11 06:47:11 +03:00
|
|
|
},
|
|
|
|
set level(level) {
|
|
|
|
this._level = level;
|
2007-10-10 13:09:28 +04:00
|
|
|
},
|
|
|
|
|
2011-02-26 01:42:59 +03:00
|
|
|
_parent: null,
|
|
|
|
get parent() this._parent,
|
|
|
|
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 13:09:28 +04:00
|
|
|
},
|
|
|
|
|
2007-12-11 06:47:11 +03:00
|
|
|
addAppender: function Logger_addAppender(appender) {
|
2011-02-26 01:42:59 +03:00
|
|
|
if (this.ownAppenders.indexOf(appender) != -1) {
|
|
|
|
return;
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2011-02-26 01:42:59 +03:00
|
|
|
this.ownAppenders.push(appender);
|
|
|
|
this.updateAppenders();
|
2007-10-10 13:09:28 +04:00
|
|
|
},
|
|
|
|
|
2009-02-20 12:54:45 +03:00
|
|
|
removeAppender: function Logger_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();
|
2009-02-20 12:54:45 +03:00
|
|
|
},
|
|
|
|
|
2011-02-26 01:42:59 +03:00
|
|
|
log: function Logger_log(level, string) {
|
|
|
|
if (this.level > level)
|
2007-10-10 13:09:28 +04:00
|
|
|
return;
|
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;
|
|
|
|
for (let i = 0; i < appenders.length; i++){
|
2011-02-26 01:42:59 +03:00
|
|
|
let appender = appenders[i];
|
|
|
|
if (appender.level > level)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!message)
|
|
|
|
message = new LogMessage(this._name, level, string);
|
|
|
|
|
|
|
|
appender.append(message);
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-12-11 06:47:11 +03:00
|
|
|
fatal: function Logger_fatal(string) {
|
2011-02-26 01:42:59 +03:00
|
|
|
this.log(Log4Moz.Level.Fatal, string);
|
2007-12-11 06:47:11 +03:00
|
|
|
},
|
|
|
|
error: function Logger_error(string) {
|
2011-02-26 01:42:59 +03:00
|
|
|
this.log(Log4Moz.Level.Error, string);
|
2007-12-11 06:47:11 +03:00
|
|
|
},
|
|
|
|
warn: function Logger_warn(string) {
|
2011-02-26 01:42:59 +03:00
|
|
|
this.log(Log4Moz.Level.Warn, string);
|
2007-12-11 06:47:11 +03:00
|
|
|
},
|
|
|
|
info: function Logger_info(string) {
|
2011-02-26 01:42:59 +03:00
|
|
|
this.log(Log4Moz.Level.Info, string);
|
2007-12-11 06:47:11 +03:00
|
|
|
},
|
|
|
|
config: function Logger_config(string) {
|
2011-02-26 01:42:59 +03:00
|
|
|
this.log(Log4Moz.Level.Config, string);
|
2007-12-11 06:47:11 +03:00
|
|
|
},
|
|
|
|
debug: function Logger_debug(string) {
|
2011-02-26 01:42:59 +03:00
|
|
|
this.log(Log4Moz.Level.Debug, string);
|
2007-12-11 06:47:11 +03:00
|
|
|
},
|
|
|
|
trace: function Logger_trace(string) {
|
2011-02-26 01:42:59 +03:00
|
|
|
this.log(Log4Moz.Level.Trace, string);
|
2007-10-10 13:09:28 +04: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);
|
2007-12-15 05:07:25 +03:00
|
|
|
this._rootLogger.level = Log4Moz.Level.All;
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
|
|
|
return this._rootLogger;
|
|
|
|
},
|
2011-02-26 01:42:59 +03:00
|
|
|
set rootLogger(logger) {
|
|
|
|
throw "Cannot change the root logger";
|
|
|
|
},
|
2007-10-10 13:09:28 +04:00
|
|
|
|
|
|
|
_updateParents: function LogRep__updateParents(name) {
|
|
|
|
let pieces = name.split('.');
|
|
|
|
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)
|
|
|
|
cur += '.' + pieces[i];
|
|
|
|
else
|
|
|
|
cur = pieces[i];
|
|
|
|
if (cur in this._loggers)
|
|
|
|
parent = cur;
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getLogger: function LogRep_getLogger(name) {
|
2008-11-04 01:48:53 +03:00
|
|
|
if (name in this._loggers)
|
|
|
|
return this._loggers[name];
|
|
|
|
this._loggers[name] = new Logger(name, this);
|
|
|
|
this._updateParents(name);
|
2007-10-10 13:09:28 +04:00
|
|
|
return this._loggers[name];
|
|
|
|
}
|
2007-12-11 06:47:11 +03:00
|
|
|
};
|
2007-10-10 13:09:28 +04:00
|
|
|
|
|
|
|
/*
|
2007-12-11 06:47:11 +03:00
|
|
|
* Formatters
|
|
|
|
* These massage a LogMessage into whatever output is desired
|
|
|
|
* Only the BasicFormatter is currently implemented
|
2007-10-10 13:09:28 +04:00
|
|
|
*/
|
|
|
|
|
2007-12-11 06:47:11 +03:00
|
|
|
// Abstract formatter
|
|
|
|
function Formatter() {}
|
|
|
|
Formatter.prototype = {
|
|
|
|
format: function Formatter_format(message) {}
|
|
|
|
};
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2011-02-26 01:42:59 +03:00
|
|
|
// Basic formatter that doesn't do anything fancy
|
2007-12-11 06:47:11 +03:00
|
|
|
function BasicFormatter(dateFormat) {
|
|
|
|
if (dateFormat)
|
|
|
|
this.dateFormat = dateFormat;
|
|
|
|
}
|
|
|
|
BasicFormatter.prototype = {
|
2008-11-04 01:48:53 +03:00
|
|
|
__proto__: Formatter.prototype,
|
|
|
|
|
2007-12-11 06:47:11 +03:00
|
|
|
format: function BF_format(message) {
|
2011-02-26 01:42:59 +03:00
|
|
|
return message.time + "\t" + message.loggerName + "\t" + message.levelDesc
|
|
|
|
+ "\t" + message.message + "\n";
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Appender(formatter) {
|
|
|
|
this._name = "Appender";
|
|
|
|
this._formatter = formatter? formatter : new BasicFormatter();
|
|
|
|
}
|
|
|
|
Appender.prototype = {
|
2011-02-26 01:42:59 +03:00
|
|
|
level: Log4Moz.Level.All,
|
2007-10-10 13:09:28 +04:00
|
|
|
|
|
|
|
append: function App_append(message) {
|
2011-02-26 01:42:59 +03:00
|
|
|
this.doAppend(this._formatter.format(message));
|
2007-10-10 13:09:28 +04:00
|
|
|
},
|
|
|
|
toString: function App_toString() {
|
|
|
|
return this._name + " [level=" + this._level +
|
|
|
|
", formatter=" + this._formatter + "]";
|
|
|
|
},
|
|
|
|
doAppend: function App_doAppend(message) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DumpAppender
|
|
|
|
* Logs to standard out
|
|
|
|
*/
|
|
|
|
|
|
|
|
function DumpAppender(formatter) {
|
|
|
|
this._name = "DumpAppender";
|
2011-06-13 22:39:37 +04:00
|
|
|
Appender.call(this, formatter);
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2007-10-11 04:08:58 +04:00
|
|
|
DumpAppender.prototype = {
|
2008-11-04 01:48:53 +03:00
|
|
|
__proto__: Appender.prototype,
|
|
|
|
|
2007-10-11 04:08:58 +04:00
|
|
|
doAppend: function DApp_doAppend(message) {
|
|
|
|
dump(message);
|
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ConsoleAppender
|
|
|
|
* Logs to the javascript console
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ConsoleAppender(formatter) {
|
|
|
|
this._name = "ConsoleAppender";
|
2011-06-13 22:39:37 +04:00
|
|
|
Appender.call(this, formatter);
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2007-10-11 04:08:58 +04:00
|
|
|
ConsoleAppender.prototype = {
|
2008-11-04 01:48:53 +03:00
|
|
|
__proto__: Appender.prototype,
|
|
|
|
|
2007-10-11 04:08:58 +04:00
|
|
|
doAppend: function CApp_doAppend(message) {
|
2007-12-15 05:07:25 +03:00
|
|
|
if (message.level > Log4Moz.Level.Warn) {
|
2007-10-11 04:08:58 +04:00
|
|
|
Cu.reportError(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Cc["@mozilla.org/consoleservice;1"].
|
|
|
|
getService(Ci.nsIConsoleService).logStringMessage(message);
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
/**
|
|
|
|
* Base implementation for stream based appenders.
|
|
|
|
*
|
|
|
|
* Caution: This writes to the output stream synchronously, thus logging calls
|
|
|
|
* block as the data is written to the stream. This can have negligible impact
|
|
|
|
* for in-memory streams, but should be taken into account for I/O streams
|
|
|
|
* (files, network, etc.)
|
2007-10-10 13:09:28 +04:00
|
|
|
*/
|
2011-06-13 22:39:37 +04:00
|
|
|
function BlockingStreamAppender(formatter) {
|
|
|
|
this._name = "BlockingStreamAppender";
|
|
|
|
Appender.call(this, formatter);
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
2011-06-13 22:39:37 +04:00
|
|
|
BlockingStreamAppender.prototype = {
|
2008-11-04 01:48:53 +03:00
|
|
|
__proto__: Appender.prototype,
|
2011-06-13 22:39:37 +04:00
|
|
|
|
|
|
|
_converterStream: null, // holds the nsIConverterOutputStream
|
|
|
|
_outputStream: null, // holds the underlying nsIOutputStream
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output stream to write to.
|
|
|
|
*
|
|
|
|
* This will automatically open the stream if it doesn't exist yet by
|
|
|
|
* calling newOutputStream. The resulting raw stream is wrapped in a
|
|
|
|
* nsIConverterOutputStream to ensure text is written as UTF-8.
|
|
|
|
*/
|
|
|
|
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,
|
|
|
|
Ci.nsIConverterOutputStream.DEFAULT_REPLACEMENT_CHARACTER);
|
|
|
|
}
|
|
|
|
return this._converterStream;
|
2007-10-11 04:08:58 +04:00
|
|
|
},
|
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
newOutputStream: function newOutputStream() {
|
|
|
|
throw "Stream-based appenders need to implement newOutputStream()!";
|
|
|
|
},
|
|
|
|
|
|
|
|
reset: function reset() {
|
|
|
|
if (!this._outputStream) {
|
|
|
|
return;
|
2009-05-26 22:51:29 +04:00
|
|
|
}
|
2011-06-13 22:39:37 +04:00
|
|
|
this.outputStream.close();
|
|
|
|
this._outputStream = null;
|
2007-10-11 04:08:58 +04:00
|
|
|
},
|
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
doAppend: function doAppend(message) {
|
|
|
|
if (!message) {
|
2007-10-11 04:08:58 +04:00
|
|
|
return;
|
2011-06-13 22:39:37 +04:00
|
|
|
}
|
2007-10-11 04:08:58 +04:00
|
|
|
try {
|
2011-06-13 22:39:37 +04:00
|
|
|
this.outputStream.writeString(message);
|
|
|
|
} catch(ex) {
|
|
|
|
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;
|
|
|
|
try {
|
|
|
|
this.outputStream.writeString(message);
|
|
|
|
} catch (ex) {
|
|
|
|
// Ah well, we tried, but something seems to be hosed permanently.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append to an nsIStorageStream
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
function StorageStreamAppender(formatter) {
|
|
|
|
this._name = "StorageStreamAppender";
|
|
|
|
BlockingStreamAppender.call(this, formatter);
|
|
|
|
}
|
|
|
|
StorageStreamAppender.prototype = {
|
|
|
|
__proto__: BlockingStreamAppender.prototype,
|
|
|
|
|
|
|
|
_ss: null,
|
|
|
|
newOutputStream: function newOutputStream() {
|
|
|
|
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;
|
2007-10-11 04:08:58 +04:00
|
|
|
}
|
2011-06-13 22:39:37 +04:00
|
|
|
return this._ss.newInputStream(0);
|
2007-10-11 04:08:58 +04:00
|
|
|
},
|
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
reset: function reset() {
|
|
|
|
BlockingStreamAppender.prototype.reset.call(this);
|
|
|
|
this._ss = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File appender (discouraged)
|
|
|
|
*
|
|
|
|
* Writes otuput to a file using a regular nsIFileOutputStream (as opposed
|
|
|
|
* to nsISafeFileOutputStream, since immediate durability is typically not
|
|
|
|
* needed for logs.) Note that I/O operations block the logging caller.
|
|
|
|
*/
|
|
|
|
function FileAppender(file, formatter) {
|
|
|
|
this._name = "FileAppender";
|
|
|
|
this._file = file; // nsIFile
|
|
|
|
BlockingStreamAppender.call(this, formatter);
|
|
|
|
}
|
|
|
|
FileAppender.prototype = {
|
|
|
|
__proto__: BlockingStreamAppender.prototype,
|
|
|
|
|
|
|
|
newOutputStream: function newOutputStream() {
|
2007-10-11 04:08:58 +04:00
|
|
|
try {
|
2011-06-13 22:39:37 +04:00
|
|
|
return FileUtils.openFileOutputStream(this._file);
|
2007-10-11 04:08:58 +04:00
|
|
|
} catch(e) {
|
2011-06-13 22:39:37 +04:00
|
|
|
return null;
|
2007-10-11 04:08:58 +04:00
|
|
|
}
|
2008-03-29 05:36:11 +03:00
|
|
|
},
|
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
reset: function reset() {
|
|
|
|
BlockingStreamAppender.prototype.reset.call(this);
|
2009-02-24 03:55:41 +03:00
|
|
|
try {
|
|
|
|
this._file.remove(false);
|
|
|
|
} catch (e) {
|
2011-06-13 22:39:37 +04:00
|
|
|
// File didn't exist in the first place, or we're on Windows. Meh.
|
2009-02-24 03:55:41 +03:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
/**
|
|
|
|
* Rotating file appender (discouraged)
|
|
|
|
*
|
|
|
|
* Similar to FileAppender, but rotates logs when they become too large.
|
2007-10-10 13:09:28 +04:00
|
|
|
*/
|
|
|
|
function RotatingFileAppender(file, formatter, maxSize, maxBackups) {
|
2008-05-30 05:15:50 +04:00
|
|
|
if (maxSize === undefined)
|
|
|
|
maxSize = ONE_MEGABYTE * 2;
|
|
|
|
|
|
|
|
if (maxBackups === undefined)
|
|
|
|
maxBackups = 0;
|
|
|
|
|
2007-10-10 13:09:28 +04:00
|
|
|
this._name = "RotatingFileAppender";
|
2011-06-13 22:39:37 +04:00
|
|
|
FileAppender.call(this, file, formatter);
|
2007-10-10 13:09:28 +04:00
|
|
|
this._maxSize = maxSize;
|
|
|
|
this._maxBackups = maxBackups;
|
|
|
|
}
|
2007-10-11 04:08:58 +04:00
|
|
|
RotatingFileAppender.prototype = {
|
2008-11-04 01:48:53 +03:00
|
|
|
__proto__: FileAppender.prototype,
|
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
doAppend: function doAppend(message) {
|
|
|
|
FileAppender.prototype.doAppend.call(this, message);
|
2007-10-11 04:08:58 +04:00
|
|
|
try {
|
|
|
|
this.rotateLogs();
|
|
|
|
} catch(e) {
|
2009-05-26 22:51:29 +04:00
|
|
|
dump("Error writing file:" + e + "\n");
|
2007-10-11 04:08:58 +04:00
|
|
|
}
|
|
|
|
},
|
2009-05-26 22:51:29 +04:00
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
rotateLogs: function rotateLogs() {
|
|
|
|
if (this._file.exists() && this._file.fileSize < this._maxSize) {
|
2007-10-11 04:08:58 +04:00
|
|
|
return;
|
2011-06-13 22:39:37 +04:00
|
|
|
}
|
2007-10-11 04:08:58 +04:00
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
BlockingStreamAppender.prototype.reset.call(this);
|
2007-10-11 04:08:58 +04:00
|
|
|
|
2011-06-13 22:39:37 +04:00
|
|
|
for (let i = this.maxBackups - 1; i > 0; i--) {
|
2007-10-11 04:08:58 +04:00
|
|
|
let backup = this._file.parent.clone();
|
|
|
|
backup.append(this._file.leafName + "." + i);
|
2011-06-13 22:39:37 +04:00
|
|
|
if (backup.exists()) {
|
2007-10-11 04:08:58 +04:00
|
|
|
backup.moveTo(this._file.parent, this._file.leafName + "." + (i + 1));
|
2011-06-13 22:39:37 +04:00
|
|
|
}
|
2007-10-11 04:08:58 +04:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2007-10-11 04:08:58 +04:00
|
|
|
let cur = this._file.clone();
|
2011-06-13 22:39:37 +04:00
|
|
|
if (cur.exists()) {
|
2007-10-11 04:08:58 +04:00
|
|
|
cur.moveTo(cur.parent, cur.leafName + ".1");
|
2011-06-13 22:39:37 +04:00
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
|
2007-10-11 04:08:58 +04:00
|
|
|
// Note: this._file still points to the same file
|
|
|
|
}
|
2007-10-10 13:09:28 +04:00
|
|
|
};
|