2012-01-31 00:07:08 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
//
|
|
|
|
// bunyan -- filter and pretty-print JSON logs, like Bunyan logs.
|
|
|
|
//
|
|
|
|
// See <https://github.com/trentm/node-bunyan>.
|
|
|
|
//
|
|
|
|
|
2013-01-19 00:27:53 +00:00
|
|
|
var VERSION = "0.18.0";
|
2012-01-31 00:07:08 +00:00
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var pathlib = require('path');
|
|
|
|
var vm = require('vm');
|
2012-02-04 01:05:13 +00:00
|
|
|
var http = require('http');
|
2012-02-24 05:19:28 +00:00
|
|
|
var fs = require('fs');
|
2012-01-31 00:07:08 +00:00
|
|
|
var warn = console.warn;
|
2012-11-13 08:12:57 +00:00
|
|
|
var child_process = require('child_process'),
|
|
|
|
spawn = child_process.spawn,
|
|
|
|
exec = child_process.exec,
|
|
|
|
execFile = child_process.execFile;
|
2013-01-19 00:27:53 +00:00
|
|
|
var assert = require('assert');
|
2012-01-31 00:07:08 +00:00
|
|
|
|
2012-12-14 19:47:38 +00:00
|
|
|
var nodeSpawnSupportsStdio = (
|
|
|
|
Number(process.version.split('.')[0]) >= 0
|
|
|
|
|| Number(process.version.split('.')[1]) >= 8);
|
2012-11-05 06:11:55 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
//---- globals and constants
|
|
|
|
|
2013-01-19 00:27:53 +00:00
|
|
|
// Internal debug logging via `console.warn`.
|
|
|
|
var _DEBUG = false;
|
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
// Output modes.
|
|
|
|
var OM_PAUL = 1;
|
|
|
|
var OM_JSON = 2;
|
|
|
|
var OM_INSPECT = 3;
|
|
|
|
var OM_SIMPLE = 4;
|
2012-08-13 22:59:59 +00:00
|
|
|
var OM_SHORT = 5;
|
2012-01-31 00:07:08 +00:00
|
|
|
var OM_FROM_NAME = {
|
|
|
|
"paul": OM_PAUL,
|
|
|
|
"json": OM_JSON,
|
|
|
|
"inspect": OM_INSPECT,
|
2012-08-13 22:59:59 +00:00
|
|
|
"simple": OM_SIMPLE,
|
|
|
|
"short": OM_SHORT
|
2012-05-11 23:22:28 +00:00
|
|
|
};
|
2012-01-31 00:07:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Levels
|
2012-02-06 23:23:51 +00:00
|
|
|
var TRACE = 10;
|
|
|
|
var DEBUG = 20;
|
|
|
|
var INFO = 30;
|
|
|
|
var WARN = 40;
|
|
|
|
var ERROR = 50;
|
|
|
|
var FATAL = 60;
|
2012-01-31 00:07:08 +00:00
|
|
|
|
|
|
|
var levelFromName = {
|
2012-01-31 22:43:13 +00:00
|
|
|
'trace': TRACE,
|
2012-01-31 00:07:08 +00:00
|
|
|
'debug': DEBUG,
|
|
|
|
'info': INFO,
|
|
|
|
'warn': WARN,
|
|
|
|
'error': ERROR,
|
|
|
|
'fatal': FATAL
|
|
|
|
};
|
2012-02-06 23:23:51 +00:00
|
|
|
var nameFromLevel = {};
|
|
|
|
var upperNameFromLevel = {};
|
2012-02-23 05:03:03 +00:00
|
|
|
var upperPaddedNameFromLevel = {};
|
2012-02-06 23:23:51 +00:00
|
|
|
Object.keys(levelFromName).forEach(function (name) {
|
|
|
|
var lvl = levelFromName[name];
|
|
|
|
nameFromLevel[lvl] = name;
|
|
|
|
upperNameFromLevel[lvl] = name.toUpperCase();
|
2012-02-23 05:03:03 +00:00
|
|
|
upperPaddedNameFromLevel[lvl] = (
|
|
|
|
name.length === 4 ? ' ' : '') + name.toUpperCase();
|
2012-02-06 23:23:51 +00:00
|
|
|
});
|
2012-01-31 00:07:08 +00:00
|
|
|
|
2012-08-08 18:57:13 +00:00
|
|
|
|
2012-10-21 23:23:46 +00:00
|
|
|
// The current raw input line being processed. Used for `uncaughtException`.
|
|
|
|
var currLine = null;
|
|
|
|
|
2012-11-02 19:15:29 +00:00
|
|
|
// Child dtrace process, if any. Used for signal-handling.
|
|
|
|
var child = null;
|
|
|
|
|
2012-11-15 03:27:53 +00:00
|
|
|
// Whether ANSI codes are being used. Used for signal-handling.
|
|
|
|
var usingAnsiCodes = false;
|
|
|
|
|
2012-12-14 19:47:38 +00:00
|
|
|
// Global ref to options used only by 'uncaughtException' handler.
|
|
|
|
var gOptsForUncaughtException;
|
|
|
|
|
2013-01-19 00:27:53 +00:00
|
|
|
// Pager child process, and output stream to which to write.
|
|
|
|
var pager = null;
|
|
|
|
var stdout = process.stdout;
|
|
|
|
|
2012-10-21 23:23:46 +00:00
|
|
|
|
2012-08-08 18:57:13 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
//---- support functions
|
|
|
|
|
|
|
|
function getVersion() {
|
|
|
|
return VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var format = util.format;
|
|
|
|
if (!format) {
|
|
|
|
// If not node 0.6, then use its `util.format`:
|
|
|
|
// <https://github.com/joyent/node/blob/master/lib/util.js#L22>:
|
|
|
|
var inspect = util.inspect;
|
|
|
|
var formatRegExp = /%[sdj%]/g;
|
|
|
|
format = function format(f) {
|
|
|
|
if (typeof f !== 'string') {
|
|
|
|
var objects = [];
|
|
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
|
|
objects.push(inspect(arguments[i]));
|
|
|
|
}
|
|
|
|
return objects.join(' ');
|
|
|
|
}
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
var i = 1;
|
|
|
|
var args = arguments;
|
|
|
|
var len = args.length;
|
|
|
|
var str = String(f).replace(formatRegExp, function(x) {
|
|
|
|
if (i >= len) return x;
|
|
|
|
switch (x) {
|
|
|
|
case '%s': return String(args[i++]);
|
|
|
|
case '%d': return Number(args[i++]);
|
|
|
|
case '%j': return JSON.stringify(args[i++]);
|
|
|
|
case '%%': return '%';
|
|
|
|
default:
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (var x = args[i]; i < len; x = args[++i]) {
|
|
|
|
if (x === null || typeof x !== 'object') {
|
|
|
|
str += ' ' + x;
|
|
|
|
} else {
|
|
|
|
str += ' ' + inspect(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-02-02 05:33:18 +00:00
|
|
|
function indent(s) {
|
2012-05-11 23:22:28 +00:00
|
|
|
return ' ' + s.split(/\r?\n/).join('\n ');
|
2012-02-02 05:33:18 +00:00
|
|
|
}
|
2012-01-31 00:07:08 +00:00
|
|
|
|
2012-02-08 23:18:07 +00:00
|
|
|
function objCopy(obj) {
|
|
|
|
if (obj === null) {
|
|
|
|
return null;
|
|
|
|
} else if (Array.isArray(obj)) {
|
|
|
|
return obj.slice();
|
|
|
|
} else {
|
|
|
|
var copy = {};
|
|
|
|
Object.keys(obj).forEach(function (k) {
|
|
|
|
copy[k] = obj[k];
|
|
|
|
});
|
|
|
|
return copy;
|
|
|
|
}
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function printHelp() {
|
2012-08-09 16:10:28 +00:00
|
|
|
console.log("Usage:");
|
|
|
|
console.log(" bunyan [OPTIONS] [FILE ...]");
|
2012-11-02 06:49:09 +00:00
|
|
|
console.log(" ... | bunyan [OPTIONS]");
|
|
|
|
console.log(" bunyan [OPTIONS] -p PID");
|
2012-08-09 16:10:28 +00:00
|
|
|
console.log("");
|
|
|
|
console.log("Filter and pretty-print Bunyan log file content.");
|
|
|
|
console.log("");
|
|
|
|
console.log("General options:");
|
|
|
|
console.log(" -h, --help print this help info and exit");
|
|
|
|
console.log(" --version print version of this command and exit");
|
|
|
|
console.log("");
|
2012-11-02 06:49:09 +00:00
|
|
|
console.log("Dtrace options (only on dtrace-supporting platforms):");
|
|
|
|
console.log(" -p PID Process bunyan:log-* probes from the process");
|
2012-11-13 08:12:57 +00:00
|
|
|
console.log(" with the given PID. Can be used multiple times,");
|
|
|
|
console.log(" or specify all processes with '*', or a set of");
|
|
|
|
console.log(" processes whose command & args match a pattern");
|
|
|
|
console.log(" with '-p NAME'.");
|
2012-11-02 06:49:09 +00:00
|
|
|
console.log("");
|
2012-08-09 16:10:28 +00:00
|
|
|
console.log("Filtering options:");
|
|
|
|
console.log(" -l, --level LEVEL");
|
|
|
|
console.log(" Only show messages at or above the specified level.");
|
|
|
|
console.log(" You can specify level *names* or numeric values.");
|
|
|
|
console.log(" (See 'Log Levels' below.)");
|
|
|
|
console.log(" -c, --condition CONDITION");
|
2012-12-14 19:47:38 +00:00
|
|
|
console.log(" Run each log message through the condition and");
|
|
|
|
console.log(" only show those that return truish. E.g.:");
|
|
|
|
console.log(" -c 'this.pid == 123'");
|
|
|
|
console.log(" -c 'this.level == DEBUG'");
|
|
|
|
console.log(" -c 'this.msg.indexOf(\"boom\") != -1'");
|
|
|
|
console.log(" 'CONDITION' must be legal JS code. `this` holds");
|
|
|
|
console.log(" the log record. The TRACE, DEBUG, ... FATAL values");
|
|
|
|
console.log(" are defined to help with comparing `this.level`.");
|
2012-08-09 16:29:50 +00:00
|
|
|
console.log(" --strict Suppress all but legal Bunyan JSON log lines. By default");
|
|
|
|
console.log(" non-JSON, and non-Bunyan lines are passed through.");
|
2012-08-09 16:10:28 +00:00
|
|
|
console.log("");
|
|
|
|
console.log("Output options:");
|
2013-01-19 00:27:53 +00:00
|
|
|
console.log(" --pager Pipe output into `less` (or $PAGER if set), if");
|
|
|
|
console.log(" stdout is a terminal. This overrides $BUNYAN_NO_PAGER.");
|
|
|
|
console.log(" Note: Paging is only supported on node >=0.8.");
|
|
|
|
console.log(" --no-pager Do not pipe output into a pager.");
|
2012-08-09 16:10:28 +00:00
|
|
|
console.log(" --color Colorize output. Defaults to try if output");
|
|
|
|
console.log(" stream is a TTY.");
|
|
|
|
console.log(" --no-color Force no coloring (e.g. terminal doesn't support it)");
|
|
|
|
console.log(" -o, --output MODE");
|
|
|
|
console.log(" Specify an output mode/format. One of");
|
|
|
|
console.log(" paul: (the default) pretty");
|
|
|
|
console.log(" json: JSON output, 2-space indent");
|
|
|
|
console.log(" json-N: JSON output, N-space indent, e.g. 'json-4'");
|
|
|
|
console.log(" inspect: node.js `util.inspect` output");
|
2012-08-13 22:59:59 +00:00
|
|
|
console.log(" short: like paul, but more concise");
|
2012-08-09 16:10:28 +00:00
|
|
|
console.log(" -j shortcut for `-o json`");
|
|
|
|
console.log("");
|
|
|
|
console.log("Log Levels:");
|
|
|
|
console.log(" Either numeric values or their associated strings are valid for the");
|
|
|
|
console.log(" -l|--level argument. However, -c|--condition scripts will see a numeric");
|
|
|
|
console.log(" 'level' value, not a string.");
|
|
|
|
console.log("");
|
2012-08-08 00:30:39 +00:00
|
|
|
Object.keys(levelFromName).forEach(function(name) {
|
|
|
|
var n = name;
|
|
|
|
while (n.length < 6)
|
|
|
|
n += " ";
|
2012-08-08 05:13:38 +00:00
|
|
|
console.log(" %s %d", n, levelFromName[name]);
|
2012-08-08 00:30:39 +00:00
|
|
|
});
|
2012-08-09 16:10:28 +00:00
|
|
|
console.log("");
|
2012-11-06 20:50:39 +00:00
|
|
|
console.log("Environment Variables:");
|
|
|
|
console.log(" BUNYAN_NO_COLOR Set to a non-empty value to force no output ");
|
|
|
|
console.log(" coloring. See '--no-color'.");
|
2013-01-19 00:27:53 +00:00
|
|
|
console.log(" BUNYAN_NO_PAGER Disable piping output to a pager. ");
|
|
|
|
console.log(" See '--no-pager'.");
|
2012-11-06 20:50:39 +00:00
|
|
|
console.log("");
|
2012-08-09 16:10:28 +00:00
|
|
|
console.log("See <https://github.com/trentm/node-bunyan> for more complete docs.");
|
|
|
|
console.log("Please report bugs to <https://github.com/trentm/node-bunyan/issues>.");
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
|
2012-05-11 23:22:28 +00:00
|
|
|
/*
|
|
|
|
* If the user specifies multiple input sources, we want to print out records
|
|
|
|
* from all sources in a single, chronologically ordered stream. To do this
|
|
|
|
* efficiently, we first assume that all records within each source are ordered
|
|
|
|
* already, so we need only keep track of the next record in each source and
|
|
|
|
* the time of the last record emitted. To avoid excess memory usage, we
|
|
|
|
* pause() streams that are ahead of others.
|
|
|
|
*
|
|
|
|
* "streams" is an object indexed by source name (file name) which specifies:
|
|
|
|
*
|
|
|
|
* stream Actual stream object, so that we can pause and resume it.
|
|
|
|
*
|
|
|
|
* records Array of log records we've read, but not yet emitted. Each
|
|
|
|
* record includes "line" (the raw line), "rec" (the JSON
|
|
|
|
* record), and "time" (the parsed time value).
|
|
|
|
*
|
|
|
|
* done Whether the stream has any more records to emit.
|
|
|
|
*/
|
|
|
|
var streams = {};
|
|
|
|
|
|
|
|
function gotRecord(file, line, rec, opts, stylize)
|
|
|
|
{
|
|
|
|
var time = new Date(rec.time);
|
|
|
|
|
|
|
|
streams[file]['records'].push({ line: line, rec: rec, time: time });
|
|
|
|
emitNextRecord(opts, stylize);
|
|
|
|
}
|
|
|
|
|
2012-08-08 00:30:39 +00:00
|
|
|
function filterRecord(rec, opts)
|
|
|
|
{
|
|
|
|
if (opts.level && rec.level < opts.level) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-08 00:31:39 +00:00
|
|
|
if (opts.conditions) {
|
|
|
|
for (var i = 0; i < opts.conditions.length; i++) {
|
|
|
|
var pass = opts.conditions[i].runInNewContext(rec);
|
|
|
|
if (!pass)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-08 00:30:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-11 23:22:28 +00:00
|
|
|
function emitNextRecord(opts, stylize)
|
|
|
|
{
|
|
|
|
var ofile, ready, minfile, rec;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
|
|
* Take a first pass through the input streams to see if we have a record
|
|
|
|
* from all of them. If not, we'll pause any streams for which we do
|
|
|
|
* already have a record (to avoid consuming excess memory) and then wait
|
|
|
|
* until we have records from the others before emitting the next record.
|
|
|
|
*
|
|
|
|
* As part of the same pass, we look for the earliest record we have not yet
|
|
|
|
* emitted.
|
|
|
|
*/
|
|
|
|
minfile = undefined;
|
|
|
|
ready = true;
|
|
|
|
for (ofile in streams) {
|
|
|
|
|
|
|
|
if (streams[ofile].stream === null ||
|
|
|
|
(!streams[ofile].done && streams[ofile].records.length === 0)) {
|
|
|
|
ready = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (streams[ofile].records.length > 0 && (minfile === undefined ||
|
|
|
|
streams[minfile].records[0].time > streams[ofile].records[0].time)) {
|
|
|
|
minfile = ofile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-21 20:29:42 +00:00
|
|
|
if (!ready || minfile === undefined) {
|
2012-05-11 23:22:28 +00:00
|
|
|
for (ofile in streams) {
|
|
|
|
if (!streams[ofile].stream || streams[ofile].done)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (streams[ofile].records.length > 0) {
|
2012-06-20 23:26:28 +00:00
|
|
|
if (!streams[ofile].paused) {
|
2012-05-11 23:22:28 +00:00
|
|
|
streams[ofile].paused = true;
|
|
|
|
streams[ofile].stream.pause();
|
2012-06-20 23:26:28 +00:00
|
|
|
}
|
2012-05-11 23:22:28 +00:00
|
|
|
} else if (streams[ofile].paused) {
|
|
|
|
streams[ofile].paused = false;
|
|
|
|
streams[ofile].stream.resume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emit the next record for "minfile", and invoke ourselves again to make
|
|
|
|
* sure we emit as many records as we can right now.
|
|
|
|
*/
|
|
|
|
rec = streams[minfile].records.shift();
|
|
|
|
emitRecord(rec.rec, rec.line, opts, stylize);
|
|
|
|
}
|
|
|
|
}
|
2012-01-31 00:07:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the command-line options and arguments into an object.
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* 'args': [...] // arguments
|
|
|
|
* 'help': true, // true if '-h' option given
|
|
|
|
* // etc.
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @return {Object} The parsed options. `.args` is the argument list.
|
|
|
|
* @throws {Error} If there is an error parsing argv.
|
|
|
|
*/
|
|
|
|
function parseArgv(argv) {
|
|
|
|
var parsed = {
|
|
|
|
args: [],
|
|
|
|
help: false,
|
2012-11-06 20:50:39 +00:00
|
|
|
color: null,
|
2013-01-19 00:27:53 +00:00
|
|
|
paginate: null,
|
2012-01-31 00:07:08 +00:00
|
|
|
outputMode: OM_PAUL,
|
2012-08-08 00:30:39 +00:00
|
|
|
jsonIndent: 2,
|
2012-08-08 04:13:59 +00:00
|
|
|
level: null,
|
2012-08-09 16:29:50 +00:00
|
|
|
conditions: null,
|
2012-11-02 06:49:09 +00:00
|
|
|
strict: false,
|
2012-11-13 08:12:57 +00:00
|
|
|
pids: null,
|
|
|
|
pidsType: null
|
2012-01-31 00:07:08 +00:00
|
|
|
};
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
// Turn '-iH' into '-i -H', except for argument-accepting options.
|
|
|
|
var args = argv.slice(2); // drop ['node', 'scriptname']
|
|
|
|
var newArgs = [];
|
2012-11-02 06:49:09 +00:00
|
|
|
var optTakesArg = {'d': true, 'o': true, 'c': true, 'l': true, 'p': true};
|
2012-01-31 00:07:08 +00:00
|
|
|
for (var i = 0; i < args.length; i++) {
|
|
|
|
if (args[i].charAt(0) === "-" && args[i].charAt(1) !== '-' && args[i].length > 2) {
|
|
|
|
var splitOpts = args[i].slice(1).split("");
|
|
|
|
for (var j = 0; j < splitOpts.length; j++) {
|
2012-05-11 23:22:28 +00:00
|
|
|
newArgs.push('-' + splitOpts[j]);
|
2012-01-31 00:07:08 +00:00
|
|
|
if (optTakesArg[splitOpts[j]]) {
|
|
|
|
var optArg = splitOpts.slice(j+1).join("");
|
|
|
|
if (optArg.length) {
|
|
|
|
newArgs.push(optArg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newArgs.push(args[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args = newArgs;
|
|
|
|
|
2012-08-14 03:15:06 +00:00
|
|
|
var condDefines = [];
|
|
|
|
Object.keys(upperNameFromLevel).forEach(function (lvl) {
|
|
|
|
condDefines.push(
|
|
|
|
format("Object.prototype.%s = %s;", upperNameFromLevel[lvl], lvl));
|
|
|
|
});
|
|
|
|
condDefines = condDefines.join('\n') + '\n';
|
|
|
|
|
2012-05-11 23:22:28 +00:00
|
|
|
var endOfOptions = false;
|
2012-01-31 00:07:08 +00:00
|
|
|
while (args.length > 0) {
|
|
|
|
var arg = args.shift();
|
|
|
|
switch(arg) {
|
|
|
|
case "--":
|
|
|
|
endOfOptions = true;
|
|
|
|
break;
|
|
|
|
case "-h": // display help and exit
|
|
|
|
case "--help":
|
|
|
|
parsed.help = true;
|
|
|
|
break;
|
|
|
|
case "--version":
|
|
|
|
parsed.version = true;
|
|
|
|
break;
|
2012-08-09 16:29:50 +00:00
|
|
|
case "--strict":
|
|
|
|
parsed.strict = true;
|
|
|
|
break;
|
2012-02-23 05:03:03 +00:00
|
|
|
case "--color":
|
|
|
|
parsed.color = true;
|
2012-01-31 00:07:08 +00:00
|
|
|
break;
|
2012-02-23 21:00:53 +00:00
|
|
|
case "--no-color":
|
|
|
|
parsed.color = false;
|
|
|
|
break;
|
2013-01-19 00:27:53 +00:00
|
|
|
case "--pager":
|
|
|
|
parsed.paginate = true;
|
|
|
|
break;
|
|
|
|
case "--no-pager":
|
|
|
|
parsed.paginate = false;
|
|
|
|
break;
|
2012-01-31 00:07:08 +00:00
|
|
|
case "-o":
|
|
|
|
case "--output":
|
|
|
|
var name = args.shift();
|
|
|
|
var idx = name.lastIndexOf('-');
|
|
|
|
if (idx !== -1) {
|
2012-12-14 19:47:38 +00:00
|
|
|
var indentation = Number(name.slice(idx+1));
|
|
|
|
if (! isNaN(indentation)) {
|
|
|
|
parsed.jsonIndent = indentation;
|
2012-01-31 00:07:08 +00:00
|
|
|
name = name.slice(0, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parsed.outputMode = OM_FROM_NAME[name];
|
|
|
|
if (parsed.outputMode === undefined) {
|
|
|
|
throw new Error("unknown output mode: '"+name+"'");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "-j": // output with JSON.stringify
|
|
|
|
parsed.outputMode = OM_JSON;
|
|
|
|
break;
|
2012-11-02 06:49:09 +00:00
|
|
|
case "-p":
|
2012-11-13 08:12:57 +00:00
|
|
|
if (!parsed.pids) {
|
|
|
|
parsed.pids = [];
|
|
|
|
}
|
2012-11-02 06:49:09 +00:00
|
|
|
var pidArg = args.shift();
|
|
|
|
var pid = +(pidArg);
|
2012-11-13 08:12:57 +00:00
|
|
|
if (!isNaN(pid) || pidArg === '*') {
|
|
|
|
if (parsed.pidsType && parsed.pidsType !== 'num') {
|
|
|
|
throw new Error(format(
|
|
|
|
'cannot mix PID name and number arguments: "%s"', pidArg));
|
|
|
|
}
|
|
|
|
parsed.pidsType = 'num';
|
|
|
|
if (!parsed.pids) {
|
|
|
|
parsed.pids = [];
|
|
|
|
}
|
|
|
|
parsed.pids.push(isNaN(pid) ? pidArg : pid);
|
|
|
|
} else {
|
|
|
|
if (parsed.pidsType && parsed.pidsType !== 'name') {
|
|
|
|
throw new Error(format(
|
|
|
|
'cannot mix PID name and number arguments: "%s"', pidArg));
|
2012-11-06 20:55:02 +00:00
|
|
|
}
|
2012-11-13 08:12:57 +00:00
|
|
|
parsed.pidsType = 'name';
|
|
|
|
parsed.pids = pidArg;
|
2012-11-02 06:49:09 +00:00
|
|
|
}
|
|
|
|
break;
|
2012-08-08 00:30:39 +00:00
|
|
|
case "-l":
|
|
|
|
case "--level":
|
|
|
|
var levelArg = args.shift();
|
|
|
|
var level = +(levelArg);
|
|
|
|
if (isNaN(level)) {
|
|
|
|
level = +levelFromName[levelArg.toLowerCase()];
|
|
|
|
}
|
|
|
|
if (isNaN(level)) {
|
|
|
|
throw new Error("unknown level value: '"+levelArg+"'");
|
|
|
|
}
|
|
|
|
parsed.level = level;
|
|
|
|
break;
|
2012-08-08 00:31:39 +00:00
|
|
|
case "-c":
|
|
|
|
case "--condition":
|
|
|
|
var condition = args.shift();
|
2012-08-09 05:03:57 +00:00
|
|
|
parsed.conditions = parsed.conditions || [];
|
2012-08-08 00:31:39 +00:00
|
|
|
var scriptName = 'bunyan-condition-'+parsed.conditions.length;
|
2012-12-14 19:47:38 +00:00
|
|
|
var code = condDefines + condition;
|
|
|
|
try {
|
|
|
|
var script = vm.createScript(code, scriptName);
|
|
|
|
} catch (compileErr) {
|
|
|
|
throw new Error(format('illegal CONDITION code: %s\n'
|
|
|
|
+ ' CONDITION script:\n'
|
|
|
|
+ '%s\n'
|
|
|
|
+ ' Error:\n'
|
|
|
|
+ '%s',
|
|
|
|
compileErr, indent(code), indent(compileErr.stack)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure this is a reasonably safe CONDITION.
|
|
|
|
try {
|
|
|
|
script.runInNewContext(minValidRecord);
|
|
|
|
} catch (condErr) {
|
|
|
|
throw new Error(format(
|
|
|
|
'CONDITION code cannot safely filter a minimal Bunyan log record\n'
|
|
|
|
+ ' CONDITION script:\n'
|
|
|
|
+ '%s\n'
|
|
|
|
+ ' Minimal Bunyan log record:\n'
|
|
|
|
+ '%s\n'
|
|
|
|
+ ' Filter error:\n'
|
|
|
|
+ '%s',
|
|
|
|
indent(code),
|
|
|
|
indent(JSON.stringify(minValidRecord, null, 2)),
|
|
|
|
indent(condErr.stack)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-08-08 00:31:39 +00:00
|
|
|
parsed.conditions.push(script);
|
2012-08-09 05:03:57 +00:00
|
|
|
break;
|
2012-01-31 00:07:08 +00:00
|
|
|
default: // arguments
|
|
|
|
if (!endOfOptions && arg.length > 0 && arg[0] === '-') {
|
|
|
|
throw new Error("unknown option '"+arg+"'");
|
|
|
|
}
|
|
|
|
parsed.args.push(arg);
|
|
|
|
break;
|
2012-02-10 05:07:01 +00:00
|
|
|
}
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
//TODO: '--' handling and error on a first arg that looks like an option.
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isInteger(s) {
|
|
|
|
return (s.search(/^-?[0-9]+$/) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-23 05:03:03 +00:00
|
|
|
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
|
|
|
|
// Suggested colors (some are unreadable in common cases):
|
|
|
|
// - Good: cyan, yellow (limited use), grey, bold, green, magenta, red
|
|
|
|
// - Bad: blue (not visible on cmd.exe)
|
|
|
|
var colors = {
|
|
|
|
'bold' : [1, 22],
|
|
|
|
'italic' : [3, 23],
|
|
|
|
'underline' : [4, 24],
|
|
|
|
'inverse' : [7, 27],
|
|
|
|
'white' : [37, 39],
|
|
|
|
'grey' : [90, 39],
|
|
|
|
'black' : [30, 39],
|
|
|
|
'blue' : [34, 39],
|
|
|
|
'cyan' : [36, 39],
|
|
|
|
'green' : [32, 39],
|
|
|
|
'magenta' : [35, 39],
|
|
|
|
'red' : [31, 39],
|
|
|
|
'yellow' : [33, 39]
|
|
|
|
};
|
|
|
|
|
|
|
|
function stylizeWithColor(str, color) {
|
2012-02-24 05:19:28 +00:00
|
|
|
if (!str)
|
|
|
|
return '';
|
2012-02-23 05:03:03 +00:00
|
|
|
var codes = colors[color];
|
|
|
|
if (codes) {
|
|
|
|
return '\033[' + codes[0] + 'm' + str +
|
|
|
|
'\033[' + codes[1] + 'm';
|
|
|
|
} else {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function stylizeWithoutColor(str, color) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-24 05:19:28 +00:00
|
|
|
/**
|
2012-05-11 23:22:28 +00:00
|
|
|
* Is this a valid Bunyan log record.
|
2012-02-24 05:19:28 +00:00
|
|
|
*/
|
|
|
|
function isValidRecord(rec) {
|
|
|
|
if (rec.v == null ||
|
|
|
|
rec.level == null ||
|
|
|
|
rec.name == null ||
|
|
|
|
rec.hostname == null ||
|
|
|
|
rec.pid == null ||
|
|
|
|
rec.time == null ||
|
|
|
|
rec.msg == null) {
|
|
|
|
// Not valid Bunyan log.
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-12-14 19:47:38 +00:00
|
|
|
var minValidRecord = {
|
|
|
|
v: 0, //TODO: get this from bunyan.LOG_VERSION
|
|
|
|
level: INFO,
|
|
|
|
name: 'name',
|
|
|
|
hostname: 'hostname',
|
|
|
|
pid: 123,
|
|
|
|
time: Date.now(),
|
|
|
|
msg: 'msg'
|
|
|
|
};
|
2012-02-24 05:19:28 +00:00
|
|
|
|
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
/**
|
2012-05-11 23:22:28 +00:00
|
|
|
* Parses the given log line and either emits it right away (for invalid
|
|
|
|
* records) or enqueues it for emitting later when it's the next line to show.
|
2012-01-31 00:07:08 +00:00
|
|
|
*/
|
2012-05-11 23:22:28 +00:00
|
|
|
function handleLogLine(file, line, opts, stylize) {
|
2012-10-21 23:23:46 +00:00
|
|
|
currLine = line; // intentionally global
|
|
|
|
|
2012-05-11 23:22:28 +00:00
|
|
|
// Emit non-JSON lines immediately.
|
2012-01-31 00:07:08 +00:00
|
|
|
var rec;
|
|
|
|
if (!line) {
|
2012-08-09 16:29:50 +00:00
|
|
|
if (!opts.strict) emit(line + '\n');
|
|
|
|
return;
|
2012-01-31 00:07:08 +00:00
|
|
|
} else if (line[0] !== '{') {
|
2012-08-09 16:29:50 +00:00
|
|
|
if (!opts.strict) emit(line + '\n'); // not JSON
|
|
|
|
return;
|
2012-01-31 00:07:08 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
rec = JSON.parse(line);
|
|
|
|
} catch(e) {
|
2012-08-09 16:29:50 +00:00
|
|
|
if (!opts.strict) emit(line + '\n');
|
|
|
|
return;
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 16:29:50 +00:00
|
|
|
if (!isValidRecord(rec)) {
|
2013-01-07 19:18:12 +00:00
|
|
|
if (!opts.strict) emit(line + '\n');
|
2012-08-09 16:29:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-08-08 00:30:39 +00:00
|
|
|
|
|
|
|
if (!filterRecord(rec, opts))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (file === null)
|
2012-05-11 23:22:28 +00:00
|
|
|
return emitRecord(rec, line, opts, stylize);
|
|
|
|
|
|
|
|
return gotRecord(file, line, rec, opts, stylize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print out a single result, considering input options.
|
|
|
|
*/
|
|
|
|
function emitRecord(rec, line, opts, stylize) {
|
2012-08-13 22:59:59 +00:00
|
|
|
var short = false;
|
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
switch (opts.outputMode) {
|
2012-08-13 22:59:59 +00:00
|
|
|
case OM_SHORT:
|
|
|
|
short = true;
|
|
|
|
/* jsl:fall-thru */
|
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
case OM_PAUL:
|
2012-02-10 05:07:01 +00:00
|
|
|
// [time] LEVEL: name[/component]/pid on hostname (src): msg* (extras...)
|
2012-02-08 23:18:07 +00:00
|
|
|
// msg*
|
|
|
|
// --
|
|
|
|
// long and multi-line extras
|
|
|
|
// ...
|
|
|
|
// If 'msg' is single-line, then it goes in the top line.
|
2012-02-02 05:33:18 +00:00
|
|
|
// If 'req', show the request.
|
|
|
|
// If 'res', show the response.
|
|
|
|
// If 'err' and 'err.stack' then show that.
|
2012-02-24 05:19:28 +00:00
|
|
|
if (!isValidRecord(rec)) {
|
|
|
|
return emit(line + '\n');
|
|
|
|
}
|
|
|
|
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.v;
|
|
|
|
|
2012-08-13 22:59:59 +00:00
|
|
|
/*
|
|
|
|
* We assume the Date is formatted according to ISO8601, in which case we
|
|
|
|
* can safely chop off the date information.
|
|
|
|
*/
|
|
|
|
if (short && rec.time[10] == 'T') {
|
|
|
|
var time = rec.time.substr(11);
|
|
|
|
time = stylize(time, 'XXX');
|
|
|
|
} else {
|
|
|
|
var time = stylize('[' + rec.time + ']', 'XXX');
|
|
|
|
}
|
|
|
|
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.time;
|
|
|
|
|
2012-02-06 23:13:50 +00:00
|
|
|
var nameStr = rec.name;
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.name;
|
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
if (rec.component) {
|
2012-02-06 23:13:50 +00:00
|
|
|
nameStr += '/' + rec.component;
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.component;
|
|
|
|
|
2012-08-13 22:59:59 +00:00
|
|
|
if (!short)
|
|
|
|
nameStr += '/' + rec.pid;
|
2012-02-10 05:07:01 +00:00
|
|
|
delete rec.pid;
|
|
|
|
|
2012-02-23 05:03:03 +00:00
|
|
|
var level = (upperPaddedNameFromLevel[rec.level] || "LVL" + rec.level);
|
|
|
|
if (opts.color) {
|
|
|
|
var colorFromLevel = {
|
|
|
|
10: 'grey', // TRACE
|
|
|
|
20: 'grey', // DEBUG
|
|
|
|
30: 'cyan', // INFO
|
|
|
|
40: 'magenta', // WARN
|
|
|
|
50: 'red', // ERROR
|
|
|
|
60: 'inverse', // FATAL
|
2012-05-11 23:22:28 +00:00
|
|
|
};
|
|
|
|
level = stylize(level, colorFromLevel[rec.level]);
|
2012-02-23 05:03:03 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.level;
|
|
|
|
|
2012-02-06 04:33:57 +00:00
|
|
|
var src = "";
|
|
|
|
if (rec.src && rec.src.file) {
|
|
|
|
var s = rec.src;
|
|
|
|
if (s.func) {
|
|
|
|
src = format(" (%s:%d in %s)", s.file, s.line, s.func);
|
|
|
|
} else {
|
|
|
|
src = format(" (%s:%d)", s.file, s.line);
|
|
|
|
}
|
2012-04-10 22:32:33 +00:00
|
|
|
src = stylize(src, 'green');
|
2012-02-06 04:33:57 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.src;
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-02-08 23:18:07 +00:00
|
|
|
var hostname = rec.hostname;
|
|
|
|
delete rec.hostname;
|
|
|
|
|
2012-01-31 00:45:25 +00:00
|
|
|
var extras = [];
|
2012-02-02 05:33:18 +00:00
|
|
|
var details = [];
|
2012-02-08 23:18:07 +00:00
|
|
|
|
|
|
|
if (rec.req_id) {
|
|
|
|
extras.push("req_id=" + rec.req_id);
|
|
|
|
}
|
|
|
|
delete rec.req_id;
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-02-23 05:03:03 +00:00
|
|
|
var onelineMsg;
|
2012-01-31 00:07:08 +00:00
|
|
|
if (rec.msg.indexOf('\n') !== -1) {
|
2012-02-02 05:33:18 +00:00
|
|
|
onelineMsg = '';
|
2012-02-23 05:03:03 +00:00
|
|
|
details.push(indent(stylize(rec.msg, 'cyan')));
|
|
|
|
} else {
|
|
|
|
onelineMsg = ' ' + stylize(rec.msg, 'cyan');
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
delete rec.msg;
|
|
|
|
|
2012-10-10 18:39:29 +00:00
|
|
|
if (rec.req && typeof(rec.req) === 'object') {
|
2012-08-21 18:04:33 +00:00
|
|
|
var req = rec.req;
|
2012-10-10 18:39:29 +00:00
|
|
|
delete rec.req;
|
2012-08-21 18:04:33 +00:00
|
|
|
var headers = req.headers;
|
|
|
|
var s = format("%s %s HTTP/%s%s", req.method,
|
|
|
|
req.url,
|
|
|
|
req.httpVersion || "1.1",
|
2012-08-09 05:27:49 +00:00
|
|
|
(headers
|
|
|
|
? '\n' + Object.keys(headers).map(
|
|
|
|
function (h) { return h + ': ' + headers[h]; }).join('\n')
|
|
|
|
: '')
|
2012-08-21 18:04:33 +00:00
|
|
|
);
|
|
|
|
delete req.url;
|
|
|
|
delete req.method;
|
|
|
|
delete req.httpVersion;
|
|
|
|
delete req.headers;
|
|
|
|
if (req.body) {
|
2012-08-28 17:40:58 +00:00
|
|
|
s += '\n\n' + (typeof(req.body) === 'object'
|
|
|
|
? JSON.stringify(req.body, null, 2) : req.body);
|
2012-08-21 18:04:33 +00:00
|
|
|
delete req.body;
|
|
|
|
}
|
|
|
|
if (req.trailers && Object.keys(req.trailers) > 0) {
|
|
|
|
s += '\n' + Object.keys(req.trailers).map(
|
|
|
|
function (t) { return t + ': ' + req.trailers[t]; }).join('\n');
|
|
|
|
}
|
|
|
|
delete req.trailers;
|
|
|
|
details.push(indent(s));
|
|
|
|
// E.g. for extra 'foo' field on 'req', add 'req.foo' at top-level.
|
|
|
|
// This *does* have the potential to stomp on a literal 'req.foo' key.
|
|
|
|
Object.keys(req).forEach(function (k) {
|
|
|
|
rec["req." + k] = req[k];
|
|
|
|
})
|
2012-02-02 05:33:18 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
|
2012-10-10 18:39:29 +00:00
|
|
|
if (rec.client_req && typeof(rec.client_req) === 'object') {
|
2012-08-21 18:04:33 +00:00
|
|
|
var client_req = rec.client_req;
|
2012-10-10 18:39:29 +00:00
|
|
|
delete rec.client_req;
|
2012-08-21 18:04:33 +00:00
|
|
|
var headers = client_req.headers;
|
2012-02-08 23:27:14 +00:00
|
|
|
var hostHeaderLine = '';
|
2012-08-21 18:04:33 +00:00
|
|
|
var s = '';
|
|
|
|
if (client_req.address) {
|
|
|
|
hostHeaderLine = 'Host: ' + client_req.address;
|
|
|
|
if (client_req.port)
|
|
|
|
hostHeaderLine += ':' + client_req.port;
|
2012-02-08 23:27:14 +00:00
|
|
|
hostHeaderLine += '\n';
|
|
|
|
}
|
2012-08-21 18:04:33 +00:00
|
|
|
delete client_req.headers;
|
|
|
|
delete client_req.address;
|
|
|
|
delete client_req.port;
|
|
|
|
s += format("%s %s HTTP/%s\n%s%s", client_req.method,
|
|
|
|
client_req.url,
|
2012-08-30 19:16:43 +00:00
|
|
|
client_req.httpVersion || "1.1",
|
2012-02-08 23:27:14 +00:00
|
|
|
hostHeaderLine,
|
|
|
|
Object.keys(headers).map(
|
2012-08-21 18:04:33 +00:00
|
|
|
function (h) { return h + ': ' + headers[h]; }).join('\n'));
|
|
|
|
delete client_req.method;
|
|
|
|
delete client_req.url;
|
|
|
|
delete client_req.httpVersion;
|
|
|
|
if (client_req.body) {
|
2012-08-28 17:40:58 +00:00
|
|
|
s += '\n\n' + (typeof(client_req.body) === 'object'
|
|
|
|
? JSON.stringify(client_req.body, null, 2) : client_req.body);
|
2012-08-21 18:04:33 +00:00
|
|
|
delete client_req.body;
|
|
|
|
}
|
|
|
|
// E.g. for extra 'foo' field on 'client_req', add 'client_req.foo' at
|
|
|
|
// top-level. This *does* have the potential to stomp on a literal
|
|
|
|
// 'client_req.foo' key.
|
|
|
|
Object.keys(client_req).forEach(function (k) {
|
|
|
|
rec["client_req." + k] = client_req[k];
|
|
|
|
})
|
|
|
|
details.push(indent(s));
|
2012-02-08 23:27:14 +00:00
|
|
|
}
|
|
|
|
|
2012-10-10 18:39:29 +00:00
|
|
|
if (rec.res && typeof(rec.res) === 'object') {
|
2012-08-21 18:04:33 +00:00
|
|
|
var res = rec.res;
|
2012-10-10 18:39:29 +00:00
|
|
|
delete rec.res;
|
2012-02-04 01:05:13 +00:00
|
|
|
var s = '';
|
2012-08-21 18:04:33 +00:00
|
|
|
if (res.header) {
|
|
|
|
s += res.header.trimRight();
|
|
|
|
} else if (res.headers) {
|
|
|
|
if (res.statusCode) {
|
|
|
|
s += format("HTTP/1.1 %s %s\n", res.statusCode,
|
|
|
|
http.STATUS_CODES[res.statusCode]);
|
2012-02-04 01:05:13 +00:00
|
|
|
}
|
2012-08-21 18:04:33 +00:00
|
|
|
var headers = res.headers;
|
2012-02-04 01:05:13 +00:00
|
|
|
s += Object.keys(headers).map(
|
2012-05-11 23:22:28 +00:00
|
|
|
function (h) { return h + ': ' + headers[h]; }).join('\n');
|
2012-02-04 01:05:13 +00:00
|
|
|
}
|
2012-08-21 18:04:33 +00:00
|
|
|
delete res.header;
|
|
|
|
delete res.headers;
|
|
|
|
delete res.statusCode;
|
|
|
|
if (res.body) {
|
2012-10-09 22:17:15 +00:00
|
|
|
s += '\n\n' + (typeof(res.body) === 'object'
|
|
|
|
? JSON.stringify(res.body, null, 2) : res.body);
|
2012-08-21 18:04:33 +00:00
|
|
|
delete res.body;
|
|
|
|
}
|
|
|
|
if (res.trailer) {
|
|
|
|
s += '\n' + res.trailer;
|
|
|
|
}
|
|
|
|
delete res.trailer;
|
2012-02-04 01:05:13 +00:00
|
|
|
if (s) {
|
|
|
|
details.push(indent(s));
|
|
|
|
}
|
2012-08-21 18:04:33 +00:00
|
|
|
// E.g. for extra 'foo' field on 'res', add 'res.foo' at top-level.
|
|
|
|
// This *does* have the potential to stomp on a literal 'res.foo' key.
|
|
|
|
Object.keys(res).forEach(function (k) {
|
|
|
|
rec["res." + k] = res[k];
|
2012-10-10 18:39:29 +00:00
|
|
|
});
|
2012-02-02 05:33:18 +00:00
|
|
|
}
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-02-02 05:33:18 +00:00
|
|
|
if (rec.err && rec.err.stack) {
|
2012-05-11 23:22:28 +00:00
|
|
|
details.push(indent(rec.err.stack));
|
2012-02-28 22:45:53 +00:00
|
|
|
delete rec.err;
|
2012-02-02 05:33:18 +00:00
|
|
|
}
|
2012-02-08 23:18:07 +00:00
|
|
|
|
|
|
|
var leftover = Object.keys(rec);
|
|
|
|
for (var i = 0; i < leftover.length; i++) {
|
|
|
|
var key = leftover[i];
|
|
|
|
var value = rec[key];
|
2012-02-12 00:34:40 +00:00
|
|
|
var stringified = false;
|
2012-02-08 23:18:07 +00:00
|
|
|
if (typeof(value) !== 'string') {
|
|
|
|
value = JSON.stringify(value, null, 2);
|
2012-02-12 00:34:40 +00:00
|
|
|
stringified = true;
|
2012-02-08 23:18:07 +00:00
|
|
|
}
|
|
|
|
if (value.indexOf('\n') !== -1 || value.length > 50) {
|
2012-05-11 23:22:28 +00:00
|
|
|
details.push(indent(key + ': ' + value));
|
2012-02-12 00:34:40 +00:00
|
|
|
} else if (!stringified && (value.indexOf(' ') != -1 ||
|
|
|
|
value.length === 0)) {
|
2012-05-11 23:22:28 +00:00
|
|
|
extras.push(key + '=' + JSON.stringify(value));
|
2012-02-08 23:18:07 +00:00
|
|
|
} else {
|
2012-05-11 23:22:28 +00:00
|
|
|
extras.push(key + '=' + value);
|
2012-02-08 23:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 05:03:03 +00:00
|
|
|
extras = stylize(
|
|
|
|
(extras.length ? ' (' + extras.join(', ') + ')' : ''), 'grey');
|
|
|
|
details = stylize(
|
|
|
|
(details.length ? details.join('\n --\n') + '\n' : ''), 'grey');
|
2012-08-13 22:59:59 +00:00
|
|
|
if (!short)
|
|
|
|
emit(format("%s %s: %s on %s%s:%s%s\n%s",
|
|
|
|
time,
|
|
|
|
level,
|
|
|
|
nameStr,
|
|
|
|
hostname || "<no-hostname>",
|
|
|
|
src,
|
|
|
|
onelineMsg,
|
|
|
|
extras,
|
|
|
|
details));
|
|
|
|
else
|
|
|
|
emit(format("%s %s %s:%s%s\n%s",
|
|
|
|
time,
|
|
|
|
level,
|
|
|
|
nameStr,
|
|
|
|
onelineMsg,
|
|
|
|
extras,
|
|
|
|
details));
|
2012-01-31 00:07:08 +00:00
|
|
|
break;
|
2012-02-02 05:33:18 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
case OM_INSPECT:
|
|
|
|
emit(util.inspect(rec, false, Infinity, true) + '\n');
|
|
|
|
break;
|
2012-02-02 05:33:18 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
case OM_JSON:
|
|
|
|
emit(JSON.stringify(rec, null, opts.jsonIndent) + '\n');
|
|
|
|
break;
|
2012-02-02 05:33:18 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
case OM_SIMPLE:
|
|
|
|
// <http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/SimpleLayout.html>
|
2012-02-24 05:19:28 +00:00
|
|
|
if (!isValidRecord(rec)) {
|
|
|
|
return emit(line + '\n');
|
|
|
|
}
|
|
|
|
emit(format("%s - %s\n", upperNameFromLevel[rec.level] || "LVL" + rec.level,
|
2012-01-31 00:07:08 +00:00
|
|
|
rec.msg));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("unknown output mode: "+opts.outputMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var stdoutFlushed = true;
|
|
|
|
function emit(s) {
|
|
|
|
try {
|
2013-01-19 00:27:53 +00:00
|
|
|
stdoutFlushed = stdout.write(s);
|
2012-01-31 00:07:08 +00:00
|
|
|
} catch (e) {
|
2013-01-19 00:27:53 +00:00
|
|
|
// Handle any exceptions in stdout writing in `stdout.on("error", ...)`.
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A hacked up version of "process.exit" that will first drain stdout
|
|
|
|
* before exiting. *WARNING: This doesn't stop event processing.* IOW,
|
|
|
|
* callers have to be careful that code following this call isn't
|
|
|
|
* accidentally executed.
|
|
|
|
*
|
|
|
|
* In node v0.6 "process.stdout and process.stderr are blocking when they
|
|
|
|
* refer to regular files or TTY file descriptors." However, this hack might
|
|
|
|
* still be necessary in a shell pipeline.
|
|
|
|
*/
|
|
|
|
function drainStdoutAndExit(code) {
|
2013-01-19 00:27:53 +00:00
|
|
|
if (_DEBUG) warn('(drainStdoutAndExit(%d))', code);
|
|
|
|
stdout.on('drain', function () {
|
|
|
|
cleanupAndExit(code);
|
2012-01-31 00:07:08 +00:00
|
|
|
});
|
|
|
|
if (stdoutFlushed) {
|
2013-01-19 00:27:53 +00:00
|
|
|
cleanupAndExit(code);
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-24 05:19:28 +00:00
|
|
|
/**
|
|
|
|
* Process all input from stdin.
|
|
|
|
*
|
|
|
|
* @params opts {Object} Bunyan options object.
|
|
|
|
* @param stylize {Function} Output stylize function to use.
|
|
|
|
* @param callback {Function} `function ()`
|
|
|
|
*/
|
|
|
|
function processStdin(opts, stylize, callback) {
|
2012-01-31 00:07:08 +00:00
|
|
|
var leftover = ""; // Left-over partial line from last chunk.
|
2012-02-24 05:19:28 +00:00
|
|
|
var stdin = process.stdin;
|
|
|
|
stdin.resume();
|
2012-01-31 00:07:08 +00:00
|
|
|
stdin.setEncoding('utf8');
|
|
|
|
stdin.on('data', function (chunk) {
|
|
|
|
var lines = chunk.split(/\r\n|\n/);
|
|
|
|
var length = lines.length;
|
|
|
|
if (length === 1) {
|
|
|
|
leftover += lines[0];
|
|
|
|
return;
|
|
|
|
}
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
if (length > 1) {
|
2012-05-11 23:22:28 +00:00
|
|
|
handleLogLine(null, leftover + lines[0], opts, stylize);
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
leftover = lines.pop();
|
|
|
|
length -= 1;
|
|
|
|
for (var i=1; i < length; i++) {
|
2012-05-11 23:22:28 +00:00
|
|
|
handleLogLine(null, lines[i], opts, stylize);
|
2012-01-31 00:07:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
stdin.on('end', function () {
|
|
|
|
if (leftover) {
|
2012-05-11 23:22:28 +00:00
|
|
|
handleLogLine(null, leftover, opts, stylize);
|
2012-01-31 00:07:08 +00:00
|
|
|
leftover = '';
|
|
|
|
}
|
2012-02-24 05:19:28 +00:00
|
|
|
callback();
|
2012-01-31 00:07:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-02-24 05:19:28 +00:00
|
|
|
|
2012-11-02 06:49:09 +00:00
|
|
|
/**
|
|
|
|
* Process bunyan:log-* probes from the given pid.
|
|
|
|
*
|
|
|
|
* @params opts {Object} Bunyan options object.
|
|
|
|
* @param stylize {Function} Output stylize function to use.
|
|
|
|
* @param callback {Function} `function (code)`
|
|
|
|
*/
|
2012-11-13 08:12:57 +00:00
|
|
|
function processPids(opts, stylize, callback) {
|
2012-11-02 06:49:09 +00:00
|
|
|
var leftover = ""; // Left-over partial line from last chunk.
|
2012-11-13 08:12:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the PIDs to dtrace.
|
|
|
|
*
|
|
|
|
* @param cb {Function} `function (errCode, pids)`
|
|
|
|
*/
|
|
|
|
function getPids(cb) {
|
|
|
|
if (opts.pidsType === 'num') {
|
|
|
|
return cb(null, opts.pids);
|
|
|
|
}
|
|
|
|
if (process.platform === 'sunos') {
|
|
|
|
execFile('/bin/pgrep', ['-lf', opts.pids],
|
|
|
|
function (pidsErr, stdout, stderr) {
|
|
|
|
if (pidsErr) {
|
|
|
|
warn('bunyan: error getting PIDs for "%s": %s\n%s\n%s',
|
|
|
|
opts.pids, pidsErr.message, stdout, stderr);
|
|
|
|
return cb(1);
|
|
|
|
}
|
|
|
|
var pids = stdout.trim().split('\n')
|
|
|
|
.map(function (line) { return line.trim().split(/\s+/)[0] })
|
|
|
|
.filter(function (pid) { return Number(pid) !== process.pid });
|
|
|
|
if (pids.length === 0) {
|
|
|
|
warn('bunyan: error: no matching PIDs found for "%s"', opts.pids);
|
|
|
|
return cb(2);
|
|
|
|
}
|
|
|
|
cb(null, pids);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
var regex = opts.pids;
|
|
|
|
if (regex && /[a-zA-Z0-9_]/.test(regex[0])) {
|
|
|
|
// 'foo' -> '[f]oo' trick to exclude the 'grep' PID from its own
|
|
|
|
// search.
|
|
|
|
regex = '[' + regex[0] + ']' + regex.slice(1);
|
|
|
|
}
|
|
|
|
exec(format('ps -A -o pid,command | grep \'%s\'', regex),
|
|
|
|
function (pidsErr, stdout, stderr) {
|
|
|
|
if (pidsErr) {
|
|
|
|
warn('bunyan: error getting PIDs for "%s": %s\n%s\n%s',
|
|
|
|
opts.pids, pidsErr.message, stdout, stderr);
|
|
|
|
return cb(1);
|
|
|
|
}
|
|
|
|
var pids = stdout.trim().split('\n')
|
|
|
|
.map(function (line) { return line.trim().split(/\s+/)[0] })
|
|
|
|
.filter(function (pid) { return Number(pid) !== process.pid });
|
|
|
|
if (pids.length === 0) {
|
|
|
|
warn('bunyan: error: no matching PIDs found for "%s"', opts.pids);
|
|
|
|
return cb(2);
|
|
|
|
}
|
|
|
|
cb(null, pids);
|
|
|
|
}
|
|
|
|
);
|
2012-11-05 06:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-02 06:49:09 +00:00
|
|
|
|
2012-11-13 08:12:57 +00:00
|
|
|
getPids(function (errCode, pids) {
|
|
|
|
if (errCode) {
|
|
|
|
return callback(errCode);
|
2012-11-02 06:49:09 +00:00
|
|
|
}
|
2012-11-13 08:12:57 +00:00
|
|
|
|
|
|
|
var probes = pids.map(function (pid) {
|
|
|
|
return format('bunyan%s:::log-*', pid);
|
|
|
|
}).join(',');
|
|
|
|
var argv = ['dtrace', '-Z', '-x', 'strsize=4k', '-qn',
|
|
|
|
format('%s{printf("%s", copyinstr(arg0))}', probes)];
|
|
|
|
//console.log('dtrace argv: %s', argv);
|
|
|
|
var dtrace = spawn(argv[0], argv.slice(1),
|
|
|
|
// Share the stderr handle to have error output come
|
|
|
|
// straight through. Only supported in v0.8+.
|
|
|
|
{stdio: ['pipe', 'pipe', process.stderr]});
|
|
|
|
child = dtrace; // intentionally global
|
|
|
|
|
|
|
|
function finish(code) {
|
|
|
|
if (leftover) {
|
|
|
|
handleLogLine(null, leftover, opts, stylize);
|
|
|
|
leftover = '';
|
|
|
|
}
|
|
|
|
callback(returnCode);
|
2012-11-02 06:49:09 +00:00
|
|
|
}
|
2012-11-05 06:11:55 +00:00
|
|
|
|
2012-11-13 08:12:57 +00:00
|
|
|
dtrace.stdout.setEncoding('utf8');
|
|
|
|
dtrace.stdout.on('data', function (chunk) {
|
|
|
|
var lines = chunk.split(/\r\n|\n/);
|
|
|
|
var length = lines.length;
|
|
|
|
if (length === 1) {
|
|
|
|
leftover += lines[0];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (length > 1) {
|
|
|
|
handleLogLine(null, leftover + lines[0], opts, stylize);
|
|
|
|
}
|
|
|
|
leftover = lines.pop();
|
|
|
|
length -= 1;
|
|
|
|
for (var i=1; i < length; i++) {
|
|
|
|
handleLogLine(null, lines[i], opts, stylize);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (nodeSpawnSupportsStdio) {
|
|
|
|
dtrace.on('exit', finish);
|
|
|
|
} else {
|
|
|
|
// Fallback (for < v0.8) to pipe the dtrace process' stderr to this stderr.
|
|
|
|
// Wait for all of (1) process 'exit', (2) stderr 'end', and (2) stdout
|
|
|
|
// 'end' before returning to ensure all stderr is flushed (issue #54).
|
|
|
|
var returnCode = null;
|
|
|
|
var eventsRemaining = 3;
|
|
|
|
function countdownToFinish(code) {
|
|
|
|
returnCode = code;
|
|
|
|
eventsRemaining--;
|
|
|
|
if (eventsRemaining == 0) {
|
|
|
|
finish(returnCode);
|
|
|
|
}
|
2012-11-05 06:11:55 +00:00
|
|
|
}
|
2012-11-13 08:12:57 +00:00
|
|
|
dtrace.stderr.pipe(process.stderr);
|
|
|
|
dtrace.stderr.on('end', countdownToFinish);
|
|
|
|
dtrace.stderr.on('end', countdownToFinish);
|
|
|
|
dtrace.on('exit', countdownToFinish);
|
2012-11-02 06:49:09 +00:00
|
|
|
}
|
2012-11-13 08:12:57 +00:00
|
|
|
});
|
2012-11-02 06:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-24 05:19:28 +00:00
|
|
|
/**
|
|
|
|
* Process all input from the given log file.
|
|
|
|
*
|
|
|
|
* @param file {String} Log file path to process.
|
|
|
|
* @params opts {Object} Bunyan options object.
|
|
|
|
* @param stylize {Function} Output stylize function to use.
|
|
|
|
* @param callback {Function} `function ()`
|
|
|
|
*/
|
|
|
|
function processFile(file, opts, stylize, callback) {
|
2012-06-22 05:14:56 +00:00
|
|
|
var stream = fs.createReadStream(file);
|
|
|
|
if (/\.gz$/.test(file)) {
|
|
|
|
stream = stream.pipe(require('zlib').createGunzip());
|
|
|
|
}
|
|
|
|
// Manually decode streams - lazy load here as per node/lib/fs.js
|
|
|
|
var decoder = new (require('string_decoder').StringDecoder)('utf8');
|
2012-02-24 05:19:28 +00:00
|
|
|
|
2012-05-11 23:22:28 +00:00
|
|
|
streams[file].stream = stream;
|
|
|
|
|
2012-02-24 05:19:28 +00:00
|
|
|
stream.on('error', function (err) {
|
2012-06-21 20:29:42 +00:00
|
|
|
streams[file].done = true;
|
2012-02-24 05:19:28 +00:00
|
|
|
callback(err);
|
2012-05-11 23:22:28 +00:00
|
|
|
});
|
2012-02-24 05:19:28 +00:00
|
|
|
|
|
|
|
var leftover = ''; // Left-over partial line from last chunk.
|
2012-06-22 05:14:56 +00:00
|
|
|
stream.on('data', function (data) {
|
|
|
|
var chunk = decoder.write(data);
|
|
|
|
if (!chunk.length) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-24 05:19:28 +00:00
|
|
|
var lines = chunk.split(/\r\n|\n/);
|
|
|
|
var length = lines.length;
|
|
|
|
if (length === 1) {
|
|
|
|
leftover += lines[0];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 1) {
|
2012-05-11 23:22:28 +00:00
|
|
|
handleLogLine(file, leftover + lines[0], opts, stylize);
|
2012-02-24 05:19:28 +00:00
|
|
|
}
|
|
|
|
leftover = lines.pop();
|
|
|
|
length -= 1;
|
|
|
|
for (var i=1; i < length; i++) {
|
2012-05-11 23:22:28 +00:00
|
|
|
handleLogLine(file, lines[i], opts, stylize);
|
2012-02-24 05:19:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('end', function () {
|
2012-06-21 20:29:42 +00:00
|
|
|
streams[file].done = true;
|
2012-02-24 05:19:28 +00:00
|
|
|
if (leftover) {
|
2012-05-11 23:22:28 +00:00
|
|
|
handleLogLine(file, leftover, opts, stylize);
|
2012-02-24 05:19:28 +00:00
|
|
|
leftover = '';
|
2012-06-21 20:29:42 +00:00
|
|
|
} else {
|
|
|
|
emitNextRecord(opts, stylize);
|
2012-02-24 05:19:28 +00:00
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From node async module.
|
|
|
|
*/
|
2012-05-11 23:22:28 +00:00
|
|
|
function asyncForEach(arr, iterator, callback) {
|
|
|
|
callback = callback || function () {};
|
2012-02-24 05:19:28 +00:00
|
|
|
if (!arr.length) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
var completed = 0;
|
2012-05-11 23:22:28 +00:00
|
|
|
arr.forEach(function (x) {
|
|
|
|
iterator(x, function (err) {
|
2012-02-24 05:19:28 +00:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
callback = function () {};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
completed += 1;
|
|
|
|
if (completed === arr.length) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-05-11 23:22:28 +00:00
|
|
|
});
|
2012-02-24 05:19:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-19 00:27:53 +00:00
|
|
|
/**
|
|
|
|
* Cleanup and exit properly.
|
|
|
|
*
|
|
|
|
* Warning: this doesn't stop processing, i.e. process exit might be delayed.
|
|
|
|
* It is up to the caller to ensure that no subsequent bunyan processing
|
|
|
|
* is done after calling this.
|
|
|
|
*
|
|
|
|
* @param code {Number} exit code.
|
|
|
|
* @param signal {String} Optional signal name, if this was exitting because
|
|
|
|
* of a signal.
|
|
|
|
*/
|
|
|
|
var cleanedUp = false;
|
|
|
|
function cleanupAndExit(code, signal) {
|
|
|
|
// Guard one call.
|
|
|
|
if (cleanedUp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cleanedUp = true;
|
|
|
|
if (_DEBUG) warn('(bunyan: cleanupAndExit)');
|
2012-02-24 05:19:28 +00:00
|
|
|
|
2013-01-19 00:27:53 +00:00
|
|
|
// Clear possibly interrupted ANSI code (issue #59).
|
2012-11-15 03:27:53 +00:00
|
|
|
if (usingAnsiCodes) {
|
2013-01-19 00:27:53 +00:00
|
|
|
stdout.write('\033[0m');
|
2012-11-15 03:27:53 +00:00
|
|
|
}
|
2013-01-19 00:27:53 +00:00
|
|
|
|
|
|
|
// Kill possible dtrace child.
|
2012-11-02 19:15:29 +00:00
|
|
|
if (child) {
|
|
|
|
child.kill(signal);
|
|
|
|
}
|
2013-01-19 00:27:53 +00:00
|
|
|
|
|
|
|
if (pager) {
|
|
|
|
// Let pager know that output is done, then wait for pager to exit.
|
|
|
|
stdout.end();
|
|
|
|
pager.on('exit', function (pagerCode) {
|
|
|
|
if (_DEBUG)
|
|
|
|
warn('(bunyan: pager exit -> process.exit(%s))', pagerCode || code);
|
|
|
|
process.exit(pagerCode || code);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (_DEBUG) warn('(bunyan: process.exit(%s))', code);
|
|
|
|
process.exit(code);
|
|
|
|
}
|
2012-11-02 19:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-19 00:27:53 +00:00
|
|
|
|
|
|
|
//---- mainline
|
|
|
|
|
|
|
|
process.on('SIGINT', function () { cleanupAndExit(1, 'SIGINT'); });
|
|
|
|
process.on('SIGQUIT', function () { cleanupAndExit(1, 'SIGQUIT'); });
|
|
|
|
process.on('SIGTERM', function () { cleanupAndExit(1, 'SIGTERM'); });
|
|
|
|
process.on('SIGHUP', function () { cleanupAndExit(1, 'SIGHUP'); });
|
|
|
|
|
2012-10-10 18:28:03 +00:00
|
|
|
process.on('uncaughtException', function (err) {
|
2012-12-14 19:47:38 +00:00
|
|
|
function _indent(s) {
|
2012-10-10 18:28:03 +00:00
|
|
|
var lines = s.split(/\r?\n/);
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
|
|
lines[i] = '* ' + lines[i];
|
|
|
|
}
|
|
|
|
return lines.join('\n');
|
|
|
|
}
|
|
|
|
|
2012-10-21 23:23:46 +00:00
|
|
|
var title = encodeURIComponent(format(
|
2012-12-14 19:47:38 +00:00
|
|
|
"Bunyan %s crashed: %s", getVersion(), String(err)));
|
2012-10-10 18:28:03 +00:00
|
|
|
console.error('* * *');
|
2012-12-14 19:47:38 +00:00
|
|
|
console.error('* The Bunyan CLI crashed!');
|
|
|
|
console.error('*');
|
|
|
|
if (err.name === 'ReferenceError' && gOptsForUncaughtException.conditions) {
|
|
|
|
console.error('* A "ReferenceError" is often the result of given');
|
|
|
|
console.error('* `-c CONDITION` code that doesn\'t guard against undefined');
|
|
|
|
console.error('* values. If that is not the problem:');
|
|
|
|
console.error('*');
|
|
|
|
}
|
|
|
|
console.error('* Please report this issue and include the details below:');
|
2012-10-10 18:28:03 +00:00
|
|
|
console.error('*');
|
2012-10-21 23:23:46 +00:00
|
|
|
console.error('* https://github.com/trentm/node-bunyan/issues/new?title=%s', title);
|
2012-10-10 18:28:03 +00:00
|
|
|
console.error('*');
|
|
|
|
console.error('* * *');
|
|
|
|
console.error('* node version:', process.version);
|
|
|
|
console.error('* bunyan version:', getVersion());
|
2012-10-10 18:39:29 +00:00
|
|
|
console.error('* argv: %j', process.argv);
|
2012-10-21 23:23:46 +00:00
|
|
|
console.error('* log line: %j', currLine);
|
2012-10-10 18:28:03 +00:00
|
|
|
console.error('* stack:');
|
2012-12-14 19:47:38 +00:00
|
|
|
console.error(_indent(err.stack));
|
2012-10-10 18:28:03 +00:00
|
|
|
console.error('* * *');
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2013-01-19 00:27:53 +00:00
|
|
|
|
2012-02-24 05:19:28 +00:00
|
|
|
function main(argv) {
|
|
|
|
try {
|
2012-12-14 19:47:38 +00:00
|
|
|
var opts = parseArgv(argv);
|
2012-02-24 05:19:28 +00:00
|
|
|
} catch (e) {
|
2012-05-11 23:22:28 +00:00
|
|
|
warn("bunyan: error: %s", e.message);
|
2012-02-24 05:19:28 +00:00
|
|
|
return drainStdoutAndExit(1);
|
|
|
|
}
|
2012-12-14 19:47:38 +00:00
|
|
|
gOptsForUncaughtException = opts; // intentionally global
|
2012-02-24 05:19:28 +00:00
|
|
|
if (opts.help) {
|
|
|
|
printHelp();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (opts.version) {
|
2012-08-09 16:10:28 +00:00
|
|
|
console.log("bunyan " + getVersion());
|
2012-02-24 05:19:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-02 06:49:09 +00:00
|
|
|
if (opts.pid && opts.args.length > 0) {
|
2012-11-05 06:17:20 +00:00
|
|
|
warn("bunyan: error: can't use both '-p PID' (%s) and file (%s) args",
|
|
|
|
opts.pid, opts.args.join(' '));
|
2012-11-02 06:49:09 +00:00
|
|
|
return drainStdoutAndExit(1);
|
|
|
|
}
|
2012-11-06 20:50:39 +00:00
|
|
|
if (opts.color === null) {
|
|
|
|
if (process.env.BUNYAN_NO_COLOR &&
|
|
|
|
process.env.BUNYAN_NO_COLOR.length > 0) {
|
|
|
|
opts.color = false;
|
|
|
|
} else {
|
|
|
|
opts.color = process.stdout.isTTY;
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 03:27:53 +00:00
|
|
|
usingAnsiCodes = opts.color; // intentionally global
|
2012-02-24 05:19:28 +00:00
|
|
|
var stylize = (opts.color ? stylizeWithColor : stylizeWithoutColor);
|
|
|
|
|
2013-01-19 00:27:53 +00:00
|
|
|
// Pager.
|
|
|
|
var nodeVer = process.versions.node.split('.').map(Number);
|
|
|
|
var paginate = (process.stdout.isTTY
|
|
|
|
&& process.platform !== 'win32'
|
|
|
|
&& nodeVer >= [0,8,0]
|
|
|
|
&& (opts.paginate === true
|
|
|
|
|| (opts.paginate !== false
|
|
|
|
&& (!process.env.BUNYAN_NO_PAGER
|
|
|
|
|| process.env.BUNYAN_NO_PAGER.length === 0))));
|
|
|
|
if (paginate) {
|
|
|
|
var pagerCmd = process.env.PAGER || 'less';
|
|
|
|
assert.ok(pagerCmd.indexOf('"') === -1 && pagerCmd.indexOf("'") === -1,
|
|
|
|
'cannot parse PAGER quotes yet');
|
|
|
|
var argv = pagerCmd.split(/\s+/g);
|
|
|
|
var env = objCopy(process.env);
|
|
|
|
if (env.LESS === undefined) {
|
|
|
|
// git's default is LESS=FRSX. I don't like the 'S' here because lines
|
|
|
|
// are *typically* wide with bunyan output and scrolling horizontally
|
|
|
|
// is a royal pain.
|
|
|
|
// Note a bug in Mac's `less -F`, such that SIGWINCH can kill it.
|
|
|
|
// If that rears too much then I'll remove 'F' from here.
|
|
|
|
env.LESS = 'FRX';
|
|
|
|
}
|
|
|
|
if (_DEBUG) warn("(pager: argv=%j, env.LESS=%j)", argv, env.LESS);
|
|
|
|
// `pager` and `stdout` intentionally global.
|
|
|
|
pager = spawn(argv[0], argv.slice(1),
|
|
|
|
// Share the stderr handle to have error output come
|
|
|
|
// straight through. Only supported in v0.8+.
|
|
|
|
{env: env, stdio: ['pipe',1,2]});
|
|
|
|
stdout = pager.stdin;
|
|
|
|
|
|
|
|
// Early termination of the pager: just stop.
|
|
|
|
pager.on('exit', function (pagerCode) {
|
|
|
|
if (_DEBUG) warn('(bunyan: pager exit)');
|
|
|
|
pager = null;
|
|
|
|
stdout.end()
|
|
|
|
stdout = process.stdout;
|
|
|
|
cleanupAndExit(pagerCode);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stdout error handling. (Couldn't setup until `stdout` was determined.)
|
|
|
|
stdout.on("error", function (err) {
|
|
|
|
if (_DEBUG) warn('(stdout error event: %s)', err);
|
|
|
|
if (err.code === "EPIPE") {
|
|
|
|
drainStdoutAndExit(0);
|
|
|
|
} else if (err.toString() === 'Error: This socket is closed.') {
|
|
|
|
// Could get this if the pager closes its stdin, but hasn't exited yet.
|
|
|
|
drainStdoutAndExit(1);
|
|
|
|
} else {
|
|
|
|
warn(err);
|
|
|
|
drainStdoutAndExit(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-24 05:19:28 +00:00
|
|
|
var retval = 0;
|
2012-11-13 08:12:57 +00:00
|
|
|
if (opts.pids) {
|
|
|
|
processPids(opts, stylize, function (code) {
|
2013-01-19 00:27:53 +00:00
|
|
|
cleanupAndExit(code);
|
2012-11-02 06:49:09 +00:00
|
|
|
});
|
|
|
|
} else if (opts.args.length > 0) {
|
2012-02-24 05:19:28 +00:00
|
|
|
var files = opts.args;
|
2012-05-11 23:22:28 +00:00
|
|
|
files.forEach(function (file) {
|
|
|
|
streams[file] = { stream: null, records: [], done: false }
|
|
|
|
});
|
|
|
|
asyncForEach(files,
|
2012-02-24 05:19:28 +00:00
|
|
|
function (file, next) {
|
|
|
|
processFile(file, opts, stylize, function (err) {
|
|
|
|
if (err) {
|
|
|
|
warn('bunyan: %s', err.message);
|
|
|
|
retval += 1;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
warn("bunyan: unexpected error: %s", err.stack || err);
|
|
|
|
return drainStdoutAndExit(1);
|
|
|
|
}
|
2013-01-19 00:27:53 +00:00
|
|
|
cleanupAndExit(retval);
|
2012-02-24 05:19:28 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
processStdin(opts, stylize, function () {
|
2013-01-19 00:27:53 +00:00
|
|
|
cleanupAndExit(retval);
|
2012-05-11 23:22:28 +00:00
|
|
|
});
|
2012-02-24 05:19:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
if (require.main === module) {
|
|
|
|
// HACK guard for <https://github.com/trentm/json/issues/24>.
|
|
|
|
// We override the `process.stdout.end` guard that core node.js puts in
|
|
|
|
// place. The real fix is that `.end()` shouldn't be called on stdout
|
2012-02-24 05:19:28 +00:00
|
|
|
// in node core. Node v0.6.9 fixes that. Only guard for v0.6.0..v0.6.8.
|
2012-01-31 00:07:08 +00:00
|
|
|
var nodeVer = process.versions.node.split('.').map(Number);
|
|
|
|
if ([0,6,0] <= nodeVer && nodeVer <= [0,6,8]) {
|
|
|
|
var stdout = process.stdout;
|
|
|
|
stdout.end = stdout.destroy = stdout.destroySoon = function() {
|
|
|
|
/* pass */
|
|
|
|
};
|
|
|
|
}
|
2012-02-10 05:07:01 +00:00
|
|
|
|
2012-01-31 00:07:08 +00:00
|
|
|
main(process.argv);
|
|
|
|
}
|