forked from mapbox/mapbox-studio-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86e4d77
commit 4b334c1
Showing
2 changed files
with
44 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
var fs = require('fs'); | ||
var through = require('through'); | ||
var rotate = require('log-rotate'); | ||
|
||
// Set up app logging to a file with rotation. | ||
module.exports = function(filepath, maxsize, callback) { | ||
if (!filepath) return callback(); | ||
|
||
fs.stat(filepath, function(err, stat) { | ||
if (err && err.code !== 'ENOENT') return callback(err); | ||
if (stat && !stat.isFile()) return callback(new Error(filepath + ' is not a file')); | ||
if (!err && stat && stat.size > maxsize) { | ||
rotate(filepath, { compress: true }, function(err) { | ||
if (err) return callback(err); | ||
setup(0); | ||
}); | ||
} else { | ||
setup(stat && stat.size || 0); | ||
} | ||
}); | ||
|
||
function setup(offset) { | ||
var logstream = fs.createWriteStream(filepath, { | ||
flags: offset ? 'r+' : 'w', | ||
start: offset | ||
}); | ||
logstream.on('error', function (err) { | ||
console.warn('Problem writing to logfile at ' + filepath); | ||
return callback(); | ||
}) | ||
var pipeout = through(); | ||
var pipeerr = through(); | ||
pipeout.pipe(logstream); | ||
pipeout.pipe(process.stdout); | ||
pipeerr.pipe(logstream); | ||
pipeerr.pipe(process.stderr); | ||
process.__defineGetter__('stdout', function() { return pipeout; }); | ||
process.__defineGetter__('stderr', function() { return pipeerr; }); | ||
return callback(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters