2012-09-05 08:58:28 +08:00
|
|
|
var fs = require('fs'),
|
2013-04-11 19:45:16 +08:00
|
|
|
stream,
|
|
|
|
util = require('util'),
|
|
|
|
semver = require('semver');
|
|
|
|
|
|
|
|
if (semver.satisfies(process.version, '>=0.10.0')) {
|
|
|
|
stream = require('stream');
|
|
|
|
} else {
|
|
|
|
stream = require('readable-stream');
|
|
|
|
}
|
2012-09-05 08:58:28 +08:00
|
|
|
|
2013-03-24 09:50:13 +08:00
|
|
|
var debug;
|
|
|
|
if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) {
|
|
|
|
debug = function(message) { console.error('LOG4JS: (BaseRollingFileStream) %s', message); };
|
|
|
|
} else {
|
|
|
|
debug = function() { };
|
2012-09-05 08:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BaseRollingFileStream;
|
|
|
|
|
|
|
|
function BaseRollingFileStream(filename, options) {
|
2013-03-20 06:14:27 +08:00
|
|
|
debug("In BaseRollingFileStream");
|
|
|
|
this.filename = filename;
|
|
|
|
this.options = options || { encoding: 'utf8', mode: 0644, flags: 'a' };
|
|
|
|
this.currentSize = 0;
|
2012-09-18 06:46:39 +08:00
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
function currentFileSize(file) {
|
|
|
|
var fileSize = 0;
|
|
|
|
try {
|
|
|
|
fileSize = fs.statSync(file).size;
|
|
|
|
} catch (e) {
|
|
|
|
// file does not exist
|
2012-09-05 08:58:28 +08:00
|
|
|
}
|
2013-03-20 06:14:27 +08:00
|
|
|
return fileSize;
|
|
|
|
}
|
2012-09-05 08:58:28 +08:00
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
function throwErrorIfArgumentsAreNotValid() {
|
|
|
|
if (!filename) {
|
|
|
|
throw new Error("You must specify a filename");
|
2012-09-05 08:58:28 +08:00
|
|
|
}
|
2013-03-20 06:14:27 +08:00
|
|
|
}
|
2012-09-05 08:58:28 +08:00
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
throwErrorIfArgumentsAreNotValid();
|
|
|
|
debug("Calling BaseRollingFileStream.super");
|
|
|
|
BaseRollingFileStream.super_.call(this);
|
|
|
|
this.openTheStream();
|
|
|
|
this.currentSize = currentFileSize(this.filename);
|
2012-09-05 08:58:28 +08:00
|
|
|
}
|
2013-03-20 06:14:27 +08:00
|
|
|
util.inherits(BaseRollingFileStream, stream.Writable);
|
2012-09-05 08:58:28 +08:00
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
BaseRollingFileStream.prototype._write = function(chunk, encoding, callback) {
|
|
|
|
var that = this;
|
|
|
|
function writeTheChunk() {
|
|
|
|
debug("writing the chunk to the underlying stream");
|
|
|
|
that.currentSize += chunk.length;
|
|
|
|
that.theStream.write(chunk, encoding, callback);
|
|
|
|
}
|
2012-09-05 08:58:28 +08:00
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
debug("in _write");
|
2012-09-05 08:58:28 +08:00
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
if (this.shouldRoll()) {
|
|
|
|
this.currentSize = 0;
|
|
|
|
this.roll(this.filename, writeTheChunk);
|
|
|
|
} else {
|
|
|
|
writeTheChunk();
|
|
|
|
}
|
2012-09-05 08:58:28 +08:00
|
|
|
};
|
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
BaseRollingFileStream.prototype.openTheStream = function(cb) {
|
|
|
|
debug("opening the underlying stream");
|
|
|
|
this.theStream = fs.createWriteStream(this.filename, this.options);
|
|
|
|
if (cb) {
|
|
|
|
this.theStream.on("open", cb);
|
|
|
|
}
|
|
|
|
};
|
2012-09-18 06:46:39 +08:00
|
|
|
|
2013-03-20 06:14:27 +08:00
|
|
|
BaseRollingFileStream.prototype.closeTheStream = function(cb) {
|
|
|
|
debug("closing the underlying stream");
|
2013-04-11 19:45:16 +08:00
|
|
|
this.theStream.end(cb);
|
2012-09-05 08:58:28 +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
|
|
|
|
};
|
|
|
|
|