node: refactor config options.

This commit is contained in:
Christopher Jeffrey 2017-03-14 05:16:52 -07:00
parent b7c847059a
commit 4674109706
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
6 changed files with 42 additions and 24 deletions

View File

@ -13,6 +13,7 @@ node = new bcoin.fullnode({
argv: true, argv: true,
env: true, env: true,
logFile: true, logFile: true,
logConsole: true,
logLevel: 'debug', logLevel: 'debug',
db: 'leveldb', db: 'leveldb',
listen: true, listen: true,

View File

@ -15,6 +15,7 @@ node = bcoin.spvnode({
argv: true, argv: true,
env: true, env: true,
logFile: true, logFile: true,
logConsole: true,
logLevel: 'debug', logLevel: 'debug',
db: 'leveldb', db: 'leveldb',
listen: true, listen: true,

View File

@ -9,7 +9,11 @@ var server = require('../lib/wallet/server');
var wdb = server.create({ var wdb = server.create({
config: true, config: true,
argv: true, argv: true,
env: true env: true,
logFile: true,
logConsole: true,
logLevel: 'debug',
db: 'leveldb'
}); });
wdb.on('error', function(err) { wdb.on('error', function(err) {

View File

@ -36,7 +36,8 @@ function Logger(options) {
this.contexts = {}; this.contexts = {};
this.locker = new Lock(); this.locker = new Lock();
this.init(options); if (options)
this.set(options);
} }
/** /**
@ -131,14 +132,13 @@ Logger.colors = [
]; ];
/** /**
* Initialize the logger. * Set logger options.
* @private
* @param {Object} options * @param {Object} options
*/ */
Logger.prototype.init = function init(options) { Logger.prototype.set = function set(options) {
if (!options) assert(options);
return; assert(this.closed);
if (typeof options === 'string') { if (typeof options === 'string') {
this.setLevel(options); this.setLevel(options);
@ -196,11 +196,15 @@ Logger.prototype._open = co(function* open() {
if (fs.unsupported) if (fs.unsupported)
return; return;
if (!this.filename) if (!this.filename) {
this.closed = false;
return; return;
}
if (this.stream) if (this.stream) {
this.closed = false;
return; return;
}
if (this.shrink) if (this.shrink)
yield this.truncate(); yield this.truncate();

View File

@ -48,7 +48,7 @@ function Node(options) {
this.stack = []; this.stack = [];
this.spv = false; this.spv = false;
this.logger = new Logger(); this.logger = null;
this.chain = null; this.chain = null;
this.fees = null; this.fees = null;
this.mempool = null; this.mempool = null;
@ -68,24 +68,22 @@ util.inherits(Node, AsyncObject);
*/ */
Node.prototype.initOptions = function initOptions() { Node.prototype.initOptions = function initOptions() {
var logger = new Logger();
var config = this.config; var config = this.config;
if (config.has('logger')) if (config.has('logger'))
this.logger = config.obj('logger'); logger = config.obj('logger');
if (config.bool('log-file')) logger.set({
this.logger.setFile(config.location('debug.log')); filename: config.bool('log-file')
? config.location('debug.log')
: null,
level: config.str('log-level'),
console: config.bool('log-console'),
shrink: config.bool('log-shrink')
});
if (config.has('log-level')) this.logger = logger.context('node');
this.logger.setLevel(config.str('log-level'));
if (config.has('log-console'))
this.logger.console = config.bool('log-console');
if (config.bool('debug'))
this.logger.shrink = false;
this.logger = this.logger.context('node');
}; };
/** /**

View File

@ -34,13 +34,23 @@ server.create = function create(options) {
if (options.config) if (options.config)
config.open('wallet.conf'); config.open('wallet.conf');
if (config.has('logger'))
logger = config.obj('logger');
client = new Client({ client = new Client({
network: config.network, network: config.network,
uri: config.str('node-uri'), uri: config.str('node-uri'),
apiKey: config.str('node-api-key') apiKey: config.str('node-api-key')
}); });
logger.setFile(config.location('wallet.log')); logger.set({
filename: config.bool('log-file')
? config.location('wallet.log')
: null,
level: config.str('log-level'),
console: config.bool('log-console'),
shrink: config.bool('log-shrink')
});
return new WalletDB({ return new WalletDB({
network: config.network, network: config.network,