<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const util = require('util');

const stacktrace = require('stack-trace');
const Symbol = require('symbol');

const LEVELS = require('./levels');
const printf = require('./utils/printf');

const UNCAUGHT_SYMBOL = Symbol();
const LOG_VERSION = 1; // increment when log format changes
const HOSTNAME = require('os').hostname();
const MESSAGE_CACHE = '__message:' + Math.random();

// stack formatter helper
function stack(e) {
  return {
    toString: function() {
      // first line is err.message, which we already show, so start from
      // second line
      return e.stack.substr(e.stack.indexOf('\n'));
    },
    toJSON: function() {
      return stacktrace.parse(e);
    }
  };
}

function Trace(fn) {
  Error.captureStackTrace(this, fn);
}

Trace.prototype.toJSON = function() {
  return '[object Trace]';
};

function Record(name, level, args) {
  this.name = name;
  this.level = level;
  this.levelname = LEVELS.getLevelName(level);
  this.args = args;
  this.pid = process.pid;
  this.host = HOSTNAME;
  this.v = LOG_VERSION;
  this.timestamp = Date.now();

  var i = args.length;
  var trace;
  var isErr = false;
  while (i--) {
    var a = args[i];
    if (a && ((isErr = util.isError(a)) || a instanceof Trace)) {
      trace = a;
      break;
    }
  }
  this.exception = isErr;
  this.uncaughtException = isErr ? trace[UNCAUGHT_SYMBOL] : undefined;
  this.stack = trace ? stack(trace) : undefined;

}

Record.prototype.name = undefined;
Record.prototype.level = undefined;
Record.prototype.levelname = undefined;
Record.prototype.args = undefined;
Record.prototype.pid = undefined;
Record.prototype.host = undefined;
Record.prototype.v = undefined;
Record.prototype.timestamp = undefined;
Record.prototype.exception = undefined;
Record.prototype.uncaughtException = undefined;
Record.prototype.stack = undefined;
Record.prototype[MESSAGE_CACHE] = undefined;

Object.defineProperty(Record.prototype, 'message', {
  enumerable: true,
  get: function() {
    if (!this[MESSAGE_CACHE]) {
      var args = this.args;
      var message = args[0];
      var isString = typeof message === 'string';
      if (!isString || args.length > 1) {
        if (!isString) {
          args = new Array(this.args.length + 1);
          args[0] = '%?';
          var i = args.length - 1;
          while (i--) {
            args[i + 1] = this.args[i];
          }
        }
        message = printf.apply(null, args);
      }
      this[MESSAGE_CACHE] = message;
    }
    return this[MESSAGE_CACHE];
  }
});

Record.prototype.toJSON = function toJSON() {
  var json = {};
  var keys = Object.keys(this);
  var i = keys.length;
  while (i--) {
    var key = keys[i];
    if (key === 'message' || key === MESSAGE_CACHE) {
      // nothing
    } else {
      json[key] = this[key];
    }
  }
  return json;
};

Record._Trace = Trace;
Record._UNCAUGHT_SYMBOL = UNCAUGHT_SYMBOL;
module.exports = Record;
<commit_msg>Record.toJSON converts timestamp to Date<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const util = require('util');

const stacktrace = require('stack-trace');
const Symbol = require('symbol');

const LEVELS = require('./levels');
const printf = require('./utils/printf');

const UNCAUGHT_SYMBOL = Symbol();
const LOG_VERSION = 1; // increment when log format changes
const HOSTNAME = require('os').hostname();
const MESSAGE_CACHE = '__message:' + Math.random();

// stack formatter helper
function stack(e) {
  return {
    toString: function() {
      // first line is err.message, which we already show, so start from
      // second line
      return e.stack.substr(e.stack.indexOf('\n'));
    },
    toJSON: function() {
      return stacktrace.parse(e);
    }
  };
}

function Trace(fn) {
  Error.captureStackTrace(this, fn);
}

Trace.prototype.toJSON = function() {
  return '[object Trace]';
};

function Record(name, level, args) {
  this.name = name;
  this.level = level;
  this.levelname = LEVELS.getLevelName(level);
  this.args = args;
  this.pid = process.pid;
  this.host = HOSTNAME;
  this.v = LOG_VERSION;
  this.timestamp = Date.now();

  var i = args.length;
  var trace;
  var isErr = false;
  while (i--) {
    var a = args[i];
    if (a && ((isErr = util.isError(a)) || a instanceof Trace)) {
      trace = a;
      break;
    }
  }
  this.exception = isErr;
  this.uncaughtException = isErr ? trace[UNCAUGHT_SYMBOL] : undefined;
  this.stack = trace ? stack(trace) : undefined;

}

Record.prototype.name = undefined;
Record.prototype.level = undefined;
Record.prototype.levelname = undefined;
Record.prototype.args = undefined;
Record.prototype.pid = undefined;
Record.prototype.host = undefined;
Record.prototype.v = undefined;
Record.prototype.timestamp = undefined;
Record.prototype.exception = undefined;
Record.prototype.uncaughtException = undefined;
Record.prototype.stack = undefined;
Record.prototype[MESSAGE_CACHE] = undefined;

Object.defineProperty(Record.prototype, 'message', {
  enumerable: true,
  get: function() {
    if (!this[MESSAGE_CACHE]) {
      var args = this.args;
      var message = args[0];
      var isString = typeof message === 'string';
      if (!isString || args.length > 1) {
        if (!isString) {
          args = new Array(this.args.length + 1);
          args[0] = '%?';
          var i = args.length - 1;
          while (i--) {
            args[i + 1] = this.args[i];
          }
        }
        message = printf.apply(null, args);
      }
      this[MESSAGE_CACHE] = message;
    }
    return this[MESSAGE_CACHE];
  }
});

Record.prototype.toJSON = function toJSON() {
  var json = {};
  var keys = Object.keys(this);
  var i = keys.length;
  while (i--) {
    var key = keys[i];
    if (key === 'message' || key === MESSAGE_CACHE) {
      // nothing
    } else if (key === 'timestamp') {
      json[key] = new Date(this[key]);
    } else {
      json[key] = this[key];
    }
  }
  return json;
};

Record._Trace = Trace;
Record._UNCAUGHT_SYMBOL = UNCAUGHT_SYMBOL;
module.exports = Record;

{'fraction_non_alphanumeric': 0.1444723618090452, 'fraction_numerical': 0.003454773869346734, 'mean_word_length': 2.9981167608286254, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1144849', 'n_tokens_mistral': 2027, 'n_tokens_neox': 2013, 'n_words': 531}