Quellcode durchsuchen

Safari: Show log timestamps when in DEBUG mode (#632)

Danilo Bargen vor 6 Jahren
Ursprung
Commit
cc9aa481d7
1 geänderte Dateien mit 32 neuen und 0 gelöschten Zeilen
  1. 32 0
      src/app.ts

+ 32 - 0
src/app.ts

@@ -27,6 +27,7 @@ import './filters';
 import './partials/messenger';
 import './partials/welcome';
 import './services';
+import {BrowserService} from './services/browser';
 import './threema/container';
 
 // Configure asynchronous events
@@ -138,4 +139,35 @@ angular.module('3ema', [
     }]);
 }])
 
+.run([
+    '$log', 'CONFIG', 'BrowserService',
+    function($log: ng.ILogService, CONFIG: threema.Config, browserService: BrowserService) {
+        // For Safari (when in DEBUG mode), monkey-patch $log to show timestamps.
+
+        if (!(CONFIG.DEBUG && browserService.getBrowser().isSafari)) {
+            return;
+        }
+
+        const oldLog = $log.log;
+        const oldInfo = $log.info;
+        const oldWarn = $log.warn;
+        const oldDebug = $log.debug;
+        const oldError = $log.error;
+
+        function enhanceLogging(wrapped) {
+            return function(data) {
+                const now = new Date();
+                const currentDate = `[${now.toISOString()}.${now.getMilliseconds()}]`;
+                wrapped.apply(this, [currentDate, ...arguments]);
+            };
+        }
+
+        $log.log = enhanceLogging(oldLog);
+        $log.info = enhanceLogging(oldInfo);
+        $log.warn = enhanceLogging(oldWarn);
+        $log.debug = enhanceLogging(oldDebug);
+        $log.error = enhanceLogging(oldError);
+    },
+])
+
 ;