debug.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @fileOverview
  3. *
  4. * 支持各种调试后门
  5. *
  6. * @author: techird
  7. * @copyright: Baidu FEX, 2014
  8. */
  9. define(function(require, exports, module) {
  10. var format = require('./format');
  11. function noop() {}
  12. function stringHash(str) {
  13. var hash = 0;
  14. for (var i = 0; i < str.length; i++) {
  15. hash += str.charCodeAt(i);
  16. }
  17. return hash;
  18. }
  19. /* global console */
  20. function Debug(flag) {
  21. var debugMode = this.flaged = window.location.search.indexOf(flag) != -1;
  22. if (debugMode) {
  23. var h = stringHash(flag) % 360;
  24. var flagStyle = format(
  25. 'background: hsl({0}, 50%, 80%); ' +
  26. 'color: hsl({0}, 100%, 30%); ' +
  27. 'padding: 2px 3px; ' +
  28. 'margin: 1px 3px 0 0;' +
  29. 'border-radius: 2px;', h);
  30. var textStyle = 'background: none; color: black;';
  31. this.log = function() {
  32. var output = format.apply(null, arguments);
  33. console.log(format('%c{0}%c{1}', flag, output), flagStyle, textStyle);
  34. };
  35. } else {
  36. this.log = noop;
  37. }
  38. }
  39. return module.exports = Debug;
  40. });