blob: 6f2628da0608f22df27df03eced640ff78f8d64f [file] [log] [blame]
Matteo Scandolo17bf8242017-01-23 17:30:39 -08001export 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;
Matteo Scandolo4e870232017-01-30 13:43:05 -080010 // let errorFn = $delegate.error;
Matteo Scandolo17bf8242017-01-23 17:30:39 -080011 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
Matteo Scandolo47860fe2017-02-02 12:05:55 -080024 args.unshift(`[${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}]`);
Matteo Scandolo17bf8242017-01-23 17:30:39 -080025
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);
Matteo Scandolo4e870232017-01-30 13:43:05 -080034 // $delegate.error = replacement(errorFn); // note this will prevent errors to be printed
Matteo Scandolo17bf8242017-01-23 17:30:39 -080035 $delegate.debug = replacement(debugFn);
36
37 return $delegate;
38 });
39}