<commit_before>
//
// Satan is splitted in two parts.
// - The first one is a daemon wrapper around God.js
//   which acts as an RPC server
//      => Satan.remoteWrapper
//
// - The other part is an RPC client which is mainly
//   used by the CLI
//
// by <NAME>

var rpc    = require("axon-rpc");
var axon   = require("axon");
var rep    = axon.socket("rep");
var req    = axon.socket("req");
var debug  = require("debug")("god:satan");
var events = require("events");
var util   = require("util");
var fs     = require("fs");
var p      = require("path");
var cst    = require('../constants.js');

//
// Get host:port we should bind to
//
var bind = (function(addr) {
  var hostport = String(addr).split(':');
  if (hostport.length < 2) {
    hostport = [undefined, hostport[0]];
  }
  if (hostport[0] == null) {
    hostport[0] = 'localhost';
  }

  return {
    HOST: hostport[0],
    PORT: Number(hostport[1])
  };
})(cst.DAEMON_BIND_ADDR);

var Satan = module.exports = {};

//
// Code switcher
//
Satan.onReady = function() {
  (function init() {
    if (process.env.DAEMON) {
      // ! This env variable is used only for the transitional state of this class
      delete process.env.DAEMON;
      process.title = "pm2: Satan Daemonizer";
      Satan.remoteWrapper();
    }
    else {
      Satan.pingDaemon(function(ab) {
        if (ab == false)
          return Satan.launchDaemon(function(err, child) {
            if (err) {
              console.error(err);
              process.exit(1);
            }
            Satan.launchRPC();
          });
        return Satan.launchRPC();
      });
    }
  })();
};

//
// The code that will be executed on the next process
// Here it exposes God methods
//
Satan.remoteWrapper = function() {

  if (process.env.SILENT == "true") {
    //
    // Redirect output to files
    //
    var stdout = fs.createWriteStream(cst.PM2_LOG_FILE_PATH, { flags : 'a' });

    process.stderr.write = (function(write) {
                              return function(string, encoding, fd) {
                                stdout.write(JSON.stringify([(new Date()).toISOString(), string]));
                              };
                            }
                           )(process.stderr.write);

    process.stdout.write = (function(write) {
                              return function(string, encoding, fd) {
                                stdout.write(JSON.stringify([(new Date()).toISOString(), string]));
                              };
                            })(process.stdout.write);
  }

  // Only require here because God init himself
  var God = require("./God");

  // Send ready message to Satan Client
  process.send({
    online : true, success : true, pid : process.pid
  });
<commit_msg>make logs line break properly<commit_after>
//
// Satan is splitted in two parts.
// - The first one is a daemon wrapper around God.js
//   which acts as an RPC server
//      => Satan.remoteWrapper
//
// - The other part is an RPC client which is mainly
//   used by the CLI
//
// by <NAME>

var rpc    = require("axon-rpc");
var axon   = require("axon");
var rep    = axon.socket("rep");
var req    = axon.socket("req");
var debug  = require("debug")("god:satan");
var events = require("events");
var util   = require("util");
var fs     = require("fs");
var p      = require("path");
var cst    = require('../constants.js');

//
// Get host:port we should bind to
//
var bind = (function(addr) {
  var hostport = String(addr).split(':');
  if (hostport.length < 2) {
    hostport = [undefined, hostport[0]];
  }
  if (hostport[0] == null) {
    hostport[0] = 'localhost';
  }

  return {
    HOST: hostport[0],
    PORT: Number(hostport[1])
  };
})(cst.DAEMON_BIND_ADDR);

var Satan = module.exports = {};

//
// Code switcher
//
Satan.onReady = function() {
  (function init() {
    if (process.env.DAEMON) {
      // ! This env variable is used only for the transitional state of this class
      delete process.env.DAEMON;
      process.title = "pm2: Satan Daemonizer";
      Satan.remoteWrapper();
    }
    else {
      Satan.pingDaemon(function(ab) {
        if (ab == false)
          return Satan.launchDaemon(function(err, child) {
            if (err) {
              console.error(err);
              process.exit(1);
            }
            Satan.launchRPC();
          });
        return Satan.launchRPC();
      });
    }
  })();
};

//
// The code that will be executed on the next process
// Here it exposes God methods
//
Satan.remoteWrapper = function() {

  if (process.env.SILENT == "true") {
    //
    // Redirect output to files
    //
    var stdout = fs.createWriteStream(cst.PM2_LOG_FILE_PATH, { flags : 'a' });

    process.stderr.write = function(string) {
      stdout.write(new Date().toISOString() + ' : ' + string);
    }

    process.stdout.write = function(string) {
      stdout.write(new Date().toISOString() + ' : ' + string);
    }
  }

  // Only require here because God init himself
  var God = require("./God");

  // Send ready message to Satan Client
  process.send({
    online : true, success : true, pid : process.pid
  });

{'fraction_non_alphanumeric': 0.15103152977812379, 'fraction_numerical': 0.0035033086804203972, 'mean_word_length': 2.090198436560433, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '705927', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1568, 'n_words': 426}