Commit d40e26df authored by Sergey Razuvaev's avatar Sergey Razuvaev

add logger

parent 4c368700
......@@ -5,10 +5,16 @@ var commands = require('./command');
//base context
var Context = function (stream, debug) {
var Context = function (stream, loggerOptions) {
EventEmitter.call(this);
this.debug = debug;
function consoleDecorator(data){
return console.log(JSON.stringify (data))
}
this.debug = loggerOptions.debug
this.log = loggerOptions.logger.debug || consoleDecorator;
this.stream = new Readable();
this.stream.setEncoding('utf8');
......@@ -113,11 +119,11 @@ Context.prototype.end = function () {
};
Context.prototype.sendCommand = function (command) {
if (this.debug) console.log('command', command);
if (this.debug) this.log({ command: 'command' });
var self = this;
return new Promise(function (resolve, reject) {
self.send(command + '\n', function (err, result) {
if (self.debug) console.log('err:', err, 'result:', result);
if (self.debug) this.log({ err: err, result: result });
if (err) {
reject(err);
} else {
......
......@@ -8,11 +8,12 @@ var agi = function (handler, optionsIn) {
var settings = {
port: options.port || 3000,
debug: options.debug || false,
logger: options.logger || false,
host: options.host,
};
var handle = function (stream) {
var context = new Context(stream, settings.debug);
var context = new Context(stream, { debug: settings.debug, logger: options.logger});
handler(context);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment