'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty'));
var Constants = require('./Constants');
var fs = require('fs');
var nodeUtils = require('@forensic-js/node-utils');
var os = require('os');

var Logger =
/*#__PURE__*/
function () {
  function Logger(config) {
    _classCallCheck(this, Logger);

    _defineProperty(this, "config", void 0);

    _defineProperty(this, "errorLogHandle", void 0);

    _defineProperty(this, "accessLogHandle", void 0);

    this.config = config;
    var errorLogPath = nodeUtils.resolvePaths(this.config.entryPath, this.config.errorLog);
    var accessLogPath = nodeUtils.resolvePaths(this.config.entryPath, this.config.accessLog);
    nodeUtils.mkDirSync(errorLogPath);
    nodeUtils.mkDirSync(accessLogPath);
    this.errorLogHandle = fs.openSync(nodeUtils.resolvePaths(this.config.entryPath, this.config.errorLog), 'a');
    this.accessLogHandle = fs.openSync(nodeUtils.resolvePaths(this.config.entryPath, this.config.accessLog), 'a');
  }
  /**
   * close the handles once the server is closed
   */


  _createClass(Logger, [{
    key: "close",
    value: function close() {
      fs.closeSync(this.errorLogHandle);
      fs.closeSync(this.accessLogHandle);
      return this;
    }
    /**
     * runs the error logging
     */

  }, {
    key: "logError",
    value: function logError(level, err) {
      var now = new Date();
      fs.writeSync(this.errorLogHandle, "[".concat(now.toUTCString(), "] [").concat(level, "] ").concat(err.stack).concat(os.EOL));
      return this;
    }
    /**
     * logs access information
     */

  }, {
    key: "logAccess",
    value: function logAccess(req, res) {
      var protocol = req.encrypted ? 'Https' : 'Http';
      var startTime = req.startedAt.toUTCString();
      var log = "[".concat(startTime, "] [").concat(req.method, "] '").concat(req.url, " ").concat(protocol, "/").concat(req.httpVersion, "' ").concat(res.statusCode).concat(os.EOL);
      fs.writeSync(this.accessLogHandle, log);
    }
    /**
     * log request response profile
     */

  }, {
    key: "profile",
    value: function profile(req, res) {
      this.logAccess(req, res);

      if (this.config.env === 'dev' && this.config.profileRequest) {
        var requestTime = req.endedAt.getTime() - req.startedAt.getTime();
        var responseTime = res.endedAt.getTime() - res.startedAt.getTime();
        var template = res.statusCode >= 400 ? '%s: %s \x1b[31m%d\x1b[0m ~%dms ~%dms\x1b[0m' : '%s: %s \x1b[32m%d\x1b[0m ~%dms ~%dms\x1b[0m';
        console.log(template, req.method, req.url, res.statusCode, requestTime, responseTime);
      }

      return this;
    }
    /**
     * logs fatal error to error log file and ends the response
     */

  }, {
    key: "fatal",
    value: function fatal(err) {
      this.logError(Constants.ERROR_LEVELS.FATAL, err); // const statusCode = this.config.env === 'prod' ? 500 : 200;
      // const data = this.config.env === 'prod' ? undefined : err.toString();
      // /* istanbul ignore else */
      // if (!response.finished) {
      //   return response
      //     .status(statusCode)
      //     .removeHeaders('Content-Length')
      //     .end(data);
      // } else {
      //   return Promise.resolve(false);
      // }
    }
    /**
     * log warning message to the console
     */

  }, {
    key: "warn",
    value: function warn(message) {
      console.log('\x1b[1m\x1b[31m%s\x1b[0m', message);
      return this;
    }
    /**
     * log info message to the console
     */

  }, {
    key: "info",
    value: function info(message) {
      console.log('\x1b[1m\x1b[32m%s\x1b[0m', message);
      return this;
    }
  }]);

  return Logger;
}();

module.exports = Logger;
//# sourceMappingURL=Logger.js.map
