console-wrapper.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // This easy console wrapper introduces the logging level to console for
  2. // preventing console outputs caused when we purposely test the code path
  3. // including console outputs.
  4. //
  5. // Example: Prevent the console warnings caused by Color.setStyle().
  6. // const c = new Color();
  7. // console.level = CONSOLE_LEVEL.ERROR;
  8. // c.setStyle( 'rgba(255,0,0,0.5)' );
  9. // console.level = CONSOLE_LEVEL.DEFAULT;
  10. //
  11. // See https://github.com/mrdoob/three.js/issues/20760#issuecomment-735190998
  12. export const CONSOLE_LEVEL = {
  13. OFF: 0,
  14. ERROR: 1,
  15. WARN: 2,
  16. LOG: 3,
  17. INFO: 4,
  18. DEBUG: 5,
  19. ALL: 6,
  20. DEFAULT: 6
  21. };
  22. console.level = CONSOLE_LEVEL.DEFAULT;
  23. // Save the original methods
  24. console._error = console.error;
  25. console._warn = console.warn;
  26. console._log = console.log;
  27. console._info = console.info;
  28. console._debug = console.debug;
  29. // Wrap console methods
  30. console.error = function () {
  31. if ( this.level >= CONSOLE_LEVEL.ERROR ) this._error.apply( this, arguments );
  32. };
  33. console.warn = function () {
  34. if ( this.level >= CONSOLE_LEVEL.WARN ) this._warn.apply( this, arguments );
  35. };
  36. console.log = function () {
  37. if ( this.level >= CONSOLE_LEVEL.LOG ) this._log.apply( this, arguments );
  38. };
  39. console.info = function () {
  40. if ( this.level >= CONSOLE_LEVEL.INFO ) this._info.apply( this, arguments );
  41. };
  42. console.debug = function () {
  43. if ( this.level >= CONSOLE_LEVEL.DEBUG ) this._debug.apply( this, arguments );
  44. };