consoleshim.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. (function(window,undefined){
  2. "use strict";
  3. var deepCopy = function(p,c) {
  4. c = c||{};
  5. for (var i in p) {
  6. if (typeof p[i] === 'object') {
  7. c[i] = (p[i].constructor === Array)?[]:{};
  8. deepCopy(p[i],c[i]);
  9. }else{
  10. c[i] = p[i];
  11. }
  12. }
  13. return c;
  14. },
  15. oC = typeof console != 'undefined' ? deepCopy(console) : {},
  16. i,
  17. console = window.console,
  18. noop = function(){},
  19. a = ['log','info','debug','error','warn'];
  20. console._call = function(index,name,args){
  21. console._old[index].apply(window,args);
  22. var d = $('#console-log'),
  23. msg = name+': '+Array.prototype.slice.call(args,0).join("\t");
  24. if(d.children().length === 0){
  25. d.css({
  26. margin: 0,
  27. padding: 0
  28. }).append($('<pre>').attr('id','console-log-pre'));
  29. }
  30. d.children('pre').each(function(){
  31. $(this).html($(this).html()+""+msg+"<br/>").scrollTop($(this).height());
  32. });
  33. return msg;
  34. };
  35. console._old = [];
  36. for(i=0;i<a.length;i++){
  37. (function(name,index){
  38. console._old.push(oC[name] || noop);
  39. if(a[i] != 'error'){
  40. console[name] = function(){
  41. return console._call(index,name.toUpperCase(),arguments)
  42. }
  43. }else{
  44. console[name] = function(){
  45. var args = [];
  46. for(var i in arguments){
  47. if(typeof arguments[i].message == 'undefined'){
  48. args.push(arguments[i]);
  49. }else{
  50. args.push(arguments[i].message);
  51. }
  52. }
  53. return console._call(index,name.toUpperCase(),args)
  54. }
  55. }
  56. })(a[i],i);
  57. }
  58. })(window);