Make debug log persistent

Save log entries in indexedDB rather than just in memory. Reload them
whenever the background page is refreshed.

// FREEBIE
This commit is contained in:
lilia 2016-02-02 17:55:27 -08:00
parent 86132a38a8
commit 87ddcef333
4 changed files with 50 additions and 7 deletions

View file

@ -311,9 +311,9 @@
<div><button class='close'>{{ close }}</button></div> <div><button class='close'>{{ close }}</button></div>
</div> </div>
</script> </script>
<script type='text/javascript' src='js/debugLog.js'></script>
<script type='text/javascript' src='js/components.js'></script> <script type='text/javascript' src='js/components.js'></script>
<script type='text/javascript' src='js/database.js'></script> <script type='text/javascript' src='js/database.js'></script>
<script type='text/javascript' src='js/debugLog.js'></script>
<script type='text/javascript' src='js/storage.js'></script> <script type='text/javascript' src='js/storage.js'></script>
<script type='text/javascript' src='js/axolotl_store.js'></script> <script type='text/javascript' src='js/axolotl_store.js'></script>
<script type='text/javascript' src='js/libtextsecure.js'></script> <script type='text/javascript' src='js/libtextsecure.js'></script>

View file

@ -4,6 +4,7 @@
;(function() { ;(function() {
'use strict'; 'use strict';
console.log('background page reloaded');
// register some chrome listeners // register some chrome listeners
if (chrome.notifications) { if (chrome.notifications) {
chrome.notifications.onClicked.addListener(function() { chrome.notifications.onClicked.addListener(function() {

View file

@ -100,6 +100,12 @@
}); });
storage.fetch(); storage.fetch();
} }
},
{
version: "7.0",
migrate: function(transaction, next) {
transaction.db.createObjectStore("debug");
}
} }
]; ];
}()); }());

View file

@ -3,22 +3,58 @@
*/ */
(function () { (function () {
'use strict'; '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 MAX_MESSAGES = 1000;
var PHONE_REGEX = /\+\d{7,12}(\d{3})/g; var PHONE_REGEX = /\+\d{7,12}(\d{3})/g;
var debugLog = []; var log = new DebugLog();
if (window.console) { if (window.console) {
console._log = console.log; console._log = console.log;
console.log = function(){ console.log = function(){
console._log.apply(this, arguments); console._log.apply(this, arguments);
if (debugLog.length > MAX_MESSAGES) {
debugLog.shift();
}
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
var str = args.join(' ').replace(PHONE_REGEX, "+[REDACTED]$1"); var str = args.join(' ').replace(PHONE_REGEX, "+[REDACTED]$1");
debugLog.push(str); log.log(str);
}; };
console.get = function() { 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) { console.post = function(log) {
if (log === undefined) { if (log === undefined) {