more linting
This commit is contained in:
parent
87dc7cf5aa
commit
2de838bc76
@ -1,88 +1,89 @@
|
|||||||
var BaseRollingFileStream = require('./BaseRollingFileStream'),
|
"use strict";
|
||||||
util = require('util'),
|
var BaseRollingFileStream = require('./BaseRollingFileStream')
|
||||||
path = require('path'),
|
, util = require('util')
|
||||||
fs = require('fs'),
|
, path = require('path')
|
||||||
async = require('async');
|
, fs = require('fs')
|
||||||
|
, async = require('async');
|
||||||
|
|
||||||
var debug;
|
var debug;
|
||||||
if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) {
|
if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) {
|
||||||
debug = function(message) { console.error('LOG4JS: (RollingFileStream) %s', message); };
|
debug = function(message) { console.error('LOG4JS: (RollingFileStream) %s', message); };
|
||||||
} else {
|
} else {
|
||||||
debug = function() { };
|
debug = function() { };
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = RollingFileStream;
|
module.exports = RollingFileStream;
|
||||||
|
|
||||||
function RollingFileStream (filename, size, backups, options) {
|
function RollingFileStream (filename, size, backups, options) {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
this.backups = backups || 1;
|
this.backups = backups || 1;
|
||||||
|
|
||||||
function throwErrorIfArgumentsAreNotValid() {
|
function throwErrorIfArgumentsAreNotValid() {
|
||||||
if (!filename || !size || size <= 0) {
|
if (!filename || !size || size <= 0) {
|
||||||
throw new Error("You must specify a filename and file size");
|
throw new Error("You must specify a filename and file size");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
throwErrorIfArgumentsAreNotValid();
|
throwErrorIfArgumentsAreNotValid();
|
||||||
|
|
||||||
RollingFileStream.super_.call(this, filename, options);
|
RollingFileStream.super_.call(this, filename, options);
|
||||||
}
|
}
|
||||||
util.inherits(RollingFileStream, BaseRollingFileStream);
|
util.inherits(RollingFileStream, BaseRollingFileStream);
|
||||||
|
|
||||||
RollingFileStream.prototype.shouldRoll = function() {
|
RollingFileStream.prototype.shouldRoll = function() {
|
||||||
debug("should roll with current size %d, and max size %d", this.currentSize, this.size);
|
debug("should roll with current size %d, and max size %d", this.currentSize, this.size);
|
||||||
return this.currentSize >= this.size;
|
return this.currentSize >= this.size;
|
||||||
};
|
};
|
||||||
|
|
||||||
RollingFileStream.prototype.roll = function(filename, callback) {
|
RollingFileStream.prototype.roll = function(filename, callback) {
|
||||||
var that = this,
|
var that = this,
|
||||||
nameMatcher = new RegExp('^' + path.basename(filename));
|
nameMatcher = new RegExp('^' + path.basename(filename));
|
||||||
|
|
||||||
function justTheseFiles (item) {
|
function justTheseFiles (item) {
|
||||||
return nameMatcher.test(item);
|
return nameMatcher.test(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
function index(filename_) {
|
function index(filename_) {
|
||||||
return parseInt(filename_.substring((path.basename(filename) + '.').length), 10) || 0;
|
return parseInt(filename_.substring((path.basename(filename) + '.').length), 10) || 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function byIndex(a, b) {
|
function byIndex(a, b) {
|
||||||
if (index(a) > index(b)) {
|
if (index(a) > index(b)) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if (index(a) < index(b) ) {
|
} else if (index(a) < index(b) ) {
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function increaseFileIndex (fileToRename, cb) {
|
function increaseFileIndex (fileToRename, cb) {
|
||||||
var idx = index(fileToRename);
|
var idx = index(fileToRename);
|
||||||
debug('Index of ' + fileToRename + ' is ' + idx);
|
debug('Index of ' + fileToRename + ' is ' + idx);
|
||||||
if (idx < that.backups) {
|
if (idx < that.backups) {
|
||||||
//on windows, you can get a EEXIST error if you rename a file to an existing file
|
//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
|
//so, we'll try to delete the file we're renaming to first
|
||||||
fs.unlink(filename + '.' + (idx+1), function (err) {
|
fs.unlink(filename + '.' + (idx+1), function (err) {
|
||||||
//ignore err: if we could not delete, it's most likely that it doesn't exist
|
//ignore err: if we could not delete, it's most likely that it doesn't exist
|
||||||
debug('Renaming ' + fileToRename + ' -> ' + filename + '.' + (idx+1));
|
debug('Renaming ' + fileToRename + ' -> ' + filename + '.' + (idx+1));
|
||||||
fs.rename(path.join(path.dirname(filename), fileToRename), filename + '.' + (idx + 1), cb);
|
fs.rename(path.join(path.dirname(filename), fileToRename), filename + '.' + (idx + 1), cb);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
cb();
|
cb();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function renameTheFiles(cb) {
|
function renameTheFiles(cb) {
|
||||||
//roll the backups (rename file.n to file.n+1, where n <= numBackups)
|
//roll the backups (rename file.n to file.n+1, where n <= numBackups)
|
||||||
debug("Renaming the old files");
|
debug("Renaming the old files");
|
||||||
fs.readdir(path.dirname(filename), function (err, files) {
|
fs.readdir(path.dirname(filename), function (err, files) {
|
||||||
async.forEachSeries(
|
async.forEachSeries(
|
||||||
files.filter(justTheseFiles).sort(byIndex).reverse(),
|
files.filter(justTheseFiles).sort(byIndex).reverse(),
|
||||||
increaseFileIndex,
|
increaseFileIndex,
|
||||||
cb
|
cb
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
debug("Rolling, rolling, rolling");
|
debug("Rolling, rolling, rolling");
|
||||||
async.series([
|
async.series([
|
||||||
|
Loading…
Reference in New Issue
Block a user