Skip to content

Commit

Permalink
Refactoring and adding in support for fast json serialisation without…
Browse files Browse the repository at this point in the history
… cycle checking
  • Loading branch information
Jim Tupper committed Feb 26, 2016
1 parent 53de21f commit 55fdc48
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
82 changes: 49 additions & 33 deletions lib/rotatingfilestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,31 @@ function RotatingFileStream(options) {
var stream = null;
var streambytesWritten = 0;

// Copied from bunyan source
function safeCycles() {
var seen = [];
return function (key, val) {
if (!val || typeof (val) !== 'object') {
return val;
}
if (seen.indexOf(val) !== -1) {
return '[Circular]';
}
seen.push(val);
return val;
};
}

function nullJsonify(textlog) {
return textlog;
}

function fastJsonify(rawlog) {
return JSON.stringify(rawlog, bunyan.safeCycles()) + '\n';
return JSON.stringify(rawlog, safeCycles()) + '\n';
}

function fastUnsafeJsonify(rawlog) {
return JSON.stringify(rawlog) + '\n';
}

function orderedJsonify(rawlog) {
Expand All @@ -52,7 +71,7 @@ function RotatingFileStream(options) {
log[k] = rawlog[k];
}

return JSON.stringify(log, bunyan.safeCycles()) + '\n';
return JSON.stringify(log, safeCycles()) + '\n';
}

function chooseJsonify(log) {
Expand All @@ -64,6 +83,8 @@ function RotatingFileStream(options) {
jsonify = nullJsonify;
} else if (options.fieldOrder) {
jsonify = orderedJsonify;
} else if (options.noCyclesCheck) {
jsonify = fastUnsafeJsonify;
} else {
jsonify = fastJsonify;
}
Expand All @@ -73,44 +94,35 @@ function RotatingFileStream(options) {

var jsonify = chooseJsonify;

var forceWrite = false;
function writer(logs, callback) {
var written = -1; // the index of the last successful write
for (var i = 0; stream && i < logs.length; i += 1) {
var str = jsonify(logs[i]);

var writeBuffer = new Buffer(str, 'utf8');

streambytesWritten += writeBuffer.byteLength;

if (forceWrite) {
// This log entry gets written regardless - don't emit
forceWrite = false;
} else {
base.emit('data', { bytesWritten: streambytesWritten });
}

if (!stream) {
// This log entry caused a rotation
// We can't write it, force it to be written
// Next time round
forceWrite = true;
break;
}
base.emit('data', { bytesWritten: streambytesWritten });

try {
stream.write(writeBuffer, function (err) {
if (err) {
if (stream) {
try {
stream.write(writeBuffer, function (err) {
if (err) {
base.emit('error', err);
}
});
} catch (err) {
base.emit('error', err);
}
});
} catch (err) {
base.emit('error', err);

written = i;
}
}

// If we didn't get all the way through the array, unshift the remaining
// records back onto our queue in reverse order
for (var rollback = logs.length -1; rollback >= i; rollback -= 1) {
for (var rollback = logs.length -1; rollback > written; rollback -= 1) {
writeQueue.unshift(logs[rollback]);
}

Expand Down Expand Up @@ -155,23 +167,27 @@ function RotatingFileStream(options) {
});
}

var rotating = false;
function rotate() {
if (rotating) {
return;
} else {
rotating = true;
}
function rotateActual() {

base.once('data', function () {
rotateFunction = rotateActual;
});

rotateFunction = function () {};

rotator.rotate(function (err, newstream, filePath) {
if (err) {
base.emit('error', err);
}

rotating = false;
});
}

var rotateFunction = rotateActual;

function rotate() {
rotateFunction();
}

function write(s, callback) {
writeQueue.push(s, callback);
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"logging",
"log4j",
"json",
"bunyan",
"rotating",
"rotate"
],
Expand Down

0 comments on commit 55fdc48

Please sign in to comment.