'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var fs = require('fs');
var nodeUtils = require('@teclone/node-utils');
var os = require('os');
var path = require('path');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () {
            return e[k];
          }
        });
      }
    });
  }
  n['default'] = e;
  return Object.freeze(n);
}

var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);

var Logger = /*#__PURE__*/function () {
  function Logger(config) {
    _classCallCheck__default['default'](this, Logger);

    _defineProperty__default['default'](this, "config", void 0);

    _defineProperty__default['default'](this, "errorLogHandle", void 0);

    _defineProperty__default['default'](this, "accessLogHandle", void 0);

    this.config = config;
    var errorLogPath = path.resolve(this.config.entryPath, this.config.errorLog);
    var accessLogPath = path.resolve(this.config.entryPath, this.config.accessLog);
    nodeUtils.mkDirSync(errorLogPath);
    nodeUtils.mkDirSync(accessLogPath);
    this.errorLogHandle = fs__namespace.openSync(path.resolve(this.config.entryPath, this.config.errorLog), 'a');
    this.accessLogHandle = fs__namespace.openSync(path.resolve(this.config.entryPath, this.config.accessLog), 'a');
  }
  /**
   * log warning message to the console
   */


  _createClass__default['default'](Logger, [{
    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;
    }
    /**
     * close the handles once the server is closed
     */

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

  }, {
    key: "logError",
    value: function logError(err) {
      if (err instanceof Error) {
        var now = new Date();
        fs__namespace.writeSync(this.errorLogHandle, "[".concat(now.toUTCString(), "] ").concat(err.stack).concat(os.EOL));

        if (this.config.env !== 'production') {
          console.error(err);
        }
      }

      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__namespace.writeSync(this.accessLogHandle, log);
    }
    /**
     * log request response profile
     */

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

      if (this.config.env === 'development' && 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: %s \x1b[32m%d\x1b[0m ~%dms ~%dms\x1b[0m %s';
        console.log(template, req.method, req.url, res.statusCode, requestTime, responseTime, req.headers['user-agent']);
      }

      return this;
    }
  }]);

  return Logger;
}();

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