diff --git a/README.md b/README.md index 2485714de0..67c7ecaa25 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,14 @@ Instead of a boolean, you may provide an object with custom initialization optio A passphrase to encrypt/decrypt your keys. +##### `options.silent` + +| Type | Default | +|------|---------| +| Boolean | `false` | + +Prevents all logging output from the IPFS node. + ##### `options.relay` | Type | Default | diff --git a/src/cli/bin.js b/src/cli/bin.js index 401357ebf3..7878a05978 100755 --- a/src/cli/bin.js +++ b/src/cli/bin.js @@ -24,7 +24,12 @@ const cli = yargs desc: 'Write no output', type: 'boolean', default: false, - coerce: ('silent', silent => silent ? utils.disablePrinting() : silent) + coerce: ('silent', silent => { + if (silent) { + utils.disablePrinting() + } + return silent + }) }) .option('pass', { desc: 'Pass phrase for the keys', diff --git a/src/cli/utils.js b/src/cli/utils.js index 9c462bc3d7..46777125e5 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -47,6 +47,7 @@ exports.getIPFS = (argv, callback) => { // Required inline to reduce startup time const IPFS = require('../core') const node = new IPFS({ + silent: argv.silent, repo: exports.getRepoPath(), init: false, start: false, diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index d195623467..c5e3a40b1c 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -107,7 +107,7 @@ module.exports = function libp2p (self) { if (err) { return callback(err) } self._libp2pNode.peerInfo.multiaddrs.forEach((ma) => { - console.log('Swarm listening on', ma.toString()) + self._print('Swarm listening on', ma.toString()) }) callback() diff --git a/src/core/index.js b/src/core/index.js index 8425cab7a2..8aa1fd823c 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -127,6 +127,7 @@ class IPFS extends EventEmitter { this._preload = preload(this) this._mfsPreload = mfsPreload(this) this._ipns = undefined + this._print = this._options.silent ? this.log : console.log // IPFS Core exposed components // - for booting up a node diff --git a/src/http/index.js b/src/http/index.js index dd6b19b430..fbace44d3b 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -68,6 +68,7 @@ function HttpApi (repo, config, cliArgs) { try { // start the daemon this.node = new IPFS({ + silent: cliArgs.silent, repo: repo, init: init, start: true, @@ -157,9 +158,9 @@ function HttpApi (repo, config, cliArgs) { api.info.ma = uriToMultiaddr(api.info.uri) gateway.info.ma = uriToMultiaddr(gateway.info.uri) - console.log('API listening on %s', api.info.ma) - console.log('Gateway (read only) listening on %s', gateway.info.ma) - console.log('Web UI available at %s', api.info.uri + '/webui') + this.node._print('API listening on %s', api.info.ma) + this.node._print('Gateway (read only) listening on %s', gateway.info.ma) + this.node._print('Web UI available at %s', api.info.uri + '/webui') // for the CLI to know the where abouts of the API this.node._repo.apiAddr.set(api.info.ma, cb) diff --git a/test/cli/daemon.js b/test/cli/daemon.js index d60d591279..a9aaccacf0 100644 --- a/test/cli/daemon.js +++ b/test/cli/daemon.js @@ -126,6 +126,21 @@ describe('daemon', () => { }) }) + it('should be silent', function (done) { + this.timeout(10 * 1000) + const res = ipfs('daemon --silent') + res.catch(function () {}) // Handles the unhandled promise rejection + let output = '' + const onData = (d) => { output += d } + res.stdout.on('data', onData) + res.stderr.on('data', onData) + setTimeout(function () { + res.kill() + expect(output).to.be.empty() + done() + }, 5 * 1000) + }) + it('should present ipfs path help when option help is received', function (done) { this.timeout(100 * 1000) diff --git a/test/core/create-node.spec.js b/test/core/create-node.spec.js index f1e8c5cc75..0ffdac5d41 100644 --- a/test/core/create-node.spec.js +++ b/test/core/create-node.spec.js @@ -7,6 +7,7 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) const series = require('async/series') +const sinon = require('sinon') const waterfall = require('async/waterfall') const parallel = require('async/parallel') const os = require('os') @@ -131,6 +132,21 @@ describe('create node', function () { }) }) + it('should be silent', (done) => { + sinon.spy(console, 'log') + + const ipfs = new IPFS({ + silent: true, + repo: tempRepo + }) + + ipfs.on('ready', () => { + expect(console.log.called).to.be.false() + console.log.restore() + done() + }) + }) + it('init: false errors (start default: true) and errors only once', function (done) { this.timeout(80 * 1000) diff --git a/test/core/libp2p.spec.js b/test/core/libp2p.spec.js index aacc4215a3..c7acca3a6d 100644 --- a/test/core/libp2p.spec.js +++ b/test/core/libp2p.spec.js @@ -76,6 +76,7 @@ describe('libp2p customization', function () { }, _peerInfo: peerInfo, _peerBook: peerBook, + _print: console.log, config: mockConfig, _options: { libp2p: (opts) => { @@ -122,6 +123,7 @@ describe('libp2p customization', function () { }, _peerInfo: peerInfo, _peerBook: peerBook, + _print: console.log, config: mockConfig } @@ -164,6 +166,7 @@ describe('libp2p customization', function () { }, _peerInfo: peerInfo, _peerBook: peerBook, + _print: console.log, config: mockConfig, _options: { config: {