<commit_before>'use strict';

var canLog = require('./can-log');
var fs = require('fs');
var getConfig = require('./get-config');
var resolve = require('path').resolve;
var tmpdir = require('os').tmpdir();
var stackTrace = require('stack-trace');
var util = require('util');
var noop = function(){};
var space = '                                                           ';
var instanceCache = {};

module.exports = function(filename) {
  if (!instanceCache[filename]) {
    instanceCache[filename] = createTmpDebug(filename);
  }

  return instanceCache[filename];
};

function createTmpDebug(filename) {
  if (typeof filename !== 'string') {
    throw new Error(filename + ' must be a string at ' +
        stackTrace.parse(new Error())[1].getFileName());
  }

  var config = getConfig();

  var logFile = config.enabled ? fs.createWriteStream(resolve(tmpdir, filename),
      {flags: 'a+', encoding: 'utf8'}) : null;

  var log = config.enabled ? function log(msg) {
    var stack = stackTrace.get();

    if (!canLog(config, stack)) {
      return;
    }

    logFile.write('\n' + msg + '\n', 'utf8', noop);
  } : noop;

  var logStackTrace = config.enabled ? function logStackTrace(prefix) {
    var stack = stackTrace.get();

    if (!canLog(config, stack)) {
      return;
    }

    if (prefix === undefined) {
      prefix = '';
    } else {
      prefix += ' -->\n';
    }

    logFile.write('\n' + prefix + stack.slice(1).map(function(stack) {
            return '  ' + formatLineInfo(stack.getFileName(), stack.getLineNumber(),
                stack.getColumnNumber());
          })
          .join('\n') + '\n', 'utf8', noop);
  } : noop;

  var logArgs = config.enabled ? function logArgs(args) {
    var stack = stackTrace.get();

    if (!canLog(config, stack)) {
      return;
    }

    var callerStack = stack[1];
    var file = callerStack.getFileName();
    var fnName = '' + (callerStack.getFunctionName() || 'anonymous');
    var lineNumber = callerStack.getLineNumber();
    var columnNumber = callerStack.getColumnNumber();
    var fn = callerStack.getFunction() || noop;

    args = [].slice.call(args || []).map(stringifyArg);

    var params = fn.toString()
        .replace(/function[^(]*\(\s*([^)]*)\s*\)[\s\S]+/m, '$1')
        .split(/,\s*/);

    logFile.write('\n' + fnName + '(' + args.join(',\n' + space.slice(-(fnName.length + 1))) +
        ')\n  at ' + formatLineInfo(file, lineNumber, columnNumber) + '\n', 'utf8', noop);
  } : noop;

  function stringifyArg(arg) {
    switch (typeof arg) {
      case 'string':
        return arg;
      case 'function':
        return arg.toString().replace(/(function[^)]+\))[\s\S]+/m, '$1');
      case 'undefined':
        return '' + arg;<commit_msg>Fixing lint issues.<commit_after>'use strict';

var canLog = require('./can-log');
var fs = require('fs');
var getConfig = require('./get-config');
var resolve = require('path').resolve;
var tmpdir = require('os').tmpdir();
var stackTrace = require('stack-trace');
var util = require('util');
var noop = function(){};
var space = '                                                           ';
var instanceCache = {};

module.exports = function(filename) {
  if (!instanceCache[filename]) {
    instanceCache[filename] = createTmpDebug(filename);
  }

  return instanceCache[filename];
};

function createTmpDebug(filename) {
  if (typeof filename !== 'string') {
    throw new Error(filename + ' must be a string at ' +
        stackTrace.parse(new Error())[1].getFileName());
  }

  var config = getConfig();

  var logFile = config.enabled ? fs.createWriteStream(resolve(tmpdir, filename),
      {flags: 'a+', encoding: 'utf8'}) : null;

  var log = config.enabled ? function log(msg) {
    var stack = stackTrace.get();

    if (!canLog(config, stack)) {
      return;
    }

    logFile.write('\n' + msg + '\n', 'utf8', noop);
  } : noop;

  var logStackTrace = config.enabled ? function logStackTrace(prefix) {
    var stack = stackTrace.get();

    if (!canLog(config, stack)) {
      return;
    }

    if (prefix === undefined) {
      prefix = '';
    } else {
      prefix += ' -->\n';
    }

    logFile.write('\n' + prefix + stack.slice(1).map(function(stack) {
            return '  ' + formatLineInfo(stack.getFileName(), stack.getLineNumber(),
                stack.getColumnNumber());
          })
          .join('\n') + '\n', 'utf8', noop);
  } : noop;

  var logArgs = config.enabled ? function logArgs(args) {
    var stack = stackTrace.get();

    if (!canLog(config, stack)) {
      return;
    }

    var callerStack = stack[1];
    var file = callerStack.getFileName();
    var fnName = '' + (callerStack.getFunctionName() || 'anonymous');
    var lineNumber = callerStack.getLineNumber();
    var columnNumber = callerStack.getColumnNumber();

    args = [].slice.call(args || []).map(stringifyArg);

    logFile.write('\n' + fnName + '(' + args.join(',\n' + space.slice(-(fnName.length + 1))) +
        ')\n  at ' + formatLineInfo(file, lineNumber, columnNumber) + '\n', 'utf8', noop);
  } : noop;

  function stringifyArg(arg) {
    switch (typeof arg) {
      case 'string':
        return arg;
      case 'function':
        return arg.toString().replace(/(function[^)]+\))[\s\S]+/m, '$1');
      case 'undefined':
        return '' + arg;
{'fraction_non_alphanumeric': 0.18022268352519344, 'fraction_numerical': 0.0035855821853179844, 'mean_word_length': 2.7402964008468595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2683409', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1627, 'n_words': 369}