2012-02-22 05:53:28 +08:00
|
|
|
var util = require('util'),
|
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
events = require('events'),
|
2011-12-22 11:36:30 +08:00
|
|
|
async = require('async');
|
|
|
|
|
|
|
|
function debug(message) {
|
2012-02-22 05:53:28 +08:00
|
|
|
util.debug(message);
|
|
|
|
// console.log(message);
|
2011-12-22 11:36:30 +08:00
|
|
|
}
|
2011-12-19 13:58:21 +08:00
|
|
|
|
|
|
|
function BufferedWriteStream(stream) {
|
|
|
|
var that = this;
|
|
|
|
this.stream = stream;
|
|
|
|
this.buffer = [];
|
|
|
|
this.canWrite = false;
|
|
|
|
this.bytes = 0;
|
|
|
|
|
|
|
|
this.stream.on("open", function() {
|
|
|
|
that.canWrite = true;
|
|
|
|
that.flushBuffer();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.stream.on("error", function (err) {
|
|
|
|
that.emit("error", err);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.stream.on("drain", function() {
|
|
|
|
that.canWrite = true;
|
|
|
|
that.flushBuffer();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(BufferedWriteStream, events.EventEmitter);
|
|
|
|
|
|
|
|
Object.defineProperty(
|
|
|
|
BufferedWriteStream.prototype,
|
|
|
|
"fd",
|
|
|
|
{
|
|
|
|
get: function() { return this.stream.fd; },
|
|
|
|
set: function(newFd) {
|
|
|
|
this.stream.fd = newFd;
|
|
|
|
this.bytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Object.defineProperty(
|
|
|
|
BufferedWriteStream.prototype,
|
|
|
|
"bytesWritten",
|
|
|
|
{
|
|
|
|
get: function() { return this.bytes; }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
BufferedWriteStream.prototype.write = function(data, encoding) {
|
|
|
|
this.buffer.push({ data: data, encoding: encoding });
|
|
|
|
this.flushBuffer();
|
2011-12-22 11:36:30 +08:00
|
|
|
};
|
2011-12-19 13:58:21 +08:00
|
|
|
|
|
|
|
BufferedWriteStream.prototype.end = function(data, encoding) {
|
|
|
|
if (data) {
|
|
|
|
this.buffer.push({ data: data, encoding: encoding });
|
|
|
|
}
|
|
|
|
this.flushBufferEvenIfCannotWrite();
|
2011-12-22 11:36:30 +08:00
|
|
|
};
|
2011-12-19 13:58:21 +08:00
|
|
|
|
|
|
|
BufferedWriteStream.prototype.writeToStream = function(toWrite) {
|
|
|
|
this.bytes += toWrite.data.length;
|
|
|
|
this.canWrite = this.stream.write(toWrite.data, toWrite.encoding);
|
2011-12-22 11:36:30 +08:00
|
|
|
};
|
2011-12-19 13:58:21 +08:00
|
|
|
|
|
|
|
BufferedWriteStream.prototype.flushBufferEvenIfCannotWrite = function() {
|
|
|
|
while (this.buffer.length > 0) {
|
|
|
|
this.writeToStream(this.buffer.shift());
|
|
|
|
}
|
2011-12-22 11:36:30 +08:00
|
|
|
};
|
2011-12-19 13:58:21 +08:00
|
|
|
|
|
|
|
BufferedWriteStream.prototype.flushBuffer = function() {
|
|
|
|
while (this.buffer.length > 0 && this.canWrite) {
|
|
|
|
this.writeToStream(this.buffer.shift());
|
|
|
|
}
|
2011-12-22 11:36:30 +08:00
|
|
|
};
|
2011-12-19 13:58:21 +08:00
|
|
|
|
2011-12-22 11:36:30 +08:00
|
|
|
function BaseRollingFileStream(filename, options) {
|
2011-12-19 13:58:21 +08:00
|
|
|
this.filename = filename;
|
2011-12-22 11:36:30 +08:00
|
|
|
this.options = options || { encoding: 'utf8', mode: 0644, flags: 'a' };
|
2011-12-19 13:58:21 +08:00
|
|
|
this.rolling = false;
|
|
|
|
this.writesWhileRolling = [];
|
2011-12-22 11:36:30 +08:00
|
|
|
this.currentSize = 0;
|
2011-12-19 13:58:21 +08:00
|
|
|
|
|
|
|
function currentFileSize(file) {
|
|
|
|
var fileSize = 0;
|
|
|
|
try {
|
|
|
|
fileSize = fs.statSync(file).size;
|
|
|
|
} catch (e) {
|
2011-12-22 11:36:30 +08:00
|
|
|
// file does not exist
|
2011-12-19 13:58:21 +08:00
|
|
|
}
|
|
|
|
return fileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
function throwErrorIfArgumentsAreNotValid() {
|
2011-12-22 11:36:30 +08:00
|
|
|
if (!filename) {
|
|
|
|
throw new Error("You must specify a filename");
|
2011-12-19 13:58:21 +08:00
|
|
|
}
|
|
|
|
}
|
2011-12-22 11:36:30 +08:00
|
|
|
|
|
|
|
throwErrorIfArgumentsAreNotValid();
|
|
|
|
|
|
|
|
BaseRollingFileStream.super_.call(this, this.filename, this.options);
|
|
|
|
this.currentSize = currentFileSize(this.filename);
|
2011-12-19 13:58:21 +08:00
|
|
|
}
|
2011-12-22 11:36:30 +08:00
|
|
|
util.inherits(BaseRollingFileStream, fs.FileWriteStream);
|
|
|
|
|
|
|
|
BaseRollingFileStream.prototype.initRolling = function() {
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
function emptyRollingQueue() {
|
|
|
|
debug("emptying the rolling queue");
|
|
|
|
var toWrite;
|
|
|
|
while ((toWrite = that.writesWhileRolling.shift())) {
|
|
|
|
BaseRollingFileStream.super_.prototype.write.call(that, toWrite.data, toWrite.encoding);
|
|
|
|
that.currentSize += toWrite.data.length;
|
|
|
|
if (that.shouldRoll()) {
|
|
|
|
that.flush();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
that.flush();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rolling = true;
|
|
|
|
this.roll(this.filename, function() {
|
|
|
|
that.currentSize = 0;
|
|
|
|
that.rolling = emptyRollingQueue();
|
|
|
|
if (that.rolling) {
|
|
|
|
process.nextTick(function() { that.initRolling(); });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2011-12-19 13:58:21 +08:00
|
|
|
|
2011-12-22 11:36:30 +08:00
|
|
|
BaseRollingFileStream.prototype.write = function(data, encoding) {
|
2011-12-19 13:58:21 +08:00
|
|
|
if (this.rolling) {
|
|
|
|
this.writesWhileRolling.push({ data: data, encoding: encoding });
|
|
|
|
return false;
|
|
|
|
} else {
|
2011-12-22 11:36:30 +08:00
|
|
|
var canWrite = BaseRollingFileStream.super_.prototype.write.call(this, data, encoding);
|
|
|
|
this.currentSize += data.length;
|
|
|
|
debug('current size = ' + this.currentSize);
|
|
|
|
if (this.shouldRoll()) {
|
|
|
|
this.initRolling();
|
2011-12-19 13:58:21 +08:00
|
|
|
}
|
|
|
|
return canWrite;
|
|
|
|
}
|
2011-12-22 11:36:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
BaseRollingFileStream.prototype.shouldRoll = function() {
|
|
|
|
return false; // default behaviour is never to roll
|
|
|
|
};
|
|
|
|
|
|
|
|
BaseRollingFileStream.prototype.roll = function(filename, callback) {
|
|
|
|
callback(); // default behaviour is not to do anything
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function RollingFileStream (filename, size, backups, options) {
|
|
|
|
this.size = size;
|
|
|
|
this.backups = backups || 1;
|
|
|
|
|
|
|
|
function throwErrorIfArgumentsAreNotValid() {
|
|
|
|
if (!filename || !size || size <= 0) {
|
|
|
|
throw new Error("You must specify a filename and file size");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throwErrorIfArgumentsAreNotValid();
|
|
|
|
|
|
|
|
RollingFileStream.super_.call(this, filename, options);
|
2011-12-19 13:58:21 +08:00
|
|
|
}
|
2011-12-22 11:36:30 +08:00
|
|
|
util.inherits(RollingFileStream, BaseRollingFileStream);
|
2011-12-19 13:58:21 +08:00
|
|
|
|
2011-12-22 11:36:30 +08:00
|
|
|
RollingFileStream.prototype.shouldRoll = function() {
|
|
|
|
return this.currentSize >= this.size;
|
|
|
|
};
|
|
|
|
|
|
|
|
RollingFileStream.prototype.roll = function(filename, callback) {
|
2011-12-19 13:58:21 +08:00
|
|
|
var that = this,
|
2011-12-22 11:36:30 +08:00
|
|
|
nameMatcher = new RegExp('^' + path.basename(filename));
|
2011-12-19 13:58:21 +08:00
|
|
|
|
|
|
|
function justTheseFiles (item) {
|
|
|
|
return nameMatcher.test(item);
|
|
|
|
}
|
|
|
|
|
2011-12-22 11:36:30 +08:00
|
|
|
function index(filename_) {
|
|
|
|
return parseInt(filename_.substring((path.basename(filename) + '.').length), 10) || 0;
|
2011-12-19 13:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function byIndex(a, b) {
|
|
|
|
if (index(a) > index(b)) {
|
|
|
|
return 1;
|
|
|
|
} else if (index(a) < index(b) ) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function increaseFileIndex (fileToRename, cb) {
|
|
|
|
var idx = index(fileToRename);
|
2011-12-22 11:36:30 +08:00
|
|
|
debug('Index of ' + fileToRename + ' is ' + idx);
|
2011-12-19 13:58:21 +08:00
|
|
|
if (idx < that.backups) {
|
|
|
|
//on windows, you can get a EEXIST error if you rename a file to an existing file
|
|
|
|
//so, we'll try to delete the file we're renaming to first
|
2011-12-22 11:36:30 +08:00
|
|
|
fs.unlink(filename + '.' + (idx+1), function (err) {
|
2011-12-19 13:58:21 +08:00
|
|
|
//ignore err: if we could not delete, it's most likely that it doesn't exist
|
2011-12-22 11:36:30 +08:00
|
|
|
debug('Renaming ' + fileToRename + ' -> ' + filename + '.' + (idx+1));
|
|
|
|
fs.rename(path.join(path.dirname(filename), fileToRename), filename + '.' + (idx + 1), cb);
|
2011-12-19 13:58:21 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function renameTheFiles(cb) {
|
|
|
|
//roll the backups (rename file.n to file.n+1, where n <= numBackups)
|
2011-12-22 11:36:30 +08:00
|
|
|
debug("Renaming the old files");
|
|
|
|
fs.readdir(path.dirname(filename), function (err, files) {
|
2011-12-19 13:58:21 +08:00
|
|
|
async.forEachSeries(
|
|
|
|
files.filter(justTheseFiles).sort(byIndex).reverse(),
|
|
|
|
increaseFileIndex,
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function openANewFile(cb) {
|
2011-12-22 11:36:30 +08:00
|
|
|
debug("Opening a new file");
|
2011-12-19 13:58:21 +08:00
|
|
|
fs.open(
|
2011-12-22 11:36:30 +08:00
|
|
|
filename,
|
2011-12-19 13:58:21 +08:00
|
|
|
that.options.flags,
|
|
|
|
that.options.mode,
|
|
|
|
function (err, fd) {
|
2011-12-22 11:36:30 +08:00
|
|
|
debug("opened new file");
|
2011-12-19 13:58:21 +08:00
|
|
|
var oldLogFileFD = that.fd;
|
|
|
|
that.fd = fd;
|
|
|
|
that.writable = true;
|
2011-12-22 11:36:30 +08:00
|
|
|
fs.close(oldLogFileFD, cb);
|
2011-12-19 13:58:21 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-12-22 11:36:30 +08:00
|
|
|
debug("Starting roll");
|
|
|
|
debug("Queueing up data until we've finished rolling");
|
|
|
|
debug("Flushing underlying stream");
|
2011-12-19 13:58:21 +08:00
|
|
|
this.flush();
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
renameTheFiles,
|
2011-12-22 11:36:30 +08:00
|
|
|
openANewFile
|
|
|
|
], callback);
|
|
|
|
|
|
|
|
};
|
2011-12-19 13:58:21 +08:00
|
|
|
|
|
|
|
|
2011-12-22 11:36:30 +08:00
|
|
|
exports.BaseRollingFileStream = BaseRollingFileStream;
|
2011-12-19 13:58:21 +08:00
|
|
|
exports.RollingFileStream = RollingFileStream;
|
2012-02-22 05:53:28 +08:00
|
|
|
exports.BufferedWriteStream = BufferedWriteStream;
|