DateRollingFileStream.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. var BaseRollingFileStream = require('./BaseRollingFileStream')
  3. , debug = require('../debug')('DateRollingFileStream')
  4. , format = require('../date_format')
  5. , async = require('async')
  6. , fs = require('fs')
  7. , util = require('util');
  8. module.exports = DateRollingFileStream;
  9. function DateRollingFileStream(filename, pattern, options, now) {
  10. debug("Now is " + now);
  11. if (pattern && typeof(pattern) === 'object') {
  12. now = options;
  13. options = pattern;
  14. pattern = null;
  15. }
  16. this.pattern = pattern || '.yyyy-MM-dd';
  17. this.now = now || Date.now;
  18. this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now()));
  19. this.baseFilename = filename;
  20. this.alwaysIncludePattern = false;
  21. if (options) {
  22. if (options.alwaysIncludePattern) {
  23. this.alwaysIncludePattern = true;
  24. filename = this.baseFilename + this.lastTimeWeWroteSomething;
  25. }
  26. delete options.alwaysIncludePattern;
  27. if (Object.keys(options).length === 0) {
  28. options = null;
  29. }
  30. }
  31. debug("this.now is " + this.now + ", now is " + now);
  32. DateRollingFileStream.super_.call(this, filename, options);
  33. }
  34. util.inherits(DateRollingFileStream, BaseRollingFileStream);
  35. DateRollingFileStream.prototype.shouldRoll = function() {
  36. var lastTime = this.lastTimeWeWroteSomething,
  37. thisTime = format.asString(this.pattern, new Date(this.now()));
  38. debug("DateRollingFileStream.shouldRoll with now = " +
  39. this.now() + ", thisTime = " + thisTime + ", lastTime = " + lastTime);
  40. this.lastTimeWeWroteSomething = thisTime;
  41. this.previousTime = lastTime;
  42. return thisTime !== lastTime;
  43. };
  44. DateRollingFileStream.prototype.roll = function(filename, callback) {
  45. var that = this;
  46. debug("Starting roll");
  47. if (this.alwaysIncludePattern) {
  48. this.filename = this.baseFilename + this.lastTimeWeWroteSomething;
  49. async.series([
  50. this.closeTheStream.bind(this),
  51. this.openTheStream.bind(this)
  52. ], callback);
  53. } else {
  54. var newFilename = this.baseFilename + this.previousTime;
  55. async.series([
  56. this.closeTheStream.bind(this),
  57. deleteAnyExistingFile,
  58. renameTheCurrentFile,
  59. this.openTheStream.bind(this)
  60. ], callback);
  61. }
  62. function deleteAnyExistingFile(cb) {
  63. //on windows, you can get a EEXIST error if you rename a file to an existing file
  64. //so, we'll try to delete the file we're renaming to first
  65. fs.unlink(newFilename, function (err) {
  66. //ignore err: if we could not delete, it's most likely that it doesn't exist
  67. cb();
  68. });
  69. }
  70. function renameTheCurrentFile(cb) {
  71. debug("Renaming the " + filename + " -> " + newFilename);
  72. fs.rename(filename, newFilename, cb);
  73. }
  74. };