From 87ddcef33359700a4a0cb33b871fd7f011bee382 Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 2 Feb 2016 17:55:27 -0800 Subject: [PATCH] Make debug log persistent Save log entries in indexedDB rather than just in memory. Reload them whenever the background page is refreshed. // FREEBIE --- background.html | 2 +- js/background.js | 1 + js/database.js | 6 ++++++ js/debugLog.js | 48 ++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/background.html b/background.html index 056275b5..a330a71a 100644 --- a/background.html +++ b/background.html @@ -311,9 +311,9 @@
- + diff --git a/js/background.js b/js/background.js index 8aa6e0dd..52a5e009 100644 --- a/js/background.js +++ b/js/background.js @@ -4,6 +4,7 @@ ;(function() { 'use strict'; + console.log('background page reloaded'); // register some chrome listeners if (chrome.notifications) { chrome.notifications.onClicked.addListener(function() { diff --git a/js/database.js b/js/database.js index 5f8c364b..69a7ee32 100644 --- a/js/database.js +++ b/js/database.js @@ -100,6 +100,12 @@ }); storage.fetch(); } + }, + { + version: "7.0", + migrate: function(transaction, next) { + transaction.db.createObjectStore("debug"); + } } ]; }()); diff --git a/js/debugLog.js b/js/debugLog.js index c7d79b82..1cbcec5a 100644 --- a/js/debugLog.js +++ b/js/debugLog.js @@ -3,22 +3,58 @@ */ (function () { 'use strict'; + + var LogEntry = Backbone.Model.extend({ + database: Whisper.Database, + storeName: 'debug', + printTime: function() { + try { + return new Date(this.get('time')).toISOString(); + } catch(e) { + return ''; + } + }, + printValue: function() { + return this.get('value') || ''; + } + }); + + var DebugLog = Backbone.Collection.extend({ + database: Whisper.Database, + storeName: 'debug', + model: LogEntry, + comparator: 'time', + initialize: function() { + this.fetch({remove: false}); + }, + log: function(str) { + this.add({time: Date.now(), value: str}).save(); + if (this.length > MAX_MESSAGES) { + this.at(0).destroy(); + } + }, + print: function() { + return this.map(function(entry) { + return entry.printTime() + ' ' + entry.printValue(); + }).join('\n'); + } + }); + var MAX_MESSAGES = 1000; var PHONE_REGEX = /\+\d{7,12}(\d{3})/g; - var debugLog = []; + var log = new DebugLog(); if (window.console) { console._log = console.log; console.log = function(){ console._log.apply(this, arguments); - if (debugLog.length > MAX_MESSAGES) { - debugLog.shift(); - } var args = Array.prototype.slice.call(arguments); var str = args.join(' ').replace(PHONE_REGEX, "+[REDACTED]$1"); - debugLog.push(str); + log.log(str); }; console.get = function() { - return window.navigator.userAgent + '\n' + debugLog.join('\n'); + return window.navigator.userAgent + + ' Signal-Desktop/' + chrome.runtime.getManifest().version + + '\n' + log.print(); }; console.post = function(log) { if (log === undefined) {