Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 1 | // TODO write tests for log |
| 2 | |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 3 | /* eslint-disable angular/ng_window_service*/ |
| 4 | |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 5 | angular.module('xos.helpers') |
| 6 | .config([ '$provide', function( $provide ) |
| 7 | { |
| 8 | // Use the `decorator` solution to substitute or attach behaviors to |
| 9 | // original service instance; @see angular-mocks for more examples.... |
| 10 | |
| 11 | $provide.decorator( '$log', [ '$delegate', function( $delegate ) |
| 12 | { |
| 13 | |
| 14 | const isLogEnabled = () => { |
| 15 | return window.location.href.indexOf('debug=true') >= 0; |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 16 | }; |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 17 | // Save the original $log.debug() |
Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame] | 18 | let logFn = $delegate.log; |
| 19 | let infoFn = $delegate.info; |
| 20 | let warnFn = $delegate.warn; |
| 21 | let errorFn = $delegate.error; |
| 22 | let debugFn = $delegate.debug; |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 23 | |
| 24 | // create the replacement function |
| 25 | const replacement = (fn) => { |
| 26 | return function(){ |
Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame] | 27 | // console.log(`Is Log Enabled: ${isLogEnabled()}`) |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 28 | if(!isLogEnabled()){ |
Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame] | 29 | // console.log('logging is disabled'); |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 30 | return; |
| 31 | } |
| 32 | let args = [].slice.call(arguments); |
| 33 | let now = new Date(); |
| 34 | |
| 35 | // Prepend timestamp |
| 36 | args[0] = `[${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}] ${args[0]}`; |
| 37 | |
Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame] | 38 | // HACK awfull fix for angular mock implementation whithin jasmine test failing issue |
Matteo Scandolo | b585d38 | 2016-06-15 14:53:33 -0700 | [diff] [blame] | 39 | if (angular.isFunction($delegate.reset) && !($delegate.debug.logs instanceof Array)) { |
Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame] | 40 | // if we are within the mock and did not reset yet, we call it to avoid issue |
| 41 | // console.log('mock log impl fix to avoid logs array not existing...'); |
| 42 | $delegate.reset(); |
| 43 | } |
| 44 | |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 45 | // Call the original with the output prepended with formatted timestamp |
| 46 | fn.apply(null, args) |
| 47 | }; |
| 48 | }; |
| 49 | |
Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame] | 50 | $delegate.info = replacement(infoFn); |
| 51 | $delegate.log = replacement(logFn); |
| 52 | $delegate.warn = replacement(warnFn); |
| 53 | $delegate.error = replacement(errorFn); |
| 54 | $delegate.debug = replacement(debugFn); |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 55 | |
| 56 | return $delegate; |
| 57 | }]); |
| 58 | }]); |