Skip to content

Commit 96495b9

Browse files
committed
Support setting log level as an argument
* Add loglevel and logfile switch to `embark build` * Support existing `loglevel` and `logfile` switch for `embark run/upload` * make casing consistent for `loglevel` and `logfile` * remove passing engine to `build()` and instead pass needed objects in options. * prevent duplicate plugin initiation (above point) * allow `Events` object to be passed to `Engine` constructor and `init()` * prevent echo of upload commands to allow interception and control via logging
1 parent d985a5e commit 96495b9

File tree

6 files changed

+61
-50
lines changed

6 files changed

+61
-50
lines changed

lib/cmd.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,14 @@ class Cmd {
8787
build() {
8888
program
8989
.command('build [environment]')
90+
.option('--logfile [logfile]', 'filename to output logs (default: none)')
91+
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"] (default: debug)')
9092
.description('deploy and build dapp at dist/ (default: development)')
9193
.action(function (env, _options) {
92-
embark.build({env: env || 'development'});
94+
_options.env = env || 'development';
95+
_options.logFile = _options.logfile; // fix casing
96+
_options.logLevel = _options.loglevel; // fix casing
97+
embark.build(_options);
9398
});
9499
}
95100

@@ -102,7 +107,7 @@ class Cmd {
102107
.option('--nodashboard', 'simple mode, disables the dashboard')
103108
.option('--no-color', 'no colors in case it\'s needed for compatbility purposes')
104109
.option('--logfile [logfile]', 'filename to output logs (default: none)')
105-
.option('--logLevel [logLevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"] (default: debug)')
110+
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"] (default: debug)',)
106111
.description('run dapp (default: development)')
107112
.action(function (env, options) {
108113
embark.run({
@@ -111,8 +116,8 @@ class Cmd {
111116
serverHost: options.host,
112117
runWebserver: !options.noserver,
113118
useDashboard: !options.nodashboard,
114-
logfile: options.logfile,
115-
logLevel: options.logLevel
119+
logFile: options.logfile,
120+
logLevel: options.loglevel
116121
});
117122
});
118123
}
@@ -171,15 +176,18 @@ class Cmd {
171176

172177
upload() {
173178
program
174-
.command('upload [platform] [environment]')
179+
.command('upload <platform> [environment]')
175180
.option('--logfile [logfile]', 'filename to output logs (default: none)')
176-
.description('upload your dapp to a decentralized storage (e.g embark upload ipfs)')
181+
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
182+
.description('Upload your dapp to a decentralized storage (e.g embark upload ipfs).')
177183
.action(function (platform, env, _options) {
178184
let environment = env || 'development';
179185
embark.initConfig(environment, {
180186
embarkConfig: 'embark.json', interceptLogs: false
181187
});
182188
_options.env = environment;
189+
_options.logFile = _options.logfile; // fix casing
190+
_options.logLevel = _options.loglevel; // fix casing
183191
embark.upload(platform, _options);
184192
});
185193
}
@@ -191,7 +199,7 @@ class Cmd {
191199
.action(function (env, options) {
192200
embark.graph({
193201
env: env || 'development',
194-
logfile: options.logfile
202+
logFile: options.logfile
195203
});
196204
});
197205
}

lib/core/engine.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ class Engine {
1616
this.embarkConfig = options.embarkConfig;
1717
this.interceptLogs = options.interceptLogs;
1818
this.version = options.version;
19-
this.logfile = options.logfile;
19+
this.logFile = options.logFile;
2020
this.logLevel = options.logLevel;
21+
this.events = options.events;
2122
}
2223

2324
init(_options) {
2425
let self = this;
2526
let options = _options || {};
26-
this.events = new Events();
27-
this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logfile: this.logfile});
27+
this.events = options.events || this.events || new Events();
28+
this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile});
2829
this.config = new Config({env: this.env, logger: this.logger, events: this.events});
2930
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
3031
this.plugins = this.config.plugins;

lib/core/logger.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ class Logger {
77
this.logLevels = ['error', 'warn', 'info', 'debug', 'trace'];
88
this.logLevel = options.logLevel || 'info';
99
this.logFunction = options.logFunction || console.log;
10-
this.logfile = options.logfile;
10+
this.logFile = options.logFile;
1111
}
1212
}
1313

1414
Logger.prototype.writeToFile = function (txt) {
15-
if (!this.logfile) {
15+
if (!this.logFile) {
1616
return;
1717
}
1818

19-
fs.appendFileSync(this.logfile, "\n" + txt);
19+
fs.appendFileSync(this.logFile, "\n" + txt);
2020
};
2121

2222
Logger.prototype.error = function (txt) {

lib/index.js

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ class Embark {
6161
env: options.env,
6262
version: this.version,
6363
embarkConfig: options.embarkConfig || 'embark.json',
64-
logfile: options.logfile,
65-
logLevel: options.logLevel,
66-
interceptLogs: true
64+
logFile: options.logFile,
65+
logLevel: options.logLevel
6766
});
6867
engine.init();
6968

@@ -149,16 +148,20 @@ class Embark {
149148
});
150149
}
151150

