Skip to content
This repository
tree: 374737a3eb
Fetching contributors…

Cannot retrieve contributors at this time

executable file 141 lines (106 sloc) 2.866 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#!/usr/bin/env node

var program = require('commander')
  , util = require('util')
  , ms = require('ms')
  , jog = require('..');

// map functions

var maps = [];

// filter functions

var selects = [];

// --map

program.on('map', function(val){
  maps.push(new Function('_', 'return ' + val));
});

// --level

program.on('level', function(val){
  selects.push(new Function('_', 'return _.level == "' + val + '"'));
});

// --type

program.on('type', function(val){
  selects.push(new Function('_', 'return _.type == "' + val + '"'));
});

// --select

program.on('select', function(val){
  selects.push(new Function('_', 'return ' + val));
});

program
  .version(jog.version)
  .option('-w, --within <ms>', 'filter events to within the given <ms>', ms)
  .option('-t, --type <name>', 'filter using the given type <name>')
  .option('-l, --level <name>', 'filter using the given level <name>')
  .option('-f, --ignore-eof', 'do not stop on EOF')
  .option('-F, --file <path>', 'load from the given <path>')
  .option('-R, --redis', 'load from redis store')
  .option('-s, --select <fn>', 'use the given <fn> for filtering')
  .option('-m, --map <fn>', 'use the given <fn> for mapping')
  .option('-r, --reduce <fn>', 'use the given <fn> for reducing')
  .option('-c, --color', 'color the output')
  .option('-j, --json', 'output JSON (--color will not work)')
  .parse(process.argv);

// determine store

var store = program.file
  ? new jog.FileStore(program.file)
  : program.redis
    ? new jog.RedisStore
    : null;

// no store

if (!store) throw new Error('store required, use --file or --redis');

// -f

var ignoreEOF = program.ignoreEof;

// --within

if (program.within) {
  selects.push(new Function('_', 'return Date.now() - _.timestamp <= ' + program.within));
}

// --reduce

if (program.reduce) {
  var reduce = new Function('$, _', 'return ' + program.reduce);
}

// setup

var log = jog(store)
  , stream = log.stream({ end: !ignoreEOF })
  , results = []
  , prop;

// stream data

stream.on('data', function(line){
  var ret;

  // select
  if (selects.length) {
    ret = line;
    for (var i = 0, len = selects.length; i < len; ++i) {
      if (!selects[i](line)) {
        ret = undefined;
        break;
      }
    }
  } else {
    ret = line;
  }

  // map
  if (undefined === ret) return;
  for (var i = 0, len = maps.length; i < len; ++i) {
    ret = maps[i](ret);
  }

  if (ignoreEOF) {
    output(ret);
  } else {
    results.push(ret);
  }
});

// output results

stream.on('end', function(){
  if (ignoreEOF) return;
  if (reduce) results = results.reduce(reduce);
  output(results);
});

/**
* Output `obj`.
*/

function output(obj) {
  if (program.json) {
    console.log(JSON.stringify(obj, null, 2));
  } else if (program.color) {
    console.log(util.inspect(obj, false, 15, true));
  } else {
    console.log(obj);
  }
}
Something went wrong with that request. Please try again.