<commit_before>var util = require('util');
var stream = require('stream');
var fs = require('fs');
var spawn = require('child_process').spawn;

var generateLogName = require('./logname').generate;

module.exports = LogWriter;

function LogWriter(worker, options) {
  if (!(this instanceof LogWriter)) return new LogWriter(worker, options);

  stream.PassThrough.call(this);

  this.template = options.log;
  this.worker = {
    id: worker.id || 'supervisor',
    pid: worker.pid || worker.process.pid
  }
  this.name = generateLogName(this.template, this.worker);
  if (/^\|[^\|]/.test(this.name)) {
    this.cmd = this.name
                .slice(1) // strip leading '|'
                .trim()
                .split(/\s+/);
    this.proc = spawn(this.cmd[0], this.cmd.slice(1),
                      { stdio: ['pipe', process.stdout, process.stderr] });
    this.sink = this.proc.stdin;
  } else {
    this.sink = fs.createWriteStream(this.name, { flags: 'a'});
  }
  this.pipe(this.sink);
}

util.inherits(LogWriter, stream.PassThrough);

LogWriter.prototype.reOpen = function LogWriterReOpen() {
  if (this.sink instanceof fs.WriteStream) {
    this.unpipe(this.sink);
    this.sink.end();
    // lose our reference to previous stream, but it should clean itself up
    this.sink = fs.createWriteStream(this.name, { flags: 'a'});
    this.pipe(sink);
  }
}<commit_msg>Fix log rotation<commit_after>var util = require('util');
var stream = require('stream');
var fs = require('fs');
var spawn = require('child_process').spawn;

var generateLogName = require('./logname').generate;

module.exports = LogWriter;

function LogWriter(worker, options) {
  if (!(this instanceof LogWriter)) return new LogWriter(worker, options);

  stream.PassThrough.call(this);

  this.template = options.log;
  this.worker = {
    id: worker.id || 'supervisor',
    pid: worker.pid || worker.process.pid
  }
  this.name = generateLogName(this.template, this.worker);
  if (/^\|[^\|]/.test(this.name)) {
    this.cmd = this.name
                .slice(1) // strip leading '|'
                .trim()
                .split(/\s+/);
    this.proc = spawn(this.cmd[0], this.cmd.slice(1),
                      { stdio: ['pipe', process.stdout, process.stderr] });
    this.sink = this.proc.stdin;
  } else {
    this.sink = fs.createWriteStream(this.name, { flags: 'a'});
  }
  this.pipe(this.sink);
}

util.inherits(LogWriter, stream.PassThrough);

LogWriter.prototype.reOpen = function LogWriterReOpen() {
  if (this.sink instanceof fs.WriteStream) {
    this.unpipe(this.sink);
    this.sink.end();
    // lose our reference to previous stream, but it should clean itself up
    this.sink = fs.createWriteStream(this.name, { flags: 'a'});
    this.pipe(this.sink);
  }
}
{'fraction_non_alphanumeric': 0.16521423384168482, 'fraction_numerical': 0.002178649237472767, 'mean_word_length': 3.0754437869822486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3577252', 'n_tokens_mistral': 915, 'n_tokens_neox': 893, 'n_words': 188}