152-
build(options, engine, continueProcessing) {
153-
if(!engine){
154-
engine = new Engine({
155-
env: options.env,
156-
version: this.version,
157-
embarkConfig: 'embark.json',
158-
interceptLogs: false
159-
});
160-
engine.init();
161-
}
151+
build(options, continueProcessing) {
152+
let engine = new Engine({
153+
env: options.env,
154+
version: this.version,
155+
embarkConfig: 'embark.json',
156+
interceptLogs: false,
157+
logFile: options.logFile,
158+
logLevel: options.logLevel,
159+
events: options.events,
160+
logger: options.logger,
161+
config: options.config,
162+
plugins: options.plugins
163+
});
164+
engine.init();
162165

163166
async.waterfall([
164167
function startServices(callback) {
@@ -206,7 +209,7 @@ class Embark {
206209
env: options.env,
207210
version: this.version,
208211
embarkConfig: options.embarkConfig || 'embark.json',
209-
logfile: options.logfile
212+
logFile: options.logFile
210213
});
211214
engine.init();
212215

@@ -254,61 +257,57 @@ class Embark {
254257
// TODO: should deploy if it hasn't already
255258
upload(platform, options) {
256259

260+
// populate options that were instantiated with initConfig to pass around
257261
options.buildDir = 'dist/';
258262
options.storageConfig = this.config.storageConfig;
259-
260-
// initialise embark engine
261-
let engine = new Engine({
262-
env: options.env,
263-
version: this.version,
264-
embarkConfig: options.embarkConfig || 'embark.json',
265-
logfile: options.logfile
266-
});
267-
engine.init();
263+
options.events = this.events;
264+
options.logger = this.logger;
265+
options.config = this.config;
268266

269267
// load plugins
270268
this.plugins.loadInternalPlugin('ipfs', options);
271269
this.plugins.loadInternalPlugin('swarm', options);
272270

273-
let plugins = this.plugins;
271+
// upddate our options with loaded plugins
272+
options.plugins = this.plugins;
273+
274274
let cmdPlugin;
275275
let self = this;
276276
async.waterfall([
277277
function setupStoragePlugin(callback){
278278
// check use has input existing storage plugin
279-
let cmdPlugins = plugins.getPluginsFor('uploadCmds');
279+
let cmdPlugins = self.plugins.getPluginsFor('uploadCmds');
280280

281281
if (cmdPlugins.length > 0) {
282282
cmdPlugin = cmdPlugins.find((pluginCmd) => {
283283
return pluginCmd.name == platform;
284284
});
285285
}
286286
if (!cmdPlugin) {
287-
engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
287+
self.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
288288
callback({message: 'unknown platform: ' + platform});
289289
} else {
290290
callback();
291291
}
292292
},
293293
function buildAndDeployContracts(callback){
294294
// 2. upload to storage (outputDone event triggered after webpack finished)
295-
engine.events.on('outputDone', function () {
296-
engine.logger.info('deploying to ' + platform + '...');
295+
self.events.on('outputDone', function () {
297296
cmdPlugin.uploadCmds[0].cb()
298297
.then((success) => {
299298
callback(null, success);
300299
})
301300
.catch(callback);
302301
});
303302
// 1. build the contracts and dapp webpack
304-
self.build(options, engine, true);
303+
self.build(options, true);
305304
}
306305
], function (err, _result) {
307306
if (err) {
308-
engine.logger.error(err.message);
309-
engine.logger.debug(err.stack);
307+
self.logger.error(err.message);
308+
self.logger.debug(err.stack);
310309
} else {
311-
engine.logger.info("finished building dapp and deploying to " + platform.underline);
310+
self.logger.info("finished building dapp and deploying to " + platform.underline);
312311
}
313312

314313
// needed due to child processes

lib/modules/ipfs/upload.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class IPFS {
1313

1414
deploy() {
1515
return new Promise((resolve, reject) => {
16-
console.log("deploying!");
16+
console.log("deploying to ipfs!");
1717
let self = this;
1818
async.waterfall([
1919
function findBinary(callback) {
@@ -29,8 +29,9 @@ class IPFS {
2929
function runCommand(ipfs_bin, callback) {
3030
let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`;
3131
console.log(("=== adding " + self.buildDir + " to ipfs").green);
32-
console.log(cmd.green);
33-
shelljs.exec(cmd, function(code, stdout, stderr){
32+
console.trace(cmd);
33+
shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel
34+
console.log(stdout.green);
3435
callback(stderr, stdout);
3536
});
3637
},

lib/modules/swarm/upload.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Swarm {
1010

1111
deploy() {
1212
return new Promise((resolve, reject) => {
13+
console.log("deploying to swarm!");
1314
let self = this;
1415
async.waterfall([
1516
function findBinary(callback) {
@@ -25,8 +26,9 @@ class Swarm {
2526
function runCommand(swarm_bin, callback) {
2627
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
2728
console.log(("=== adding " + self.buildDir + " to swarm").green);
28-
console.log(cmd.green);
29-
shelljs.exec(cmd, function(code, stdout, stderr){
29+
console.trace(cmd);
30+
shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel
31+
console.log(stdout.green);
3032
callback(stderr, {code: code, output: stdout});
3133
});
3234
},

0 commit comments

Comments
 (0)