Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 1 | export default function XosLogDecorator($provide: ng.auto.IProvideService) { |
| 2 | $provide.decorator('$log', function($delegate: any) { |
| 3 | const isLogEnabled = () => { |
| 4 | return window.location.href.indexOf('debug=true') >= 0; |
| 5 | }; |
| 6 | // Save the original $log.debug() |
| 7 | let logFn = $delegate.log; |
| 8 | let infoFn = $delegate.info; |
| 9 | let warnFn = $delegate.warn; |
| 10 | let errorFn = $delegate.error; |
| 11 | let debugFn = $delegate.debug; |
| 12 | |
| 13 | // create the replacement function |
| 14 | const replacement = (fn) => { |
| 15 | return function(){ |
| 16 | if (!isLogEnabled()) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | let args = [].slice.call(arguments); |
| 21 | let now = new Date(); |
| 22 | |
| 23 | // Prepend timestamp |
| 24 | args[0] = `[${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}] ${args[0]}`; |
| 25 | |
| 26 | // Call the original with the output prepended with formatted timestamp |
| 27 | fn.apply(null, args); |
| 28 | }; |
| 29 | }; |
| 30 | |
| 31 | $delegate.info = replacement(infoFn); |
| 32 | $delegate.log = replacement(logFn); |
| 33 | $delegate.warn = replacement(warnFn); |
| 34 | $delegate.error = replacement(errorFn); // note this will prevent errors to be printed |
| 35 | $delegate.debug = replacement(debugFn); |
| 36 | |
| 37 | return $delegate; |
| 38 | }); |
| 39 | } |