log.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. (function() {
  2. var ffSupport, formats, getOrderedMatches, hasMatches, isFF, isIE, isOpera, isSafari, log, makeArray, operaSupport, safariSupport, stringToArgs, _log;
  3. if (!(window.console && window.console.log)) {
  4. return;
  5. }
  6. log = function() {
  7. var args;
  8. args = [];
  9. makeArray(arguments).forEach(function(arg) {
  10. if (typeof arg === 'string') {
  11. return args = args.concat(stringToArgs(arg));
  12. } else {
  13. return args.push(arg);
  14. }
  15. });
  16. return _log.apply(window, args);
  17. };
  18. _log = function() {
  19. return console.log.apply(console, makeArray(arguments));
  20. };
  21. makeArray = function(arrayLikeThing) {
  22. return Array.prototype.slice.call(arrayLikeThing);
  23. };
  24. formats = [
  25. {
  26. regex: /\*([^\*]+)\*/,
  27. replacer: function(m, p1) {
  28. return "%c" + p1 + "%c";
  29. },
  30. styles: function() {
  31. return ['font-style: italic', ''];
  32. }
  33. }, {
  34. regex: /\_([^\_]+)\_/,
  35. replacer: function(m, p1) {
  36. return "%c" + p1 + "%c";
  37. },
  38. styles: function() {
  39. return ['font-weight: bold', ''];
  40. }
  41. }, {
  42. regex: /\`([^\`]+)\`/,
  43. replacer: function(m, p1) {
  44. return "%c" + p1 + "%c";
  45. },
  46. styles: function() {
  47. return ['background: rgb(255, 255, 219); padding: 1px 5px; border: 1px solid rgba(0, 0, 0, 0.1)', ''];
  48. }
  49. }, {
  50. regex: /\[c\=(?:\"|\')?((?:(?!(?:\"|\')\]).)*)(?:\"|\')?\]((?:(?!\[c\]).)*)\[c\]/,
  51. replacer: function(m, p1, p2) {
  52. return "%c" + p2 + "%c";
  53. },
  54. styles: function(match) {
  55. return [match[1], ''];
  56. }
  57. }
  58. ];
  59. hasMatches = function(str) {
  60. var _hasMatches;
  61. _hasMatches = false;
  62. formats.forEach(function(format) {
  63. if (format.regex.test(str)) {
  64. return _hasMatches = true;
  65. }
  66. });
  67. return _hasMatches;
  68. };
  69. getOrderedMatches = function(str) {
  70. var matches;
  71. matches = [];
  72. formats.forEach(function(format) {
  73. var match;
  74. match = str.match(format.regex);
  75. if (match) {
  76. return matches.push({
  77. format: format,
  78. match: match
  79. });
  80. }
  81. });
  82. return matches.sort(function(a, b) {
  83. return a.match.index - b.match.index;
  84. });
  85. };
  86. stringToArgs = function(str) {
  87. var firstMatch, matches, styles;
  88. styles = [];
  89. while (hasMatches(str)) {
  90. matches = getOrderedMatches(str);
  91. firstMatch = matches[0];
  92. str = str.replace(firstMatch.format.regex, firstMatch.format.replacer);
  93. styles = styles.concat(firstMatch.format.styles(firstMatch.match));
  94. }
  95. return [str].concat(styles);
  96. };
  97. isSafari = function() {
  98. return /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);
  99. };
  100. isOpera = function() {
  101. return /OPR/.test(navigator.userAgent) && /Opera/.test(navigator.vendor);
  102. };
  103. isFF = function() {
  104. return /Firefox/.test(navigator.userAgent);
  105. };
  106. isIE = function() {
  107. return /MSIE/.test(navigator.userAgent);
  108. };
  109. safariSupport = function() {
  110. var m;
  111. m = navigator.userAgent.match(/AppleWebKit\/(\d+)\.(\d+)(\.|\+|\s)/);
  112. if (!m) {
  113. return false;
  114. }
  115. return 537.38 <= parseInt(m[1], 10) + (parseInt(m[2], 10) / 100);
  116. };
  117. operaSupport = function() {
  118. var m;
  119. m = navigator.userAgent.match(/OPR\/(\d+)\./);
  120. if (!m) {
  121. return false;
  122. }
  123. return 15 <= parseInt(m[1], 10);
  124. };
  125. ffSupport = function() {
  126. return window.console.firebug || window.console.exception;
  127. };
  128. if (isIE() || (isFF() && !ffSupport()) || (isOpera() && !operaSupport()) || (isSafari() && !safariSupport())) {
  129. window.log = _log;
  130. } else {
  131. window.log = log;
  132. }
  133. window.log.l = _log;
  134. }).call(this);