var pkg = require("./package.json")
var MongoClient = require("mongodb").MongoClient
var params = require("commander")

params
  .version(pkg.version)
  .description(pkg.description)
  .option("-H, --host <address>", "DataBase host (127.0.0.1)", "127.0.0.1")
  .option("-P, --port <number>", "DataBase port (27017)", "27017")
  .option("-d, --db <name>", "DataBase name (logs)", "logs")
  .option("-c, --collection <name>", "DataBase collection name (logs)", "logs")
  .option("-u, --user <username>", "DataBase username (root)", "root")
  .option("-p, --pass <password>", "DataBase username password ()", "")
  .parse(process.argv)

var mongoOptions = "mongodb://"
  + (params.user && params.pass)
    ? "[" + params.user + ":" + params.pass + "]@"
    : ""
  + params.host
  + ":" + params.port
  + "/" + params.db

MongoClient.connect(mongoOptions, onConnection)

function onConnection(e, db) {

  if (e) {
    return handleError(e)
  }

  var collection = db.collection(params.collection)
  
  process.stdin.resume()
  process.stdin.setEncoding("utf8")
  process.stdin.on("data", function stdin(data) {

    var document = null

    try {
      document = JSON.parse(data)
    } catch (e) {
      document = {
        msg: data
      }
    }

    collection.insertOne(document, function insertOne(e) {
      if (e) {
        return handleError(e)
      }
    })

    process.stdout.write(JSON.stringify(document) + "\n")

  })
}

function handleError(e) {
  if (e instanceof Error) {
    console.error(e)
  } else {
    console.error(new Error(e))
  }
  return
}

@@ -690,18 +690,19 @@
 ams.pass
-)
 %0A
+  
     ? %22%5B
@@ -752,12 +752,15 @@
     
+  
 : %22%22
+)
 %0A  +
@@ -993,16 +993,112 @@
 ion)%0A  %0A
+  process.on(%22SIGINT%22, function() %7B%0A    db.close(function() %7B%0A      process.exit()%0A    %7D)%0A  %7D)%0A%0A
   proces
@@ -1653,15 +1653,6 @@
   %7D%0A
-  return%0A
 %7D%0A

Add process SIGINT handler