2021-01-06 17:23:15 +00:00
|
|
|
// Copyright 2015-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-03-05 22:57:23 +00:00
|
|
|
/* global i18n: false */
|
|
|
|
/* global Whisper: false */
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2020-11-18 15:15:42 +00:00
|
|
|
(function () {
|
2018-03-05 22:54:33 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2017-10-04 21:40:35 +00:00
|
|
|
|
2021-01-14 18:01:48 +00:00
|
|
|
// This enum-like object describes the load state of `DebugLogView`. It's designed to be
|
|
|
|
// unidirectional; `NotStarted` → `Started` → `LogsFetchedButNotInTextarea`, etc.
|
|
|
|
const LoadState = {
|
|
|
|
NotStarted: 0,
|
|
|
|
Started: 1,
|
|
|
|
LogsFetchedButNotInTextarea: 2,
|
|
|
|
PuttingLogsInTextarea: 3,
|
|
|
|
LogsInTextarea: 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
Whisper.LoadingFullLogsToast = Whisper.ToastView.extend({
|
|
|
|
render_attributes() {
|
|
|
|
return { toastMessage: i18n('loading') };
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-05-04 19:20:01 +00:00
|
|
|
Whisper.LinkedCopiedToast = Whisper.ToastView.extend({
|
|
|
|
render_attributes() {
|
|
|
|
return { toastMessage: i18n('debugLogLinkCopied') };
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-03-05 22:54:33 +00:00
|
|
|
Whisper.DebugLogLinkView = Whisper.View.extend({
|
|
|
|
templateName: 'debug-log-link',
|
|
|
|
initialize(options) {
|
|
|
|
this.url = options.url;
|
|
|
|
},
|
2020-05-04 19:20:01 +00:00
|
|
|
events: {
|
|
|
|
'click .copy': 'copy',
|
|
|
|
},
|
2018-03-05 22:54:33 +00:00
|
|
|
render_attributes() {
|
|
|
|
return {
|
|
|
|
url: this.url,
|
|
|
|
reportIssue: i18n('reportIssue'),
|
2020-05-04 19:20:01 +00:00
|
|
|
debugLogCopy: i18n('debugLogCopy'),
|
|
|
|
debugLogCopyAlt: i18n('debugLogCopyAlt'),
|
2018-03-05 22:54:33 +00:00
|
|
|
};
|
|
|
|
},
|
2020-05-04 19:20:01 +00:00
|
|
|
copy(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
window.copyText(e.currentTarget.href);
|
|
|
|
Whisper.ToastView.show(Whisper.LinkedCopiedToast, document.body);
|
|
|
|
},
|
2018-03-05 22:54:33 +00:00
|
|
|
});
|
2021-01-14 18:01:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The bulk of the logic in this view involves grabbing the logs from disk and putting
|
|
|
|
* them in a `<textarea>`. The first part isn't instant but is reasonably fast; setting
|
|
|
|
* the textarea's `value` takes a long time.
|
|
|
|
*
|
|
|
|
* After loading the logs into memory, we only put a small number of lines into the
|
|
|
|
* textarea. If the user clicks or scrolls the textarea, we pull the full logs, which
|
|
|
|
* can cause the system to lock up for a bit.
|
|
|
|
*
|
|
|
|
* Ideally, we'd only show a sampling of the logs and allow the user to download and
|
|
|
|
* edit them in their own editor. This is mostly a stopgap solution.
|
|
|
|
*/
|
2018-03-05 22:54:33 +00:00
|
|
|
Whisper.DebugLogView = Whisper.View.extend({
|
|
|
|
templateName: 'debug-log',
|
|
|
|
className: 'debug-log modal',
|
|
|
|
initialize() {
|
|
|
|
this.render();
|
2015-09-16 06:28:00 +00:00
|
|
|
|
2021-01-14 18:01:48 +00:00
|
|
|
this.textarea = this.$('.textarea').get(0);
|
|
|
|
if (!this.textarea) {
|
|
|
|
throw new Error('textarea not found');
|
|
|
|
}
|
|
|
|
this.textarea.setAttribute('readonly', '');
|
|
|
|
|
|
|
|
this.loadState = LoadState.NotStarted;
|
|
|
|
this.putFullLogsInTextareaPlease = false;
|
|
|
|
|
|
|
|
this.fetchLogs();
|
2018-03-05 22:54:33 +00:00
|
|
|
},
|
|
|
|
events: {
|
2021-01-14 18:01:48 +00:00
|
|
|
'click .textarea': 'putFullLogsInTextarea',
|
|
|
|
'scroll .textarea': 'putFullLogsInTextarea',
|
|
|
|
'wheel .textarea': 'putFullLogsInTextarea',
|
2018-03-05 22:54:33 +00:00
|
|
|
'click .submit': 'submit',
|
|
|
|
'click .close': 'close',
|
|
|
|
},
|
|
|
|
render_attributes: {
|
|
|
|
title: i18n('submitDebugLog'),
|
|
|
|
cancel: i18n('cancel'),
|
|
|
|
submit: i18n('submit'),
|
|
|
|
close: i18n('gotIt'),
|
|
|
|
debugLogExplanation: i18n('debugLogExplanation'),
|
|
|
|
},
|
2021-01-14 18:01:48 +00:00
|
|
|
async fetchLogs() {
|
|
|
|
if (this.loadState !== LoadState.NotStarted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loadState = LoadState.Started;
|
|
|
|
this.textarea.value = i18n('loading');
|
|
|
|
this.$('.submit').attr('disabled', 'disabled');
|
|
|
|
|
|
|
|
this.logText = await window.log.fetch();
|
|
|
|
this.loadState = LoadState.LogsFetchedButNotInTextarea;
|
|
|
|
|
|
|
|
// This number is somewhat arbitrary; we want to show enough that it's clear that
|
|
|
|
// we need to scroll, but not so many that things get slow.
|
|
|
|
const linesToShow = Math.ceil(Math.min(window.innerHeight, 2000) / 5);
|
|
|
|
this.textarea.value = this.logText
|
|
|
|
.split(/\n/g)
|
|
|
|
.slice(0, linesToShow)
|
|
|
|
.concat(['', i18n('loading')])
|
|
|
|
.join('\n');
|
|
|
|
|
|
|
|
this.$('.submit').removeAttr('disabled');
|
|
|
|
|
|
|
|
if (this.putFullLogsInTextareaPlease) {
|
|
|
|
this.putFullLogsInTextarea();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
putFullLogsInTextarea() {
|
|
|
|
switch (this.loadState) {
|
|
|
|
case LoadState.NotStarted:
|
|
|
|
case LoadState.Started:
|
|
|
|
this.putFullLogsInTextareaPlease = true;
|
|
|
|
break;
|
|
|
|
case LoadState.LogsInTextarea:
|
|
|
|
case LoadState.PuttingLogsInTextarea:
|
|
|
|
break;
|
|
|
|
case LoadState.LogsFetchedButNotInTextarea:
|
|
|
|
if (!this.logText) {
|
|
|
|
throw new Error('Expected log text to be present');
|
|
|
|
}
|
|
|
|
this.loadState = LoadState.PuttingLogsInTextarea;
|
|
|
|
Whisper.ToastView.show(Whisper.LoadingFullLogsToast, document.body);
|
|
|
|
setTimeout(() => {
|
|
|
|
this.textarea.value = this.logText;
|
|
|
|
this.textarea.removeAttribute('readonly');
|
|
|
|
this.loadState = LoadState.LogsInTextarea;
|
|
|
|
}, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// When we can, we should make this throw a `missingCaseError`.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2018-07-03 22:33:50 +00:00
|
|
|
close() {
|
|
|
|
window.closeDebugLog();
|
2018-03-05 22:54:33 +00:00
|
|
|
},
|
2018-03-05 23:23:59 +00:00
|
|
|
async submit(e) {
|
2018-03-05 22:54:33 +00:00
|
|
|
e.preventDefault();
|
2021-01-14 18:01:48 +00:00
|
|
|
|
|
|
|
let text;
|
|
|
|
switch (this.loadState) {
|
|
|
|
case LoadState.NotStarted:
|
|
|
|
case LoadState.Started:
|
|
|
|
return;
|
|
|
|
case LoadState.LogsFetchedButNotInTextarea:
|
|
|
|
text = this.logText;
|
|
|
|
break;
|
|
|
|
case LoadState.LogsInTextarea:
|
|
|
|
text = this.textarea.value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// When we can, we should make this throw a `missingCaseError`.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:54:33 +00:00
|
|
|
if (text.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-05 23:23:59 +00:00
|
|
|
|
2021-01-14 18:01:48 +00:00
|
|
|
this.$('.buttons, .textarea').remove();
|
2018-03-05 22:54:33 +00:00
|
|
|
this.$('.result').addClass('loading');
|
2018-03-05 23:23:59 +00:00
|
|
|
|
2018-07-19 17:21:53 +00:00
|
|
|
try {
|
2021-01-06 17:23:15 +00:00
|
|
|
const publishedLogURL = await window.log.publish(
|
|
|
|
text,
|
|
|
|
window.getVersion()
|
|
|
|
);
|
2018-07-19 17:21:53 +00:00
|
|
|
const view = new Whisper.DebugLogLinkView({
|
|
|
|
url: publishedLogURL,
|
|
|
|
el: this.$('.result'),
|
|
|
|
});
|
|
|
|
this.$('.loading').removeClass('loading');
|
|
|
|
view.render();
|
2020-11-18 15:15:42 +00:00
|
|
|
this.$('.link').focus().select();
|
2018-07-19 17:21:53 +00:00
|
|
|
} catch (error) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.error(
|
2018-07-19 17:21:53 +00:00
|
|
|
'DebugLogView error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
this.$('.loading').removeClass('loading');
|
|
|
|
this.$('.result').text(i18n('debugLogError'));
|
|
|
|
}
|
2018-03-05 22:54:33 +00:00
|
|
|
},
|
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
})();
|