Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/modules / ___dist / log / index.js
Size: Mime:
// store a reference to the console.error
// since it is not a bound function, it would lose scope of console
// so we bind it to the console instance it is @memberOf
const { fromArgumentsToArray, NO_OP } = require('../exotic')
const oneStorage = require('../persistance').ls
const { defineFinal, stringIncludesAll } = require('../chain-able')
// const { isString } = require('exotic')
// const { NO_OP } = require('exotic')

// is inChain as stringIncludesAll
// function haystackHasNeedle(needle) {
//   return function doesSatisfyNeedles(haystack) {
//     return haystack.includes(needle)
//   }
// }
// done this way to use it in the browser
// function makePredicateIsStringIncludingAll(list) {
//   return function isStringSatisfyingIncludes(message) {
//     return typeof message === 'string' && list.every()
//   }
// }

// as let for when we strip console
// this is a very tricky way to make it log the original location
// https://github.com/visionmedia/debug/issues/105
const consoleLogReference = console.log
const consoleErrorReference = console.error
// let consoleLogReference = console.log.bind(console)
// let consoleErrorReference = console.error.bind(error)
// let consoleWarnReference = Function.prototype.apply.bind(console.warn, console)

// https://developers.google.com/web/tools/chrome-devtools/console/console-reference?utm_source=dcc&utm_medium=redirect&utm_campaign=2016q3
// @tutorial https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
// re-defines error on console
function silenceKeys(type = 'error') {
  Object.defineProperty(console, type, {
    // allows it to be written over if needed, defaults to writable: false
    // (which is ~ immutable, not deletable / changeable)
    configurable: true,

    // shorthand method declaration
    // this is the value on any object define
    // see the typehints for additional properties
    // and
    value(message) {
      // @todo
      // if (stringIncludesAll(['arning', 'key']))  return
      // if (stringIncludesAll(['Slow', 'network'])) return
      // if (stringIncludesAll(['Failed', 'prop'])) return

      // ignore React.warning from keys
      if (typeof message === 'string' && message.includes('arning') && message.includes('key')) {
        return
      }

      if (typeof message === 'string' && message.includes('Slow') && message.includes('network')) {
        return
      }

      if (typeof message === 'string' && message.includes('Failed') && message.includes('prop')) {
        return
      }

      if (typeof message === 'string' && message.includes('already had')) {
        return
      }

      if (typeof message === 'string' && message.includes('arning') === false) {
        const args = fromArgumentsToArray.apply(undefined, arguments)
        oneStorage.set('revert_console_for_stack_trace', { stack: args })
      }

      // apply original arguments to the reference
      // keep `thisArg` = console
      // apply is one of the only ways optimized by any js engine when you reference arguments
      // (arguments[index] is another)
      return consoleErrorReference.apply(console, arguments)

      // http://stackoverflow.com/questions/13815640/a-proper-wrapper-for-console-log-with-correct-line-number
      // return Function.prototype.apply.call(consoleErrorReference, console, arguments)
      // if we were using debug js style wrapper, this would let us return logger with right source trace
      // return Function.prototype.apply.bind(console.log, console, arguments)
    },
  })
}

// const SHOULD_REVERT = oneStorage.has('revert_console_for_stack_trace')
// if (SHOULD_REVERT !== true) {
//   silenceKeys()
// }

// eslint-disable-next-line
function setup() {
  // @todo - hmr warnings are pretty bloody annoying too
  silenceKeys('error')
  // silenceKeys('warning')

  const IS_BROWSER = typeof window === 'object'
  const REALM_ROOT = IS_BROWSER ? window : global
  // IS_HOT &&
  const IS_HOT_IN_THE_HOT_TUB = typeof REALM_ROOT.__REACT_HOT_LOADER__ === 'object' && module.hot

  // silences HMR warnings - very annoying
  // better to silence with this, than to silence keys on warning
  // or the trace will have an issue
  if (IS_HOT_IN_THE_HOT_TUB) {
    REALM_ROOT.__REACT_HOT_LOADER__.warnings = false
    REALM_ROOT.__REACT_HOT_LOADER__.errors = false
  }

  // function NO_OP() {}
  // define is on server|test_env for silencing react logs on server
  const dev = process.env.IS_BASIC_LOGGING_SILENCED
    ? NO_OP
    : consoleLogReference.bind(console)

  defineFinal(console, 'dev', dev)
  defineFinal(console, 'event', dev)
  defineFinal(console, 'action', dev)
  defineFinal(console, 'render', dev)
}


exports.setup = setup

// console.debug('namespace', stronglyTypedDebug)

// https://github.com/speedskater/babel-plugin-rewire

// ignores error from webpack HMR with postcss
// Object.defineProperty(console, 'log', {
//   configurable: true,
//   value(message, formatting) {
//     if (typeof message === 'string' && typeof formatting === 'string' && formatting.includes('color: #999933;')) {
//       consoleLogReference('POSTCSS_WEBPACK', { message: message.split('\n') })
//       // eslint-disable-next-line
//     } else if (message && Object.prototype.hasOwnProperty.call(message, '$mobx')) {
//       // make any observable a table
//       console.table(message.toJSON())
//     } else if (formatting && Object.prototype.hasOwnProperty.call(formatting, '$mobx')) {
//       console.table(formatting.toJSON())
//     } else {
//       consoleLogReference.apply(console, arguments)
//     }
//   },
// })