verbose logging switch

This commit is contained in:
Wolfgang Hottgenroth 2018-04-26 16:38:56 +02:00
parent 0eede1309f
commit f536b7a16f
Signed by: wn
GPG Key ID: B586EAFCDF2F65F4

View File

@ -27,6 +27,7 @@ options
.option('-t, --topic [topic to subscribe]', 'Topic to subscribe, can appear multiple times', collect, []) .option('-t, --topic [topic to subscribe]', 'Topic to subscribe, can appear multiple times', collect, [])
.option('-e, --encapsulate', 'store current timestamp, topic and payload in document', false) .option('-e, --encapsulate', 'store current timestamp, topic and payload in document', false)
.option('-p, --parsePayload', 'parse payload when encapsulating (otherwise always)', false) .option('-p, --parsePayload', 'parse payload when encapsulating (otherwise always)', false)
.option('-v, --verbose', 'log all inserted messages', false)
.parse(process.argv) .parse(process.argv)
@ -79,12 +80,16 @@ class MqttMongo extends Events.EventEmitter {
if (this.dbReady || true) { if (this.dbReady || true) {
while (! this.queue.isEmpty()) { while (! this.queue.isEmpty()) {
let msg : MqttMessage.MqttMessage = this.queue.deq() let msg : MqttMessage.MqttMessage = this.queue.deq()
if (this.options.verbose) {
console.info(`Something in the queue: ${JSON.stringify(msg)}`) console.info(`Something in the queue: ${JSON.stringify(msg)}`)
}
let coll = this.dbHandle.collection(this.options.collection) let coll = this.dbHandle.collection(this.options.collection)
coll.insertOne(msg.getMessage()) coll.insertOne(msg.getMessage())
.then( .then(
(r) => { (r) => {
if (this.options.verbose) {
console.success(`Successfully inserted into database ${JSON.stringify(msg.getMessage())}`) console.success(`Successfully inserted into database ${JSON.stringify(msg.getMessage())}`)
}
}, },
(err) => { (err) => {
console.error(`Error when trying to insert into database ${err}`) console.error(`Error when trying to insert into database ${err}`)
@ -122,7 +127,9 @@ class MqttMongo extends Events.EventEmitter {
this.mqttClient.on('message', (topic : string, messageBuf : Buffer) => { this.mqttClient.on('message', (topic : string, messageBuf : Buffer) => {
msgCnt++; msgCnt++;
let message = messageBuf.toString('UTF-8') let message = messageBuf.toString('UTF-8')
if (this.options.verbose) {
console.info(`Message received ${msgCnt}, topic ${topic}, payload ${message}`) console.info(`Message received ${msgCnt}, topic ${topic}, payload ${message}`)
}
if (topic == "MqttMongo/Command" && message == "shutdown") { if (topic == "MqttMongo/Command" && message == "shutdown") {
this.shutdown() this.shutdown()