blob: 37d36f9eeceb90c824cc192a15168c889c0abc62 [file] [log] [blame]
Matteo Scandoloa5d03d52016-07-21 11:35:46 -07001// TODO write tests for log
2
3/* eslint-disable angular/ng_window_service*/
4
5angular.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;
16 };
17 // Save the original $log.debug()
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;
23
24 // create the replacement function
25 const replacement = (fn) => {
26 return function(){
27 //console.log(`Is Log Enabled: ${isLogEnabled()}`)
28 if(!isLogEnabled()){
29 // console.log('logging is disabled');
30 return;
31 }
32
33 let args = [].slice.call(arguments);
34 let now = new Date();
35
36 // Prepend timestamp
37 args[0] = `[${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}] ${args[0]}`;
38
39 // HACK awfull fix for angular mock implementation whithin jasmine test failing issue
40 if (angular.isFunction($delegate.reset) && !($delegate.debug.logs instanceof Array)) {
41 // if we are within the mock and did not reset yet, we call it to avoid issue
42 // console.log('mock log impl fix to avoid logs array not existing...');
43 $delegate.reset();
44 }
45
46 // Call the original with the output prepended with formatted timestamp
47
48 return fn.apply(null, args)
49 };
50 };
51
52 $delegate.info = replacement(infoFn);
53 $delegate.log = replacement(logFn);
54 $delegate.warn = replacement(warnFn);
55 //$delegate.error = replacement(errorFn); // note this will prevent errors to be printed
56 $delegate.debug = replacement(debugFn);
57
58 return $delegate;
59 }]);
60}]);