issue #48: make dtrace-provider presence optional (disabled dtrace probes)

dtrace-provider is still in *dependencies* to strongly encourage
installing it. However I want bunyan.js to be useable for basic
usage standalone.
This commit is contained in:
Trent Mick 2012-10-31 22:46:05 -07:00
parent 2438b0b01c
commit 2c4d4713f2

View file

@ -25,7 +25,11 @@ var os = require('os');
var fs = require('fs'); var fs = require('fs');
var util = require('util'); var util = require('util');
var assert = require('assert'); var assert = require('assert');
try {
var dtrace = require('dtrace-provider'); var dtrace = require('dtrace-provider');
} catch (e) {
dtrace = null;
}
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
@ -48,7 +52,7 @@ function objCopy(obj) {
var format = util.format; var format = util.format;
if (!format) { if (!format) {
// If not node 0.6, then use its `util.format`: // If node < 0.6, then use its `util.format`:
// <https://github.com/joyent/node/blob/master/lib/util.js#L22>: // <https://github.com/joyent/node/blob/master/lib/util.js#L22>:
var inspect = util.inspect; var inspect = util.inspect;
var formatRegExp = /%[sdj%]/g; var formatRegExp = /%[sdj%]/g;
@ -157,8 +161,9 @@ var levelFromName = {
'fatal': FATAL 'fatal': FATAL
}; };
// Dtrace probes.
var dtp = undefined; var dtp = undefined;
var probes = {}; var probes = dtrace && {};
/** /**
* Resolve a level number, name (upper or lowercase) to a level number value. * Resolve a level number, name (upper or lowercase) to a level number value.
@ -302,7 +307,7 @@ function Logger(options, _childOptions, _childSimple) {
this.fields = {}; this.fields = {};
} }
if (!dtp) { if (!dtp && dtrace) {
dtp = dtrace.createDTraceProvider('bunyan'); dtp = dtrace.createDTraceProvider('bunyan');
for (var level in levelFromName) { for (var level in levelFromName) {
@ -768,7 +773,7 @@ function mkLogEmitter(minLevel) {
rec = mkRecord(msgArgs); rec = mkRecord(msgArgs);
str = this._emit(rec); str = this._emit(rec);
} }
probes[minLevel].fire(function () { probes && probes[minLevel].fire(function () {
return [ str || return [ str ||
(rec && log._emit(rec, true)) || (rec && log._emit(rec, true)) ||
log._emit(mkRecord(msgArgs), true) ]; log._emit(mkRecord(msgArgs), true) ];