blob: 37d36f9eeceb90c824cc192a15168c889c0abc62 [file] [log] [blame]
Matteo Scandolo7bc39c42016-04-20 11:38:42 -07001// TODO write tests for log
2
Matteo Scandolob585d382016-06-15 14:53:33 -07003/* eslint-disable angular/ng_window_service*/
4
Matteo Scandolo7bc39c42016-04-20 11:38:42 -07005angular.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 Scandolob585d382016-06-15 14:53:33 -070016 };
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070017 // Save the original $log.debug()
Matteo Scandolo280dcd32016-05-16 09:59:38 -070018 let logFn = $delegate.log;
19 let infoFn = $delegate.info;
20 let warnFn = $delegate.warn;
Matteo Scandolo7c798182016-06-28 10:33:03 -070021 //let errorFn = $delegate.error;
Matteo Scandolo280dcd32016-05-16 09:59:38 -070022 let debugFn = $delegate.debug;
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070023
24 // create the replacement function
25 const replacement = (fn) => {
26 return function(){
Matteo Scandolo7c798182016-06-28 10:33:03 -070027 //console.log(`Is Log Enabled: ${isLogEnabled()}`)
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070028 if(!isLogEnabled()){
Matteo Scandolo280dcd32016-05-16 09:59:38 -070029 // console.log('logging is disabled');
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070030 return;
31 }
Matteo Scandolo7c798182016-06-28 10:33:03 -070032
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070033 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
Matteo Scandolo280dcd32016-05-16 09:59:38 -070039 // HACK awfull fix for angular mock implementation whithin jasmine test failing issue
Matteo Scandolob585d382016-06-15 14:53:33 -070040 if (angular.isFunction($delegate.reset) && !($delegate.debug.logs instanceof Array)) {
Matteo Scandolo280dcd32016-05-16 09:59:38 -070041 // 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
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070046 // Call the original with the output prepended with formatted timestamp
Matteo Scandolo7c798182016-06-28 10:33:03 -070047
48 return fn.apply(null, args)
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070049 };
50 };
51
Matteo Scandolo280dcd32016-05-16 09:59:38 -070052 $delegate.info = replacement(infoFn);
53 $delegate.log = replacement(logFn);
54 $delegate.warn = replacement(warnFn);
Matteo Scandolo7c798182016-06-28 10:33:03 -070055 //$delegate.error = replacement(errorFn); // note this will prevent errors to be printed
Matteo Scandolo280dcd32016-05-16 09:59:38 -070056 $delegate.debug = replacement(debugFn);
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070057
58 return $delegate;
59 }]);
60}]);