debugLog.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * vim: ts=4:sw=4:expandtab
  3. */
  4. (function () {
  5. 'use strict';
  6. var LogEntry = Backbone.Model.extend({
  7. database: Whisper.Database,
  8. storeName: 'debug',
  9. printTime: function() {
  10. try {
  11. return new Date(this.get('time')).toISOString();
  12. } catch(e) {
  13. return '';
  14. }
  15. },
  16. printValue: function() {
  17. return this.get('value') || '';
  18. }
  19. });
  20. var DebugLog = Backbone.Collection.extend({
  21. database: Whisper.Database,
  22. storeName: 'debug',
  23. model: LogEntry,
  24. comparator: 'time',
  25. initialize: function() {
  26. this.fetch({remove: false});
  27. },
  28. log: function(str) {
  29. this.add({time: Date.now(), value: str}).save();
  30. while (this.length > MAX_MESSAGES) {
  31. this.at(0).destroy();
  32. }
  33. },
  34. print: function() {
  35. return this.map(function(entry) {
  36. return entry.printTime() + ' ' + entry.printValue();
  37. }).join('\n');
  38. }
  39. });
  40. var MAX_MESSAGES = 1000;
  41. var PHONE_REGEX = /\+\d{7,12}(\d{3})/g;
  42. var log = new DebugLog();
  43. if (window.console) {
  44. console._log = console.log;
  45. console.log = function() {
  46. console._log.apply(this, arguments);
  47. var args = Array.prototype.slice.call(arguments);
  48. var str = args.join(' ').replace(PHONE_REGEX, "+[REDACTED]$1");
  49. log.log(str);
  50. };
  51. console.get = function() {
  52. return window.navigator.userAgent +
  53. ' Signal-Desktop/' + chrome.runtime.getManifest().version +
  54. '\n' + log.print();
  55. };
  56. console.post = function(log) {
  57. if (log === undefined) {
  58. log = console.get();
  59. }
  60. return new Promise(function(resolve) {
  61. $.post('https://api.github.com/gists', textsecure.utils.jsonThing({
  62. "files": { "debugLog.txt": { "content": log } }
  63. })).then(function(response) {
  64. console._log('Posted debug log to ', response.html_url);
  65. resolve(response.html_url);
  66. }).fail(resolve);
  67. });
  68. };
  69. window.onerror = function(message, script, line, col, error) {
  70. console.log(error);
  71. };
  72. }
  73. })();