var util = require('util');
var stream = require('stream');
var fs = require('fs');

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);
  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);
  }
}

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);
  }
}

Add support for '| cmd' as log name for piping
Add support for '| cmd' as log name for piping

Create a child process as described by the log parameter following the
leading '|' and write all log events to that process's stdin stream.

The command name supports %p and %w replacement for per-worker logging.

If per-worker substitutions are made, each worker gets its own logger
process. If the command does not use per-worker substitutions, all
workers and the supervisor share the same logger process.
