From f0aed553190621b58b430753f1827471a74bddae Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Mon, 8 Apr 2019 12:18:56 +0200 Subject: [PATCH] uiux: improve logging of deployment hooks by adding logger dependency This will allow users to generated deploy hook specific output using the supplied `logger` within deployment hooks. The logger will generate an output a la: ``` ContractName > deploymentHook > Output ``` and ``` afterDeploy > Output ``` respectively. E.g. an `onDeploy` hook config like this: ``` contracts: { SimpleStorage: { fromIndex: 0, args: [100], onDeploy: (context) => { context.logger.log('Hello from on deploy'); } } } ``` Will output: ``` SimpleStorage > onDeploy > Hello from on deploy ``` --- packages/embark-specialconfigs/src/index.js | 33 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/embark-specialconfigs/src/index.js b/packages/embark-specialconfigs/src/index.js index 5ca10c45f..803c85ad2 100644 --- a/packages/embark-specialconfigs/src/index.js +++ b/packages/embark-specialconfigs/src/index.js @@ -240,7 +240,10 @@ class SpecialConfigs { if (err) { reject(err); } - this.events.request('blockchain:get', web3 => resolve(this.assembleLifecycleHookDependencies(contractInstances, web3))); + this.events.request('blockchain:get', web3 => { + const logger = this.createLoggerWithPrefix(`${contractConfig.className} > onDeploy >`); + resolve(this.assembleLifecycleHookDependencies(contractInstances, web3, logger)); + }); }); }); } @@ -259,18 +262,40 @@ class SpecialConfigs { if (err) { reject(err); } - this.events.request('blockchain:get', web3 => resolve(this.assembleLifecycleHookDependencies(contractInstances, web3))); + this.events.request('blockchain:get', web3 => { + const logger = this.createLoggerWithPrefix('afterDeploy >'); + resolve(this.assembleLifecycleHookDependencies(contractInstances, web3, logger)); + }); }); }); }); } - assembleLifecycleHookDependencies(contractInstances, web3) { + assembleLifecycleHookDependencies(contractInstances, web3, logger) { return contractInstances.reduce((dependencies, contractInstance) => { dependencies.contracts[contractInstance.className] = contractInstance.instance; return dependencies; - }, { contracts: {}, web3 }); + }, { contracts: {}, web3, logger }); + } + + createLoggerWithPrefix(prefix) { + const logger = { + log: createLogWithPrefixFn(this.logger, 'log', prefix), + warn: createLogWithPrefixFn(this.logger, 'warn', prefix), + error: createLogWithPrefixFn(this.logger, 'error', prefix), + info: createLogWithPrefixFn(this.logger, 'info', prefix), + dir: createLogWithPrefixFn(this.logger, 'dir', prefix), + debug: createLogWithPrefixFn(this.logger, 'debug', prefix) + }; + return logger; } } +function createLogWithPrefixFn(logger, method, prefix) { + return function () { + const args = Array.from(arguments).map(arg => `${prefix} ${arg}`); + args.forEach(arg => logger[method](arg)); + }; +} + module.exports = SpecialConfigs;