blob: 54b9e94e46c5bfd06dfa3bca89f8bea7cffbfa7c [file] [log] [blame]
Matteo Scandolo686547a2017-08-08 13:05:25 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070019// TODO write tests for log
20
21/* eslint-disable angular/ng_window_service*/
22
23angular.module('xos.helpers')
24.config([ '$provide', function( $provide )
25{
26 // Use the `decorator` solution to substitute or attach behaviors to
27 // original service instance; @see angular-mocks for more examples....
28
29 $provide.decorator( '$log', [ '$delegate', function( $delegate )
30 {
31
32 const isLogEnabled = () => {
33 return window.location.href.indexOf('debug=true') >= 0;
34 };
35 // Save the original $log.debug()
36 let logFn = $delegate.log;
37 let infoFn = $delegate.info;
38 let warnFn = $delegate.warn;
39 //let errorFn = $delegate.error;
40 let debugFn = $delegate.debug;
41
42 // create the replacement function
43 const replacement = (fn) => {
44 return function(){
45 //console.log(`Is Log Enabled: ${isLogEnabled()}`)
46 if(!isLogEnabled()){
47 // console.log('logging is disabled');
48 return;
49 }
50
51 let args = [].slice.call(arguments);
52 let now = new Date();
53
54 // Prepend timestamp
55 args[0] = `[${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}] ${args[0]}`;
56
57 // HACK awfull fix for angular mock implementation whithin jasmine test failing issue
58 if (angular.isFunction($delegate.reset) && !($delegate.debug.logs instanceof Array)) {
59 // if we are within the mock and did not reset yet, we call it to avoid issue
60 // console.log('mock log impl fix to avoid logs array not existing...');
61 $delegate.reset();
62 }
63
64 // Call the original with the output prepended with formatted timestamp
65
66 return fn.apply(null, args)
67 };
68 };
69
70 $delegate.info = replacement(infoFn);
71 $delegate.log = replacement(logFn);
72 $delegate.warn = replacement(warnFn);
73 //$delegate.error = replacement(errorFn); // note this will prevent errors to be printed
74 $delegate.debug = replacement(debugFn);
75
76 return $delegate;
77 }]);
78}]);