2 var BaseRollingFileStream = require('./BaseRollingFileStream')
3 , debug = require('../debug')('RollingFileStream')
4 , util = require('util')
5 , path = require('path')
6 , child_process = require('child_process')
7 , zlib = require("zlib")
10 module.exports = RollingFileStream;
12 function RollingFileStream (filename, size, backups, options) {
14 this.backups = backups || 1;
16 function throwErrorIfArgumentsAreNotValid() {
17 if (!filename || !size || size <= 0) {
18 throw new Error("You must specify a filename and file size");
22 throwErrorIfArgumentsAreNotValid();
24 RollingFileStream.super_.call(this, filename, options);
26 util.inherits(RollingFileStream, BaseRollingFileStream);
28 RollingFileStream.prototype.shouldRoll = function() {
29 debug("should roll with current size " + this.currentSize + " and max size " + this.size);
30 return this.currentSize >= this.size;
33 RollingFileStream.prototype.roll = function(filename, callback) {
35 nameMatcher = new RegExp('^' + path.basename(filename));
37 function justTheseFiles (item) {
38 return nameMatcher.test(item);
41 function index(filename_) {
42 debug('Calculating index of '+filename_);
43 return parseInt(filename_.substring((path.basename(filename) + '.').length), 10) || 0;
46 function byIndex(a, b) {
47 if (index(a) > index(b)) {
49 } else if (index(a) < index(b) ) {
56 function compress (filename, cb) {
58 var gzip = zlib.createGzip();
59 var inp = fs.createReadStream(filename);
60 var out = fs.createWriteStream(filename+".gz");
61 inp.pipe(gzip).pipe(out);
62 fs.unlink(filename, cb);
66 function increaseFileIndex (fileToRename, cb) {
67 var idx = index(fileToRename);
68 debug('Index of ' + fileToRename + ' is ' + idx);
69 if (idx < that.backups) {
71 var ext = path.extname(fileToRename);
72 var destination = filename + '.' + (idx+1);
73 if (that.options.compress && /^gz$/.test(ext.substring(1))) {
76 //on windows, you can get a EEXIST error if you rename a file to an existing file
77 //so, we'll try to delete the file we're renaming to first
78 fs.unlink(destination, function (err) {
79 //ignore err: if we could not delete, it's most likely that it doesn't exist
80 debug('Renaming ' + fileToRename + ' -> ' + destination);
81 fs.rename(path.join(path.dirname(filename), fileToRename), destination, function(err) {
85 if (that.options.compress && ext!=".gz") {
86 compress(destination, cb);
98 function renameTheFiles(cb) {
99 //roll the backups (rename file.n to file.n+1, where n <= numBackups)
100 debug("Renaming the old files");
101 fs.readdir(path.dirname(filename), function (err, files) {
102 var filesToProcess = files.filter(justTheseFiles).sort(byIndex);
103 (function processOne(err) {
104 var file = filesToProcess.pop();
105 if (!file || err) { return cb(err); }
106 increaseFileIndex(file, processOne);
111 debug("Rolling, rolling, rolling");
113 renameTheFiles.bind(null,
114 this.openTheStream.bind(this,