Advertisement
fagci

Epsilon notes console

Oct 15th, 2018
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.93 KB | None | 0 0
  1. # Console test
  2.  
  3. <style>
  4. ._cwr {
  5.   position: fixed;
  6.   bottom: 0;
  7.   left: 0;
  8.   right: 0;
  9.   transform: translateY(100%);
  10.   transition: transform ease 150ms;
  11. }
  12.  
  13. ._cwr.opened {
  14.   transform: translateY(0);
  15.   transition: transform ease 150ms;
  16. }
  17.  
  18. ._console {
  19.   font-family: monospace;
  20.   line-height: 1.4;
  21.   font-size: 0.85rem;
  22.   max-height: 25em;
  23.   overflow-y: auto;
  24.   opacity: 0.75;
  25.   z-index: 10;
  26.   padding: 1px 0;
  27.   background: #246;
  28.   color: #f0f0f0;
  29. }
  30. ._cwr .toggler {
  31.   position: absolute;
  32.   top: 0;
  33.   right: 0;
  34.   background: #444;
  35.   transform: translateY(-100%);
  36. }
  37. ._console>pre {
  38.   padding: 0 4px;
  39.   margin: 1px 0;
  40.   border: none;
  41.   border-radius:0;
  42.   min-height:1.4em;
  43. }
  44. ._console i.fa {margin-right:0.5em;}
  45. ._console>pre>* {display: inline}
  46. .log {background: #466;}
  47. .info {background: #464;}
  48. .warn {background: #884;}
  49. .error {background: #844;}
  50. .exception {color:#fff; background: #822}
  51.  
  52. ._console::-webkit-scrollbar { width: 6px; }
  53. ._console::-webkit-scrollbar-track {
  54. background: #882;
  55. border: 2px solid transparent;
  56. background-clip: content-box;
  57. }
  58. ._console::-webkit-scrollbar-thumb {
  59. background: #888;
  60. border: 1px solid;
  61. }
  62. </style>
  63. <script>
  64. ((document) => {
  65.   const logWr = document.createElement('div');
  66.   const log = document.createElement('div');
  67.   const tgl = document.createElement('div');
  68.   ['toggler','fa','fa-2x','fa-terminal'].forEach(c =>
  69.     tgl.classList.add(c)
  70.   );
  71.   log.classList.add('_console');
  72.   logWr.classList.add('_cwr');
  73.   logWr.classList.add('opened');
  74.   logWr.appendChild(tgl);
  75.   logWr.appendChild(log);
  76.  
  77.   tgl.addEventListener('click', e =>
  78.     logWr.classList.toggle('opened')
  79.   );
  80.  
  81.   const _log = (c, msg, icon) => {
  82.     let m = document.createElement('pre');
  83.     m.classList.add(c);
  84.    
  85.     if(icon) {
  86.       const i = document.createElement('i');
  87.       icon.split(' ').forEach(c => i.classList.add(c));
  88.       m.appendChild(i);
  89.     }
  90.  
  91.     let t = document.createTextNode(msg);
  92.  
  93.     m.appendChild(t);
  94.    
  95.     log.appendChild(m);
  96.   };
  97.  
  98.   const _ots = o =>
  99.     typeof o === 'string' ? o : JSON.stringify(o,null,2);
  100.  
  101.   const _times = {};
  102.   const _cnt = {};
  103.  
  104.   Object.getOwnPropertyNames(console)
  105.   .forEach(_v => {
  106.     console[_v] = ((method, _v, _log, _ots) => {
  107.       return function(...a) {
  108.         method.apply(console, arguments);
  109.         const tn = performance.now();
  110.         let i = '';
  111.         switch (_v) {
  112.           case 'log':
  113.           case 'warn':
  114.           case 'info':
  115.           case 'error':
  116.           case 'debug':
  117.             _log(_v, a.map(_ots).join(' '), i);
  118.             break;
  119.           case 'time':
  120.             _times[a[0]] = tn;
  121.             break;
  122.           case 'timeEnd':
  123.             const t = (tn - _times[a[0]]).toFixed(2);
  124.             console.log(`${a[0]}: ${t}ms`);
  125.             break;
  126.           case 'clear':
  127.             log.innerHTML = '';
  128.             console.log('console was cleared');
  129.             break;
  130.           case 'count':
  131.             const n = a[0];
  132.             if(_cnt[n] === undefined) _cnt[n] = 0;
  133.             console.log(`${n}: ${++_cnt[n]}`);
  134.             break;
  135.           case 'assert':
  136.             if(!a[0]) console.error('assertion failed', a);
  137.             break;
  138.           default:
  139.             console.warn(`console.${_v} not implemented yet.`);
  140.         }
  141.        
  142.       };
  143.     })(console[_v], _v, _log, _ots);
  144.   });
  145.  
  146.   window.addEventListener('error', e => {
  147.     _log('exception', `${e.message}. ${e.filename}:${e.lineno}:${e.colno}`);
  148.   });
  149.  
  150.   document.body.appendChild(logWr);
  151. })(document);
  152.  
  153. console.info('Welcome to console');
  154.  
  155. console.info();
  156.  
  157. console.log('Log test');
  158. console.warn('Warn test');
  159. console.error('Error test');
  160. console.time('Time test');
  161.  
  162. for(let i=0;i<5;i++)
  163.  console.count('Count test');
  164.  
  165. console.assert(false, 'some assertion');
  166.  
  167. console.timeEnd('Time test');
  168.  
  169. console.info('Webkit console methods: ', Object.getOwnPropertyNames(console));
  170.  
  171.  
  172. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement