Allow downloading logs (#165)

This commit is contained in:
Paulus Schoutsen 2022-01-23 21:03:09 -08:00 committed by GitHub
parent f1fdfd5e25
commit 99606d75fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 0 deletions

View File

@ -11,6 +11,10 @@ export class EwtConsole extends HTMLElement {
private _console?: ColoredConsole; private _console?: ColoredConsole;
private _cancelConnection?: () => Promise<void>; private _cancelConnection?: () => Promise<void>;
public logs(): string {
return this._console?.logs() || "";
}
public connectedCallback() { public connectedCallback() {
if (this._console) { if (this._console) {
return; return;

View File

@ -19,6 +19,7 @@ import {
PortNotReady, PortNotReady,
} from "improv-wifi-serial-sdk/dist/const"; } from "improv-wifi-serial-sdk/dist/const";
import { flash } from "./flash"; import { flash } from "./flash";
import { textDownload } from "./util/file-download";
import { fireEvent } from "./util/fire-event"; import { fireEvent } from "./util/fire-event";
import { sleep } from "./util/sleep"; import { sleep } from "./util/sleep";
import { downloadManifest } from "./util/manifest"; import { downloadManifest } from "./util/manifest";
@ -587,6 +588,18 @@ class EwtInstallDialog extends LitElement {
this._initialize(); this._initialize();
}} }}
></ewt-button> ></ewt-button>
<ewt-button
slot="secondaryAction"
label="Download Logs"
@click=${() => {
textDownload(
this.shadowRoot!.querySelector("ewt-console")!.logs(),
`esp-web-tools-logs.txt`
);
this.shadowRoot!.querySelector("ewt-console")!.reset();
}}
></ewt-button>
<ewt-button <ewt-button
slot="secondaryAction" slot="secondaryAction"
label="Reset Device" label="Reset Device"

View File

@ -23,6 +23,10 @@ export class ColoredConsole {
constructor(public targetElement: HTMLElement) {} constructor(public targetElement: HTMLElement) {}
logs(): string {
return this.targetElement.innerText;
}
addLine(line: string) { addLine(line: string) {
const re = /(?:\033|\\033)(?:\[(.*?)[@-~]|\].*?(?:\007|\033\\))/g; const re = /(?:\033|\\033)(?:\[(.*?)[@-~]|\].*?(?:\007|\033\\))/g;
let i = 0; let i = 0;

17
src/util/file-download.ts Normal file
View File

@ -0,0 +1,17 @@
export const fileDownload = (href: string, filename = ""): void => {
const a = document.createElement("a");
a.target = "_blank";
a.href = href;
a.download = filename;
document.body.appendChild(a);
a.dispatchEvent(new MouseEvent("click"));
document.body.removeChild(a);
};
export const textDownload = (text: string, filename = ""): void => {
const blob = new Blob([text], { type: "text/plain" });
const url = URL.createObjectURL(blob);
fileDownload(url, filename);
setTimeout(() => URL.revokeObjectURL(url), 0);
};