Merge pull request #561 from embark-framework/bug_fix/intercept-rebased

Fix log interception and enable multi-argument logging #2
This commit is contained in:
Iuri Matias 2018-06-20 16:34:36 -04:00 committed by GitHub
commit b1d9b91890
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 71 deletions

View File

@ -11,57 +11,57 @@ class Logger {
} }
} }
Logger.prototype.writeToFile = function (txt) { Logger.prototype.writeToFile = function () {
if (!this.logFile) { if (!this.logFile) {
return; return;
} }
fs.appendFileSync(this.logFile, "\n" + txt); fs.appendFileSync(this.logFile, "\n" + Array.from(arguments).join(' '));
}; };
Logger.prototype.error = function (txt) { Logger.prototype.error = function () {
if (!txt || !(this.shouldLog('error'))) { if (!arguments.length || !(this.shouldLog('error'))) {
return; return;
} }
this.events.emit("log", "error", txt); this.events.emit("log", "error", ...arguments);
this.logFunction(txt.red); this.logFunction(...Array.from(arguments).map(t => t.red));
this.writeToFile("[error]: " + txt); this.writeToFile("[error]: ", ...arguments);
}; };
Logger.prototype.warn = function (txt) { Logger.prototype.warn = function () {
if (!txt || !(this.shouldLog('warn'))) { if (!arguments.length || !(this.shouldLog('warn'))) {
return; return;
} }
this.events.emit("log", "warning", txt); this.events.emit("log", "warning", ...arguments);
this.logFunction(txt.yellow); this.logFunction(...Array.from(arguments).map(t => t.yellow));
this.writeToFile("[warning]: " + txt); this.writeToFile("[warning]: ", ...arguments);
}; };
Logger.prototype.info = function (txt) { Logger.prototype.info = function () {
if (!txt || !(this.shouldLog('info'))) { if (!arguments.length || !(this.shouldLog('info'))) {
return; return;
} }
this.events.emit("log", "info", txt); this.events.emit("log", "info", ...arguments);
this.logFunction(txt.green); this.logFunction(...Array.from(arguments).map(t => t.green));
this.writeToFile("[info]: " + txt); this.writeToFile("[info]: ", ...arguments);
}; };
Logger.prototype.debug = function (txt) { Logger.prototype.debug = function () {
if (!txt || !(this.shouldLog('debug'))) { if (!arguments.length || !(this.shouldLog('debug'))) {
return; return;
} }
this.events.emit("log", "debug", txt); this.events.emit("log", "debug", ...arguments);
this.logFunction(txt); this.logFunction(...arguments);
this.writeToFile("[debug]: " + txt); this.writeToFile("[debug]: ", ...arguments);
}; };
Logger.prototype.trace = function (txt) { Logger.prototype.trace = function () {
if (!txt || !(this.shouldLog('trace'))) { if (!arguments.length || !(this.shouldLog('trace'))) {
return; return;
} }
this.events.emit("log", "trace", txt); this.events.emit("log", "trace", ...arguments);
this.logFunction(txt); this.logFunction(...arguments);
this.writeToFile("[trace]: " + txt); this.writeToFile("[trace]: ", ...arguments);
}; };
Logger.prototype.dir = function (txt) { Logger.prototype.dir = function (txt) {

View File

@ -29,7 +29,8 @@ var Plugin = function(options) {
this.afterContractsDeployActions = []; this.afterContractsDeployActions = [];
this.onDeployActions = []; this.onDeployActions = [];
this.eventActions = {}; this.eventActions = {};
this.logger = options.logger; this._loggerObject = options.logger;
this.logger = this._loggerObject; // Might get changed if we do intercept
this.events = options.events; this.events = options.events;
this.config = options.config; this.config = options.config;
this.env = options.env; this.env = options.env;
@ -45,6 +46,22 @@ var Plugin = function(options) {
} }
}; };
Plugin.prototype._log = function(type) {
this._loggerObject[type](this.name + ':', ...[].slice.call(arguments, 1));
};
Plugin.prototype.setUpLogger = function () {
this.logger = {
log: this._log.bind(this, 'log'),
warn: this._log.bind(this, 'warn'),
error: this._log.bind(this, 'error'),
info: this._log.bind(this, 'info'),
debug: this._log.bind(this, 'debug'),
trace: this._log.bind(this, 'trace'),
dir: this._log.bind(this, 'dir')
};
};
Plugin.prototype.isContextValid = function() { Plugin.prototype.isContextValid = function() {
if (this.currentContext.includes(constants.contexts.any) || this.acceptedContext.includes(constants.contexts.any)) { if (this.currentContext.includes(constants.contexts.any) || this.acceptedContext.includes(constants.contexts.any)) {
return true; return true;
@ -65,7 +82,7 @@ Plugin.prototype.loadPlugin = function() {
} }
this.loaded = true; this.loaded = true;
if (this.shouldInterceptLogs) { if (this.shouldInterceptLogs) {
this.interceptLogs(this.pluginModule); this.setUpLogger();
} }
(this.pluginModule.call(this, this)); (this.pluginModule.call(this, this));
}; };
@ -85,37 +102,6 @@ Plugin.prototype.pathToFile = function(filename) {
return utils.joinPath(this.pluginPath, filename); return utils.joinPath(this.pluginPath, filename);
}; };
Plugin.prototype.interceptLogs = function(context) {
var self = this;
// TODO: this is a bit nasty, figure out a better way
context.console = context.console || console;
//context.console.error = function(txt) {
// // TODO: logger should support an array instead of a single argument
// //self.logger.error.apply(self.logger, arguments);
// self.logger.error(self.name + " > " + txt);
//};
context.console.log = function(txt) {
self.logger.info(self.name + " > " + txt);
};
context.console.warn = function(txt) {
self.logger.warn(self.name + " > " + txt);
};
context.console.info = function(txt) {
self.logger.info(self.name + " > " + txt);
};
context.console.debug = function(txt) {
// TODO: ue JSON.stringify
self.logger.debug(self.name + " > " + txt);
};
context.console.trace = function(txt) {
self.logger.trace(self.name + " > " + txt);
};
context.console.dir = function(txt) {
self.logger.dir(txt);
};
};
// TODO: add deploy provider // TODO: add deploy provider
Plugin.prototype.registerClientWeb3Provider = function(cb) { Plugin.prototype.registerClientWeb3Provider = function(cb) {
this.clientWeb3Providers.push(cb); this.clientWeb3Providers.push(cb);

View File

@ -77,8 +77,8 @@ class Dashboard {
this.screen.render(); this.screen.render();
} }
logEntry(text) { logEntry() {
this.logText.log(text); this.logText.log(...arguments);
this.screen.render(); this.screen.render();
} }

View File

@ -25,17 +25,9 @@ module.exports = function (embark) {
embark.registerActionForEvent("deploy:contract:beforeDeploy", (params, cb) => { embark.registerActionForEvent("deploy:contract:beforeDeploy", (params, cb) => {
embark.logger.info("applying beforeDeploy plugin..."); embark.logger.info("applying beforeDeploy plugin...");
//console.dir(params);
//console.dir(cb);
//console.dir('------------------');
cb(); cb();
}); });
// NOTE: uncommenting this will make dappConnection stop working
//embark.registerClientWeb3Provider(function(options) {
// return "web3 = new Web3(new Web3.providers.HttpProvider('http://" + options.rpcHost + ":" + options.rpcPort + "'));";
//});
embark.registerConsoleCommand((cmd) => { embark.registerConsoleCommand((cmd) => {
if (cmd === "hello") { if (cmd === "hello") {
return "hello there!"; return "hello there!";
@ -45,7 +37,7 @@ module.exports = function (embark) {
}); });
embark.events.on("contractsDeployed", function() { embark.events.on("contractsDeployed", function() {
embark.logger.info("plugin says: your contracts have been deployed"); embark.logger.info("plugin says:", ' your contracts have been deployed');
}); });
}; };