4 , debug = require('../debug')('BaseRollingFileStream')
5 , util = require('util')
6 , semver = require('semver');
8 if (semver.satisfies(process.version, '>=0.10.0')) {
9 stream = require('stream');
11 stream = require('readable-stream');
14 module.exports = BaseRollingFileStream;
16 function BaseRollingFileStream(filename, options) {
17 debug("In BaseRollingFileStream");
18 this.filename = filename;
19 this.options = options || {};
20 this.options.encoding = this.options.encoding || 'utf8';
21 this.options.mode = this.options.mode || parseInt('0644', 8);
22 this.options.flags = this.options.flags || 'a';
26 function currentFileSize(file) {
29 fileSize = fs.statSync(file).size;
31 // file does not exist
36 function throwErrorIfArgumentsAreNotValid() {
38 throw new Error("You must specify a filename");
42 throwErrorIfArgumentsAreNotValid();
43 debug("Calling BaseRollingFileStream.super");
44 BaseRollingFileStream.super_.call(this);
46 this.currentSize = currentFileSize(this.filename);
48 util.inherits(BaseRollingFileStream, stream.Writable);
50 BaseRollingFileStream.prototype._write = function(chunk, encoding, callback) {
52 function writeTheChunk() {
53 debug("writing the chunk to the underlying stream");
54 that.currentSize += chunk.length;
56 that.theStream.write(chunk, encoding, callback);
66 if (this.shouldRoll()) {
68 this.roll(this.filename, writeTheChunk);
74 BaseRollingFileStream.prototype.openTheStream = function(cb) {
75 debug("opening the underlying stream");
76 this.theStream = fs.createWriteStream(this.filename, this.options);
78 this.theStream.on("open", cb);
82 BaseRollingFileStream.prototype.closeTheStream = function(cb) {
83 debug("closing the underlying stream");
84 this.theStream.end(cb);
87 BaseRollingFileStream.prototype.shouldRoll = function() {
88 return false; // default behaviour is never to roll
91 BaseRollingFileStream.prototype.roll = function(filename, callback) {
92 callback(); // default behaviour is not to do anything