|
| 1 | +/* eslint-disable code-import-patterns */ |
| 2 | +/* eslint-disable header/header */ |
| 3 | +/*--------------------------------------------------------------------------------------------- |
| 4 | + * Copyright (c) Gitpod. All rights reserved. |
| 5 | + *--------------------------------------------------------------------------------------------*/ |
| 6 | + |
| 7 | +import { IBrowserWorkbenchEnvironmentService } from 'vs/workbench/services/environment/browser/environmentService'; |
| 8 | +import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService'; |
| 9 | +import { IFileService } from 'vs/platform/files/common/files'; |
| 10 | +import { Action2, MenuId, registerAction2 } from 'vs/platform/actions/common/actions'; |
| 11 | +import { CATEGORIES } from 'vs/workbench/common/actions'; |
| 12 | +import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; |
| 13 | +import { URI } from 'vs/base/common/uri'; |
| 14 | +import * as resources from 'vs/base/common/resources'; |
| 15 | +import { IProgressService, ProgressLocation } from 'vs/platform/progress/common/progress'; |
| 16 | +import { CancellationTokenSource } from 'vs/base/common/cancellation'; |
| 17 | +import type * as zipModule from '@zip.js/zip.js'; |
| 18 | +import { Schemas } from 'vs/base/common/network'; |
| 19 | +import { triggerDownload } from 'vs/base/browser/dom'; |
| 20 | + |
| 21 | +const getZipModule = (function () { |
| 22 | + let zip: typeof zipModule; |
| 23 | + return async () => { |
| 24 | + if (!zip) { |
| 25 | + // when actually importing the module change `zip.js` to `zipjs` |
| 26 | + // without the dot because loader.js will do a check for `.js` extension |
| 27 | + // and it won't resolve the module path correctly |
| 28 | + // @ts-ignore |
| 29 | + zip = await import('@zip.js/zipjs'); |
| 30 | + zip.configure({ |
| 31 | + useWebWorkers: false |
| 32 | + }); |
| 33 | + } |
| 34 | + return zip; |
| 35 | + }; |
| 36 | +})(); |
| 37 | + |
| 38 | +registerAction2(class ExportLogsAction extends Action2 { |
| 39 | + constructor() { |
| 40 | + super({ |
| 41 | + id: 'gitpod.workbench.exportLogs', |
| 42 | + title: { original: 'Export all logs', value: 'Export all logs' }, |
| 43 | + category: CATEGORIES.Developer, |
| 44 | + menu: { |
| 45 | + id: MenuId.CommandPalette |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + async run(accessor: ServicesAccessor): Promise<void> { |
| 51 | + const progressService = accessor.get(IProgressService); |
| 52 | + const fileService = accessor.get(IFileService); |
| 53 | + const environmentService = accessor.get(IBrowserWorkbenchEnvironmentService); |
| 54 | + const remoteAgentService = accessor.get(IRemoteAgentService); |
| 55 | + |
| 56 | + const cts = new CancellationTokenSource(); |
| 57 | + const bufferData = await progressService.withProgress<Uint8Array | undefined>( |
| 58 | + { |
| 59 | + location: ProgressLocation.Dialog, |
| 60 | + title: 'Exporting logs to zip file ...', |
| 61 | + cancellable: true, |
| 62 | + delay: 1000 |
| 63 | + }, |
| 64 | + async progress => { |
| 65 | + const token = cts.token; |
| 66 | + |
| 67 | + const zip = await getZipModule(); |
| 68 | + const uint8ArrayWriter = new zip.Uint8ArrayWriter(); |
| 69 | + const writer = new zip.ZipWriter(uint8ArrayWriter); |
| 70 | + |
| 71 | + if (token.isCancellationRequested) { |
| 72 | + return undefined; |
| 73 | + } |
| 74 | + |
| 75 | + const entries: { name: string; resource: URI }[] = []; |
| 76 | + const logsPath = URI.file(environmentService.logsPath).with({ scheme: environmentService.logFile.scheme }); |
| 77 | + const stat = await fileService.resolve(logsPath); |
| 78 | + if (stat.children) { |
| 79 | + entries.push(...stat.children.filter(stat => !stat.isDirectory).map(stat => ({ name: resources.basename(stat.resource), resource: stat.resource }))); |
| 80 | + } |
| 81 | + |
| 82 | + if (token.isCancellationRequested) { |
| 83 | + return undefined; |
| 84 | + } |
| 85 | + |
| 86 | + const remoteEnv = await remoteAgentService.getEnvironment(); |
| 87 | + const remoteLogsPath = remoteEnv?.logsPath; |
| 88 | + if (remoteLogsPath) { |
| 89 | + const remoteAgentLogFile = resources.joinPath(remoteLogsPath, 'remoteagent.log'); |
| 90 | + if (await fileService.exists(remoteAgentLogFile)) { |
| 91 | + entries.push({ name: resources.basename(remoteAgentLogFile), resource: remoteAgentLogFile }); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (token.isCancellationRequested) { |
| 96 | + return undefined; |
| 97 | + } |
| 98 | + |
| 99 | + const remoteExtHostLogsPath = remoteEnv?.extensionHostLogsPath; |
| 100 | + if (remoteExtHostLogsPath) { |
| 101 | + const remoteExtHostLogsFile = resources.joinPath(remoteExtHostLogsPath, 'exthost.log'); |
| 102 | + if (await fileService.exists(remoteExtHostLogsFile)) { |
| 103 | + entries.push({ name: resources.basename(remoteExtHostLogsFile), resource: remoteExtHostLogsFile }); |
| 104 | + } |
| 105 | + |
| 106 | + let stat = await fileService.resolve(remoteExtHostLogsPath); |
| 107 | + if (stat.children) { |
| 108 | + const ouputLoggingDirs = stat.children.filter(stat => stat.isDirectory); |
| 109 | + for (const outLogDir of ouputLoggingDirs) { |
| 110 | + if (token.isCancellationRequested) { |
| 111 | + return undefined; |
| 112 | + } |
| 113 | + |
| 114 | + stat = await fileService.resolve(outLogDir.resource); |
| 115 | + if (stat.children) { |
| 116 | + entries.push(...stat.children.filter(stat => !stat.isDirectory).map(stat => ({ |
| 117 | + name: `${resources.basename(outLogDir.resource)}_${resources.basename(stat.resource)}`, |
| 118 | + resource: stat.resource |
| 119 | + }))); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (token.isCancellationRequested) { |
| 126 | + return undefined; |
| 127 | + } |
| 128 | + |
| 129 | + const credentialHelperPath = URI.file('/tmp/gitpod-git-credential-helper.log').with({ scheme: Schemas.vscodeRemote }); |
| 130 | + if (await fileService.exists(credentialHelperPath)) { |
| 131 | + entries.push({ name: resources.basename(credentialHelperPath), resource: credentialHelperPath }); |
| 132 | + } |
| 133 | + |
| 134 | + console.log('All log entries', entries); |
| 135 | + |
| 136 | + for (const entry of entries) { |
| 137 | + if (token.isCancellationRequested) { |
| 138 | + return undefined; |
| 139 | + } |
| 140 | + const content = await fileService.readFile(entry.resource, { atomic: true }, token); |
| 141 | + |
| 142 | + if (token.isCancellationRequested) { |
| 143 | + return undefined; |
| 144 | + } |
| 145 | + await writer.add(entry.name, new zip.Uint8ArrayReader(content.value.buffer)); |
| 146 | + } |
| 147 | + |
| 148 | + return writer.close(); |
| 149 | + }, |
| 150 | + () => cts.dispose(true) |
| 151 | + ); |
| 152 | + |
| 153 | + if (bufferData) { |
| 154 | + triggerDownload(bufferData, 'vscodeLogs.zip'); |
| 155 | + } |
| 156 | + } |
| 157 | +}); |
0 commit comments