зеркало из https://github.com/mozilla/pjs.git
refactoring: remove log4moz component, use it as a js module instead
This commit is contained in:
Родитель
7e58799632
Коммит
391859079a
Двоичные данные
services/sync/ILog4MozService.xpt
Двоичные данные
services/sync/ILog4MozService.xpt
Двоичный файл не отображается.
|
@ -50,7 +50,6 @@ const MODE_TRUNCATE = 0x20;
|
|||
const PERMS_FILE = 0644;
|
||||
const PERMS_DIRECTORY = 0755;
|
||||
|
||||
// Note: these are also available in the idl
|
||||
const LEVEL_FATAL = 70;
|
||||
const LEVEL_ERROR = 60;
|
||||
const LEVEL_WARN = 50;
|
||||
|
@ -78,127 +77,30 @@ const ONE_MEGABYTE = 1024 * ONE_KILOBYTE;
|
|||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
|
||||
/*
|
||||
* LoggingService
|
||||
* LogMessage
|
||||
* Encapsulates a single log event's data
|
||||
*/
|
||||
|
||||
function Log4MozService() {
|
||||
this._repository = new LoggerRepository();
|
||||
function LogMessage(loggerName, level, message){
|
||||
this.loggerName = loggerName;
|
||||
this.message = message;
|
||||
this.level = level;
|
||||
this.time = Date.now();
|
||||
}
|
||||
Log4MozService.prototype = {
|
||||
classDescription: "Log4moz Logging Service",
|
||||
contractID: "@mozilla.org/log4moz/service;1",
|
||||
classID: Components.ID("{a60e50d7-90b8-4a12-ad0c-79e6a1896978}"),
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.ILog4MozService,
|
||||
Ci.nsISupports]),
|
||||
LogMessage.prototype = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), // Ci.ILogMessage,
|
||||
|
||||
get rootLogger() {
|
||||
return this._repository.rootLogger;
|
||||
get levelDesc() {
|
||||
if (this.level in LEVEL_DESC)
|
||||
return LEVEL_DESC[this.level];
|
||||
return "UNKNOWN";
|
||||
},
|
||||
|
||||
getLogger: function LogSvc_getLogger(name) {
|
||||
return this._repository.getLogger(name);
|
||||
},
|
||||
|
||||
newAppender: function LogSvc_newAppender(kind, formatter) {
|
||||
switch (kind) {
|
||||
case "dump":
|
||||
return new DumpAppender(formatter);
|
||||
case "console":
|
||||
return new ConsoleAppender(formatter);
|
||||
default:
|
||||
dump("log4moz: unknown appender kind: " + kind);
|
||||
return;
|
||||
}
|
||||
},
|
||||
|
||||
newFileAppender: function LogSvc_newAppender(kind, file, formatter) {
|
||||
switch (kind) {
|
||||
case "file":
|
||||
return new FileAppender(file, formatter);
|
||||
case "rotating":
|
||||
// FIXME: hardcoded constants
|
||||
return new RotatingFileAppender(file, formatter, ONE_MEGABYTE * 5, 0);
|
||||
default:
|
||||
dump("log4moz: unknown appender kind: " + kind);
|
||||
return;
|
||||
}
|
||||
},
|
||||
|
||||
newFormatter: function LogSvc_newFormatter(kind) {
|
||||
switch (kind) {
|
||||
case "basic":
|
||||
return new BasicFormatter();
|
||||
default:
|
||||
dump("log4moz: unknown formatter kind: " + kind);
|
||||
return;
|
||||
}
|
||||
toString: function LogMsg_toString(){
|
||||
return "LogMessage [" + this._date + " " + this.level + " " +
|
||||
this.message + "]";
|
||||
}
|
||||
};
|
||||
|
||||
function NSGetModule(compMgr, fileSpec) {
|
||||
return XPCOMUtils.generateModule([Log4MozService]);
|
||||
}
|
||||
|
||||
/*
|
||||
* LoggerRepository
|
||||
* Implements a hierarchy of Loggers
|
||||
*/
|
||||
|
||||
function LoggerRepository() {}
|
||||
LoggerRepository.prototype = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.ILoggerRepository, Ci.nsISupports]),
|
||||
|
||||
_loggers: {},
|
||||
|
||||
_rootLogger: null,
|
||||
get rootLogger() {
|
||||
if (!this._rootLogger) {
|
||||
this._rootLogger = new Logger("root", this);
|
||||
this._rootLogger.level = LEVEL_ALL;
|
||||
}
|
||||
return this._rootLogger;
|
||||
},
|
||||
// FIXME: need to update all parent values if we do this
|
||||
//set rootLogger(logger) {
|
||||
// this._rootLogger = logger;
|
||||
//},
|
||||
|
||||
_updateParents: function LogRep__updateParents(name) {
|
||||
let pieces = name.split('.');
|
||||
let cur, parent;
|
||||
|
||||
// find the closest parent
|
||||
for (let i = 0; i < pieces.length; i++) {
|
||||
if (cur)
|
||||
cur += '.' + pieces[i];
|
||||
else
|
||||
cur = pieces[i];
|
||||
if (cur in this._loggers)
|
||||
parent = cur;
|
||||
}
|
||||
|
||||
// if they are the same it has no parent
|
||||
if (parent == name)
|
||||
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) {
|
||||
if (logger != name && name.indexOf(logger) == 0)
|
||||
this._updateParents(logger);
|
||||
}
|
||||
},
|
||||
|
||||
getLogger: function LogRep_getLogger(name) {
|
||||
if (!(name in this._loggers)) {
|
||||
this._loggers[name] = new Logger(name, this);
|
||||
this._updateParents(name);
|
||||
}
|
||||
return this._loggers[name];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Logger
|
||||
* Hierarchical version. Logs to all appenders, assigned or inherited
|
||||
|
@ -210,7 +112,7 @@ function Logger(name, repository) {
|
|||
this._appenders = [];
|
||||
}
|
||||
Logger.prototype = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.ILogger, Ci.nsISupports]),
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), // Ci.ILogger,
|
||||
|
||||
parent: null,
|
||||
|
||||
|
@ -274,6 +176,106 @@ Logger.prototype = {
|
|||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* LoggerRepository
|
||||
* Implements a hierarchy of Loggers
|
||||
*/
|
||||
|
||||
function LoggerRepository() {}
|
||||
LoggerRepository.prototype = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), // Ci.ILoggerRepository,
|
||||
|
||||
_loggers: {},
|
||||
|
||||
_rootLogger: null,
|
||||
get rootLogger() {
|
||||
if (!this._rootLogger) {
|
||||
this._rootLogger = new Logger("root", this);
|
||||
this._rootLogger.level = LEVEL_ALL;
|
||||
}
|
||||
return this._rootLogger;
|
||||
},
|
||||
// FIXME: need to update all parent values if we do this
|
||||
//set rootLogger(logger) {
|
||||
// this._rootLogger = logger;
|
||||
//},
|
||||
|
||||
_updateParents: function LogRep__updateParents(name) {
|
||||
let pieces = name.split('.');
|
||||
let cur, parent;
|
||||
|
||||
// find the closest parent
|
||||
for (let i = 0; i < pieces.length; i++) {
|
||||
if (cur)
|
||||
cur += '.' + pieces[i];
|
||||
else
|
||||
cur = pieces[i];
|
||||
if (cur in this._loggers)
|
||||
parent = cur;
|
||||
}
|
||||
|
||||
// if they are the same it has no parent
|
||||
if (parent == name)
|
||||
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) {
|
||||
if (logger != name && name.indexOf(logger) == 0)
|
||||
this._updateParents(logger);
|
||||
}
|
||||
},
|
||||
|
||||
getLogger: function LogRep_getLogger(name) {
|
||||
if (!(name in this._loggers)) {
|
||||
this._loggers[name] = new Logger(name, this);
|
||||
this._updateParents(name);
|
||||
}
|
||||
return this._loggers[name];
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Formatters
|
||||
* These massage a LogMessage into whatever output is desired
|
||||
* Only the BasicFormatter is currently implemented
|
||||
*/
|
||||
|
||||
// Abstract formatter
|
||||
function Formatter() {}
|
||||
Formatter.prototype = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), // Ci.IFormatter,
|
||||
format: function Formatter_format(message) {}
|
||||
};
|
||||
|
||||
// FIXME: should allow for formatting the whole string, not just the date
|
||||
function BasicFormatter(dateFormat) {
|
||||
if (dateFormat)
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
BasicFormatter.prototype = {
|
||||
_dateFormat: null,
|
||||
|
||||
get dateFormat() {
|
||||
if (!this._dateFormat)
|
||||
this._dateFormat = "%Y-%m-%d %H:%M:%S";
|
||||
return this._dateFormat;
|
||||
},
|
||||
|
||||
set dateFormat(format) {
|
||||
this._dateFormat = format;
|
||||
},
|
||||
|
||||
format: function BF_format(message) {
|
||||
let date = new Date(message.time);
|
||||
return date.toLocaleFormat(this.dateFormat) + "\t" +
|
||||
message.loggerName + "\t" + message.levelDesc + "\t" +
|
||||
message.message + "\n";
|
||||
}
|
||||
};
|
||||
BasicFormatter.prototype.__proto__ = new Formatter();
|
||||
|
||||
/*
|
||||
* Appenders
|
||||
* These can be attached to Loggers to log to different places
|
||||
|
@ -285,7 +287,7 @@ function Appender(formatter) {
|
|||
this._formatter = formatter? formatter : new BasicFormatter();
|
||||
}
|
||||
Appender.prototype = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.IAppender, Ci.nsISupports]),
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), // Ci.IAppender,
|
||||
|
||||
_level: LEVEL_ALL,
|
||||
get level() { return this._level; },
|
||||
|
@ -434,66 +436,76 @@ RotatingFileAppender.prototype = {
|
|||
RotatingFileAppender.prototype.__proto__ = new FileAppender();
|
||||
|
||||
/*
|
||||
* Formatters
|
||||
* These massage a LogMessage into whatever output is desired
|
||||
* Only the BasicFormatter is currently implemented
|
||||
* LoggingService
|
||||
*/
|
||||
|
||||
// Abstract formatter
|
||||
function Formatter() {}
|
||||
Formatter.prototype = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.IFormatter, Ci.nsISupports]),
|
||||
format: function Formatter_format(message) {}
|
||||
};
|
||||
|
||||
// FIXME: should allow for formatting the whole string, not just the date
|
||||
function BasicFormatter(dateFormat) {
|
||||
if (dateFormat)
|
||||
this.dateFormat = dateFormat;
|
||||
function Log4MozService() {
|
||||
this._repository = new LoggerRepository();
|
||||
}
|
||||
BasicFormatter.prototype = {
|
||||
_dateFormat: null,
|
||||
Log4MozService.prototype = {
|
||||
//classDescription: "Log4moz Logging Service",
|
||||
//contractID: "@mozilla.org/log4moz/service;1",
|
||||
//classID: Components.ID("{a60e50d7-90b8-4a12-ad0c-79e6a1896978}"),
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]), //Ci.ILog4MozService,
|
||||
|
||||
get dateFormat() {
|
||||
if (!this._dateFormat)
|
||||
this._dateFormat = "%Y-%m-%d %H:%M:%S";
|
||||
return this._dateFormat;
|
||||
get rootLogger() {
|
||||
return this._repository.rootLogger;
|
||||
},
|
||||
|
||||
set dateFormat(format) {
|
||||
this._dateFormat = format;
|
||||
getLogger: function LogSvc_getLogger(name) {
|
||||
return this._repository.getLogger(name);
|
||||
},
|
||||
|
||||
format: function BF_format(message) {
|
||||
let date = new Date(message.time);
|
||||
return date.toLocaleFormat(this.dateFormat) + "\t" +
|
||||
message.loggerName + "\t" + message.levelDesc + "\t" +
|
||||
message.message + "\n";
|
||||
newAppender: function LogSvc_newAppender(kind, formatter) {
|
||||
switch (kind) {
|
||||
case "dump":
|
||||
return new DumpAppender(formatter);
|
||||
case "console":
|
||||
return new ConsoleAppender(formatter);
|
||||
default:
|
||||
dump("log4moz: unknown appender kind: " + kind);
|
||||
return;
|
||||
}
|
||||
},
|
||||
|
||||
newFileAppender: function LogSvc_newAppender(kind, file, formatter) {
|
||||
switch (kind) {
|
||||
case "file":
|
||||
return new FileAppender(file, formatter);
|
||||
case "rotating":
|
||||
// FIXME: hardcoded constants
|
||||
return new RotatingFileAppender(file, formatter, ONE_MEGABYTE * 5, 0);
|
||||
default:
|
||||
dump("log4moz: unknown appender kind: " + kind);
|
||||
return;
|
||||
}
|
||||
},
|
||||
|
||||
newFormatter: function LogSvc_newFormatter(kind) {
|
||||
switch (kind) {
|
||||
case "basic":
|
||||
return new BasicFormatter();
|
||||
default:
|
||||
dump("log4moz: unknown formatter kind: " + kind);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
BasicFormatter.prototype.__proto__ = new Formatter();
|
||||
|
||||
/*
|
||||
* LogMessage
|
||||
* Encapsulates a single log event's data
|
||||
*/
|
||||
function LogMessage(loggerName, level, message){
|
||||
this.loggerName = loggerName;
|
||||
this.message = message;
|
||||
this.level = level;
|
||||
this.time = Date.now();
|
||||
}
|
||||
LogMessage.prototype = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.ILogMessage, Ci.nsISupports]),
|
||||
const EXPORTED_SYMBOLS = ['Log4Moz'];
|
||||
|
||||
get levelDesc() {
|
||||
if (this.level in LEVEL_DESC)
|
||||
return LEVEL_DESC[this.level];
|
||||
return "UNKNOWN";
|
||||
},
|
||||
const Log4Moz = {};
|
||||
|
||||
toString: function LogMsg_toString(){
|
||||
return "LogMessage [" + this._date + " " + this.level + " " +
|
||||
this.message + "]";
|
||||
}
|
||||
};
|
||||
Log4Moz.Level = {};
|
||||
Log4Moz.Level.Fatal = LEVEL_FATAL;
|
||||
Log4Moz.Level.Error = LEVEL_ERROR;
|
||||
Log4Moz.Level.Warn = LEVEL_WARN;
|
||||
Log4Moz.Level.Info = LEVEL_INFO;
|
||||
Log4Moz.Level.Config = LEVEL_CONFIG;
|
||||
Log4Moz.Level.Debug = LEVEL_DEBUG;
|
||||
Log4Moz.Level.Trace = LEVEL_TRACE;
|
||||
Log4Moz.Level.All = LEVEL_ALL;
|
||||
|
||||
Log4Moz.Level.Desc = LEVEL_DESC;
|
||||
|
||||
Log4Moz.Service = new Log4MozService();
|
|
@ -58,6 +58,21 @@ const ONE_MEGABYTE = 1024 * ONE_KILOBYTE;
|
|||
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
|
||||
let ioSvc = Components.classes["@mozilla.org/network/io-service;1"]
|
||||
.getService(Components.interfaces.nsIIOService);
|
||||
let resProt = ioSvc.getProtocolHandler("resource")
|
||||
.QueryInterface(Components.interfaces.nsIResProtocolHandler);
|
||||
|
||||
if (!resProt.hasSubstitution("weave")) {
|
||||
let extMgr = Components.classes["@mozilla.org/extensions/manager;1"]
|
||||
.getService(Components.interfaces.nsIExtensionManager);
|
||||
let loc = extMgr.getInstallLocation("{340c2bbc-ce74-4362-90b5-7c26312808ef}");
|
||||
let extD = loc.getItemLocation("{340c2bbc-ce74-4362-90b5-7c26312808ef}");
|
||||
extD.append("modules");
|
||||
resProt.setSubstitution("weave", ioSvc.newFileURI(extD));
|
||||
}
|
||||
Cu.import("resource://weave/log4moz.js");
|
||||
|
||||
/*
|
||||
* Service object
|
||||
* Implements IBookmarksSyncService, main entry point
|
||||
|
@ -225,21 +240,18 @@ BookmarksSyncService.prototype = {
|
|||
},
|
||||
|
||||
_initLogs: function BSS__initLogs() {
|
||||
let logSvc = Cc["@mozilla.org/log4moz/service;1"].
|
||||
getService(Ci.ILog4MozService);
|
||||
this._log = Log4Moz.Service.getLogger("Service.Main");
|
||||
|
||||
this._log = logSvc.getLogger("Service.Main");
|
||||
let formatter = Log4Moz.Service.newFormatter("basic");
|
||||
let root = Log4Moz.Service.rootLogger;
|
||||
root.level = Log4Moz.Level.Debug;
|
||||
|
||||
let formatter = logSvc.newFormatter("basic");
|
||||
let root = logSvc.rootLogger;
|
||||
root.level = root.LEVEL_DEBUG;
|
||||
|
||||
let capp = logSvc.newAppender("console", formatter);
|
||||
capp.level = root.LEVEL_WARN;
|
||||
let capp = Log4Moz.Service.newAppender("console", formatter);
|
||||
capp.level = Log4Moz.Level.Warn;
|
||||
root.addAppender(capp);
|
||||
|
||||
let dapp = logSvc.newAppender("dump", formatter);
|
||||
dapp.level = root.LEVEL_ALL;
|
||||
let dapp = Log4Moz.Service.newAppender("dump", formatter);
|
||||
dapp.level = Log4Moz.Level.All;
|
||||
root.addAppender(dapp);
|
||||
|
||||
let logFile = this._dirSvc.get("ProfD", Ci.nsIFile);
|
||||
|
@ -249,11 +261,11 @@ BookmarksSyncService.prototype = {
|
|||
verboseFile.append("bm-sync-verbose.log");
|
||||
verboseFile.QueryInterface(Ci.nsILocalFile);
|
||||
|
||||
let fapp = logSvc.newFileAppender("rotating", logFile, formatter);
|
||||
fapp.level = root.LEVEL_INFO;
|
||||
let fapp = Log4Moz.Service.newFileAppender("rotating", logFile, formatter);
|
||||
fapp.level = Log4Moz.Level.Info;
|
||||
root.addAppender(fapp);
|
||||
let vapp = logSvc.newFileAppender("rotating", verboseFile, formatter);
|
||||
vapp.level = root.LEVEL_DEBUG;
|
||||
let vapp = Log4Moz.Service.newFileAppender("rotating", verboseFile, formatter);
|
||||
vapp.level = Log4Moz.Level.Debug;
|
||||
root.addAppender(vapp);
|
||||
},
|
||||
|
||||
|
@ -478,11 +490,7 @@ BookmarksEngine.prototype = {
|
|||
_init: function BmkEngine__init(davCollection, cryptoId) {
|
||||
this._dav = davCollection;
|
||||
this._cryptoId = cryptoId;
|
||||
|
||||
let logSvc = Cc["@mozilla.org/log4moz/service;1"].
|
||||
getService(Ci.ILog4MozService);
|
||||
this._log = logSvc.getLogger("Service." + this._logName);
|
||||
|
||||
this._log = Log4Moz.Service.getLogger("Service." + this._logName);
|
||||
this._snapshot.load();
|
||||
},
|
||||
|
||||
|
@ -1124,10 +1132,7 @@ Crypto.prototype = {
|
|||
},
|
||||
|
||||
_init: function Crypto__init() {
|
||||
let logSvc = Cc["@mozilla.org/log4moz/service;1"].
|
||||
getService(Ci.ILog4MozService);
|
||||
this._log = logSvc.getLogger("Service." + this._logName);
|
||||
|
||||
this._log = Log4Moz.Service.getLogger("Service." + this._logName);
|
||||
let branch = Cc["@mozilla.org/preferences-service;1"]
|
||||
.getService(Ci.nsIPrefBranch2);
|
||||
branch.addObserver("browser.places.sync.encryption", this, false);
|
||||
|
@ -1283,9 +1288,7 @@ SyncCore.prototype = {
|
|||
_logName: "Sync",
|
||||
|
||||
_init: function SC__init() {
|
||||
let logSvc = Cc["@mozilla.org/log4moz/service;1"].
|
||||
getService(Ci.ILog4MozService);
|
||||
this._log = logSvc.getLogger("Service." + this._logName);
|
||||
this._log = Log4Moz.Service.getLogger("Service." + this._logName);
|
||||
},
|
||||
|
||||
// FIXME: this won't work for deep objects, or objects with optional properties
|
||||
|
@ -1630,9 +1633,7 @@ Store.prototype = {
|
|||
_logName: "Store",
|
||||
|
||||
_init: function Store__init() {
|
||||
let logSvc = Cc["@mozilla.org/log4moz/service;1"].
|
||||
getService(Ci.ILog4MozService);
|
||||
this._log = logSvc.getLogger("Service." + this._logName);
|
||||
this._log = Log4Moz.Service.getLogger("Service." + this._logName);
|
||||
},
|
||||
|
||||
wrap: function Store_wrap() {
|
||||
|
@ -2119,9 +2120,7 @@ BookmarksStore.prototype.__proto__ = new Store();
|
|||
function DAVCollection(baseURL) {
|
||||
this._baseURL = baseURL;
|
||||
this._authProvider = new DummyAuthProvider();
|
||||
let logSvc = Cc["@mozilla.org/log4moz/service;1"].
|
||||
getService(Ci.ILog4MozService);
|
||||
this._log = logSvc.getLogger("Service.DAV");
|
||||
this._log = Log4Moz.Service.getLogger("Service.DAV");
|
||||
}
|
||||
DAVCollection.prototype = {
|
||||
__dp: null,
|
||||
|
@ -2614,9 +2613,7 @@ DummyAuthProvider.prototype = {
|
|||
function EventListener(handler, eventName) {
|
||||
this._handler = handler;
|
||||
this._eventName = eventName;
|
||||
let logSvc = Cc["@mozilla.org/log4moz/service;1"].
|
||||
getService(Ci.ILog4MozService);
|
||||
this._log = logSvc.getLogger("Service.EventHandler");
|
||||
this._log = Log4Moz.Service.getLogger("Service.EventHandler");
|
||||
}
|
||||
EventListener.prototype = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback, Ci.nsISupports]),
|
||||
|
|
Загрузка…
Ссылка в новой задаче