Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
//log.js
(function () {
  /* global adventurejs A */

  var p = adventurejs.Game.prototype;

  /**
   * Custom logging function.
   * @method adventurejs.Game#log
   * @memberOf adventurejs.Game
   * @param {String|Number} method 0=log, 1=warn, 2=error
   * @param {String|Number} level 0=critical, 1=high, 2=medium, 3=low
   * @param {*} msg Message for console.
   */
  p.log = function Game_log(code, method, level, msg, keyword) {
    if ("string" !== typeof keyword) keyword = "";

    const hook = ["verbaction", "verbreaction", "verbphase"].includes(
      keyword.toLowerCase()
    );

    // no logging below 0
    if (0 > this.game.settings.log_level) {
      return;
    }

    // exclude keywords that haven't been included
    if (
      method === "log" &&
      !this.game.settings.log_keywords[keyword.toLowerCase()] &&
      !this.game.settings.log_keywords.all
    ) {
      return;
    }

    // don't log above current level
    var levels = ["critical", "high", "medium", "low"];
    if ("string" === typeof level) {
      level = levels.indexOf(level);
    }
    if (level > this.game.settings.log_level) {
      return;
    }

    var methods = ["log", "warn", "error"];
    if ("undefined" === typeof method || null === method) {
      method = "log";
    }
    if ("number" === typeof method) {
      method = methods[method];
    }

    if (Array.isArray(msg)) {
      msg = msg.join(" ");
    }

    let src = "";
    let match = msg.match(/^\[[^\]]+\]\s*/);
    if (match) {
      src = match[0];
    }
    if (src) {
      msg = msg.replace(/^\[[^\]]+\]\s*/, "");
    }
    msg = msg.replace("⤷", "");
    if (!this.game.settings.include_filename_in_log) {
      src = "";
    }

    const green = "#36e400";
    const yellow = "#e5d254";
    const red = "#ef866d";
    const orange = "#ff9700";
    const hookcolors = {
      verbphase: "#31f0bf",
      verbaction: "#4fcbff",
      verbreaction: "#83a9fb",
    };
    let codecolor = green;

    if (method === "warn") {
      codecolor = yellow;
    } else if (method === "error") {
      codecolor = red;
    } else if (keyword.toLowerCase() === "debug") {
      codecolor = orange;
    } else if (hook) {
      codecolor = hookcolors[keyword.toLowerCase()];
    }

    let fontcolor = [green].includes(codecolor) ? "#FFF" : codecolor;
    let tagcolor = codecolor;

    let line = ""; //"line-height: 1.25em;";
    let common = `${line} color: #4a3532; font-weight: bold; padding: 3px 5px; `;

    let codestyle = `background: ${codecolor}; ${common} margin: 0 -2px 4px 0;`;
    let tagstyle = `background: ${tagcolor}; ${common} margin: 0 0 0px 0; `;
    let srcstyle = `color: #bbb;`;
    let msgstyle = `color: ${fontcolor}; ${line} margin-bottom: 0px; `;
    if (method !== "error") method = "log";
    if ("undefined" !== typeof console[method]) {
      console[method](
        `%c${code}%c %c${keyword}%c %c${src}%c%c${msg}%c`,
        codestyle,
        "",
        tagstyle,
        "",
        srcstyle,
        "",
        msgstyle,
        ""
      );
      // if (level <= this.game.settings.log_trace_level && method === "log") {
      //   console.groupCollapsed("Trace...");
      //   console.trace();
      //   console.groupEnd();
      // }
    }

    return;
  };
})();