2021-07-01 14:31:20 -07:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-20 18:41:51 -07:00
|
|
|
import type * as channels from '@protocol/channels';
|
2021-07-01 14:31:20 -07:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import './consoleTab.css';
|
2021-07-01 20:46:56 -07:00
|
|
|
import * as modelUtil from './modelUtil';
|
2023-07-10 12:56:56 -07:00
|
|
|
import { ListView } from '@web/components/listView';
|
2023-07-10 18:36:28 -07:00
|
|
|
import { ansi2htmlMarkup } from '@web/components/errorMessage';
|
2023-08-16 16:30:17 -07:00
|
|
|
import type { Boundaries } from '../geometry';
|
2023-08-19 16:13:42 -07:00
|
|
|
import { msToString } from '@web/uiUtils';
|
|
|
|
|
import type * as trace from '@trace/trace';
|
2023-07-10 12:56:56 -07:00
|
|
|
|
|
|
|
|
type ConsoleEntry = {
|
2023-08-19 16:13:42 -07:00
|
|
|
message?: trace.ConsoleMessageTraceEvent['initializer'];
|
2023-07-10 12:56:56 -07:00
|
|
|
error?: channels.SerializedError;
|
2023-07-10 18:36:28 -07:00
|
|
|
nodeMessage?: {
|
|
|
|
|
text?: string;
|
|
|
|
|
base64?: string;
|
|
|
|
|
isError: boolean;
|
|
|
|
|
},
|
|
|
|
|
timestamp: number;
|
2023-07-10 12:56:56 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ConsoleListView = ListView<ConsoleEntry>;
|
2021-07-01 14:31:20 -07:00
|
|
|
|
|
|
|
|
export const ConsoleTab: React.FunctionComponent<{
|
2023-07-10 12:56:56 -07:00
|
|
|
model: modelUtil.MultiTraceModel | undefined,
|
2023-08-19 16:13:42 -07:00
|
|
|
boundaries: Boundaries,
|
2023-08-16 16:30:17 -07:00
|
|
|
selectedTime: Boundaries | undefined,
|
2023-08-19 16:13:42 -07:00
|
|
|
}> = ({ model, boundaries, selectedTime }) => {
|
2023-07-10 12:56:56 -07:00
|
|
|
const { entries } = React.useMemo(() => {
|
|
|
|
|
if (!model)
|
|
|
|
|
return { entries: [] };
|
|
|
|
|
const entries: ConsoleEntry[] = [];
|
|
|
|
|
for (const event of model.events) {
|
2023-02-27 15:29:20 -08:00
|
|
|
if (event.method !== 'console' && event.method !== 'pageError')
|
2021-07-01 20:46:56 -07:00
|
|
|
continue;
|
2023-02-27 15:29:20 -08:00
|
|
|
if (event.method === 'console') {
|
|
|
|
|
const { guid } = event.params.message;
|
2023-07-10 12:56:56 -07:00
|
|
|
entries.push({
|
|
|
|
|
message: modelUtil.context(event).initializers[guid],
|
2023-07-10 18:36:28 -07:00
|
|
|
timestamp: event.time,
|
2023-07-10 12:56:56 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (event.method === 'pageError') {
|
|
|
|
|
entries.push({
|
|
|
|
|
error: event.params.error,
|
2023-07-10 18:36:28 -07:00
|
|
|
timestamp: event.time,
|
2023-07-10 12:56:56 -07:00
|
|
|
});
|
2021-07-01 14:31:20 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-10 18:36:28 -07:00
|
|
|
for (const event of model.stdio) {
|
|
|
|
|
entries.push({
|
|
|
|
|
nodeMessage: {
|
|
|
|
|
text: event.text,
|
|
|
|
|
base64: event.base64,
|
|
|
|
|
isError: event.type === 'stderr',
|
|
|
|
|
},
|
|
|
|
|
timestamp: event.timestamp,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
entries.sort((a, b) => a.timestamp - b.timestamp);
|
2023-07-10 12:56:56 -07:00
|
|
|
return { entries };
|
2023-08-16 16:30:17 -07:00
|
|
|
}, [model]);
|
|
|
|
|
|
|
|
|
|
const filteredEntries = React.useMemo(() => {
|
|
|
|
|
if (!selectedTime)
|
|
|
|
|
return entries;
|
|
|
|
|
return entries.filter(entry => entry.timestamp >= selectedTime.minimum && entry.timestamp <= selectedTime.maximum);
|
|
|
|
|
}, [entries, selectedTime]);
|
2021-07-01 14:31:20 -07:00
|
|
|
|
2023-07-10 12:56:56 -07:00
|
|
|
return <div className='console-tab'>
|
|
|
|
|
<ConsoleListView
|
2023-08-16 16:30:17 -07:00
|
|
|
items={filteredEntries}
|
2023-07-10 18:36:28 -07:00
|
|
|
isError={entry => !!entry.error || entry.message?.type === 'error' || entry.nodeMessage?.isError || false}
|
2023-07-10 12:56:56 -07:00
|
|
|
isWarning={entry => entry.message?.type === 'warning'}
|
|
|
|
|
render={entry => {
|
2023-07-10 18:36:28 -07:00
|
|
|
const { message, error, nodeMessage } = entry;
|
2023-08-19 16:13:42 -07:00
|
|
|
const timestamp = msToString(entry.timestamp - boundaries.minimum);
|
2023-07-10 12:56:56 -07:00
|
|
|
if (message) {
|
2023-08-19 16:13:42 -07:00
|
|
|
const text = message.args ? format(message.args) : message.text;
|
2023-07-10 12:56:56 -07:00
|
|
|
const url = message.location.url;
|
|
|
|
|
const filename = url ? url.substring(url.lastIndexOf('/') + 1) : '<anonymous>';
|
|
|
|
|
return <div className='console-line'>
|
2023-08-19 16:13:42 -07:00
|
|
|
<span className='console-time'>{timestamp}</span>
|
2023-07-10 12:56:56 -07:00
|
|
|
<span className='console-location'>{filename}:{message.location.lineNumber}</span>
|
|
|
|
|
<span className={'codicon codicon-' + iconClass(message)}></span>
|
2023-08-19 16:13:42 -07:00
|
|
|
<span className='console-line-message'>{text}</span>
|
2021-07-01 14:31:20 -07:00
|
|
|
</div>;
|
|
|
|
|
}
|
2023-07-10 12:56:56 -07:00
|
|
|
if (error) {
|
|
|
|
|
const { error: errorObject, value } = error;
|
|
|
|
|
if (errorObject) {
|
|
|
|
|
return <div className='console-line'>
|
2023-08-19 16:13:42 -07:00
|
|
|
<span className='console-time'>{timestamp}</span>
|
2023-07-10 12:56:56 -07:00
|
|
|
<span className={'codicon codicon-error'}></span>
|
|
|
|
|
<span className='console-line-message'>{errorObject.message}</span>
|
|
|
|
|
<div className='console-stack'>{errorObject.stack}</div>
|
|
|
|
|
</div>;
|
|
|
|
|
}
|
2023-07-10 18:36:28 -07:00
|
|
|
return <div className='console-line'>
|
2023-08-19 16:13:42 -07:00
|
|
|
<span className='console-time'>{timestamp}</span>
|
2023-07-10 18:36:28 -07:00
|
|
|
<span className={'codicon codicon-error'}></span>
|
|
|
|
|
<span className='console-line-message'>{String(value)}</span>
|
|
|
|
|
</div>;
|
|
|
|
|
}
|
|
|
|
|
if (nodeMessage?.text) {
|
|
|
|
|
return <div className='console-line'>
|
2023-08-19 16:13:42 -07:00
|
|
|
<span className='console-time'>{timestamp}</span>
|
2023-07-10 18:36:28 -07:00
|
|
|
<span className={'codicon codicon-' + stdioClass(nodeMessage.isError)}></span>
|
|
|
|
|
<span className='console-line-message' dangerouslySetInnerHTML={{ __html: ansi2htmlMarkup(nodeMessage.text.trim()) || '' }}></span>
|
|
|
|
|
</div>;
|
|
|
|
|
}
|
|
|
|
|
if (nodeMessage?.base64) {
|
|
|
|
|
return <div className='console-line'>
|
|
|
|
|
<span className={'codicon codicon-' + stdioClass(nodeMessage.isError)}></span>
|
|
|
|
|
<span className='console-line-message' dangerouslySetInnerHTML={{ __html: ansi2htmlMarkup(atob(nodeMessage.base64).trim()) || '' }}></span>
|
|
|
|
|
</div>;
|
2023-07-10 12:56:56 -07:00
|
|
|
}
|
|
|
|
|
return null;
|
2023-08-16 16:30:17 -07:00
|
|
|
}}
|
2023-07-10 12:56:56 -07:00
|
|
|
/>
|
|
|
|
|
</div>;
|
2021-07-01 14:31:20 -07:00
|
|
|
};
|
|
|
|
|
|
2023-08-19 16:13:42 -07:00
|
|
|
function iconClass(message: trace.ConsoleMessageTraceEvent['initializer']): string {
|
2021-07-01 14:31:20 -07:00
|
|
|
switch (message.type) {
|
|
|
|
|
case 'error': return 'error';
|
|
|
|
|
case 'warning': return 'warning';
|
|
|
|
|
}
|
|
|
|
|
return 'blank';
|
|
|
|
|
}
|
2023-07-10 18:36:28 -07:00
|
|
|
|
|
|
|
|
function stdioClass(isError: boolean): string {
|
|
|
|
|
return isError ? 'error' : 'blank';
|
|
|
|
|
}
|
2023-08-19 16:13:42 -07:00
|
|
|
|
|
|
|
|
function format(args: { preview: string, value: any }[]): JSX.Element[] {
|
|
|
|
|
if (args.length === 1)
|
|
|
|
|
return [<span>{args[0].preview}</span>];
|
|
|
|
|
const hasMessageFormat = typeof args[0].value === 'string' && args[0].value.includes('%');
|
|
|
|
|
const messageFormat = hasMessageFormat ? args[0].value as string : '';
|
|
|
|
|
const tail = hasMessageFormat ? args.slice(1) : args;
|
|
|
|
|
let argIndex = 0;
|
|
|
|
|
|
|
|
|
|
const regex = /%([%sdifoOc])/g;
|
|
|
|
|
let match;
|
|
|
|
|
const formatted: JSX.Element[] = [];
|
|
|
|
|
let tokens: JSX.Element[] = [];
|
|
|
|
|
formatted.push(<span>{tokens}</span>);
|
|
|
|
|
let formatIndex = 0;
|
|
|
|
|
while ((match = regex.exec(messageFormat)) !== null) {
|
|
|
|
|
const text = messageFormat.substring(formatIndex, match.index);
|
|
|
|
|
tokens.push(<span>{text}</span>);
|
|
|
|
|
formatIndex = match.index + 2;
|
|
|
|
|
const specifier = match[0][1];
|
|
|
|
|
if (specifier === '%') {
|
|
|
|
|
tokens.push(<span>%</span>);
|
|
|
|
|
} else if (specifier === 's' || specifier === 'o' || specifier === 'O' || specifier === 'd' || specifier === 'i' || specifier === 'f') {
|
|
|
|
|
const value = tail[argIndex++];
|
|
|
|
|
const styleObject: any = {};
|
|
|
|
|
if (typeof value?.value !== 'string')
|
|
|
|
|
styleObject['color'] = 'var(--vscode-debugTokenExpression-number)';
|
|
|
|
|
tokens.push(<span style={styleObject}>{value?.preview || ''}</span>);
|
|
|
|
|
} else if (specifier === 'c') {
|
|
|
|
|
tokens = [];
|
|
|
|
|
const format = tail[argIndex++];
|
|
|
|
|
const styleObject = format ? parseCSSStyle(format.preview) : {};
|
|
|
|
|
formatted.push(<span style={styleObject}>{tokens}</span>);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (formatIndex < messageFormat.length)
|
|
|
|
|
tokens.push(<span>{messageFormat.substring(formatIndex)}</span>);
|
|
|
|
|
for (; argIndex < tail.length; argIndex++) {
|
|
|
|
|
const value = tail[argIndex];
|
|
|
|
|
const styleObject: any = {};
|
|
|
|
|
if (tokens.length)
|
|
|
|
|
tokens.push(<span> </span>);
|
|
|
|
|
if (typeof value?.value !== 'string')
|
|
|
|
|
styleObject['color'] = 'var(--vscode-debugTokenExpression-number)';
|
|
|
|
|
tokens.push(<span style={styleObject}>{value?.preview || ''}</span>);
|
|
|
|
|
}
|
|
|
|
|
return formatted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseCSSStyle(cssFormat: string): Record<string, string | number> {
|
|
|
|
|
try {
|
|
|
|
|
const styleObject: Record<string, string | number> = {};
|
2023-08-21 10:59:37 -07:00
|
|
|
const cssProperties = cssFormat.split(';');
|
|
|
|
|
for (const token of cssProperties) {
|
|
|
|
|
const property = token.trim();
|
|
|
|
|
if (!property)
|
|
|
|
|
continue;
|
|
|
|
|
let [key, value] = property.split(':');
|
|
|
|
|
key = key.trim();
|
|
|
|
|
value = value.trim();
|
2023-08-19 16:13:42 -07:00
|
|
|
if (!supportProperty(key))
|
|
|
|
|
continue;
|
|
|
|
|
// cssProperties are background-color, JSDom ones are backgroundColor
|
|
|
|
|
const cssKey = key.replace(/-([a-z])/g, g => g[1].toUpperCase());
|
|
|
|
|
styleObject[cssKey] = value;
|
|
|
|
|
}
|
|
|
|
|
return styleObject;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function supportProperty(cssKey: string): boolean {
|
|
|
|
|
const prefixes = ['background', 'border', 'color', 'font', 'line', 'margin', 'padding', 'text'];
|
|
|
|
|
return prefixes.some(p => cssKey.startsWith(p));
|
|
|
|
|
}
|