Skip to content
This repository has been archived by the owner on Oct 2, 2021. It is now read-only.

Commit

Permalink
microsoft/vscode#47274 - remove logpoint indicator message from log e…
Browse files Browse the repository at this point in the history
…vent
  • Loading branch information
roblourens committed Apr 23, 2018
1 parent 423608d commit 2ba57c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/chrome/consoleHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ function resolveParams(m: Crdp.Runtime.ConsoleAPICalledEvent, skipFormatSpecifie
}
};

const displayArgs = isFromLogpoint(m) ? stripLogpointIndicatorArg(m.args) : m.args;

// Collapse all text parameters, formatting properly if there's a format specifier
for (let argIdx = 0; argIdx < m.args.length; argIdx++) {
const arg = m.args[argIdx];
for (let argIdx = 0; argIdx < displayArgs.length; argIdx++) {
const arg = displayArgs[argIdx];

const formatSpec = formatSpecifiers.shift();
const formatted = formatArg(formatSpec, arg);
Expand Down Expand Up @@ -171,7 +173,15 @@ function stackTraceToString(stackTrace: Crdp.Runtime.StackTrace): string {
.join('\n');
}

export const LOGPOINT_SPECIAL_MESSAGE = '$vscode_logpoint_expr$';
export const LOGPOINT_INDICATOR_MESSAGE = '$vscode_logpoint_expr$';
function isFromLogpoint(m: Crdp.Runtime.ConsoleAPICalledEvent): boolean {
return m.args.length && m.args[m.args.length - 1].type === 'string' && m.args[m.args.length - 1].value === LOGPOINT_INDICATOR_MESSAGE;
}

function stripLogpointIndicatorArg(args: Crdp.Runtime.RemoteObject[]): Crdp.Runtime.RemoteObject[] {
return args.slice(0, args.length - 1);
}

const LOGMESSAGE_VARIABLE_REGEXP = /{(.*?)}/g;

export function logpointExpressionToConsoleLog(msg: string): string {
Expand All @@ -189,14 +199,14 @@ export function logpointExpressionToConsoleLog(msg: string): string {
})
.replace('\'', '\\\'');

args.push(`'${LOGPOINT_SPECIAL_MESSAGE}'`);
args.push(`'${LOGPOINT_INDICATOR_MESSAGE}'`);

const argStr = args.join(', ');
return `console.log('${format}', ${argStr})`;
}

export function stacktraceWithoutLogpointFrame(m: Crdp.Runtime.ConsoleAPICalledEvent): Crdp.Runtime.StackTrace {
if (m.args.length && m.args[m.args.length - 1].type === 'string' && m.args[m.args.length - 1].value === LOGPOINT_SPECIAL_MESSAGE) {
if (isFromLogpoint(m)) {
return {
...m.stackTrace,
callFrames: m.stackTrace.callFrames.slice(1)
Expand Down
6 changes: 3 additions & 3 deletions test/chrome/consoleHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ suite('ConsoleHelper', () => {
test('simple', () => {
assert.equal(
ConsoleHelper.logpointExpressionToConsoleLog('hi'),
`console.log('hi', '${ConsoleHelper.LOGPOINT_SPECIAL_MESSAGE}')`);
`console.log('hi', '${ConsoleHelper.LOGPOINT_INDICATOR_MESSAGE}')`);
});

test('multiple args', () => {
assert.equal(
ConsoleHelper.logpointExpressionToConsoleLog('hi {test} foo {bar}'),
`console.log('hi %O foo %O', (test), (bar), '${ConsoleHelper.LOGPOINT_SPECIAL_MESSAGE}')`);
`console.log('hi %O foo %O', (test), (bar), '${ConsoleHelper.LOGPOINT_INDICATOR_MESSAGE}')`);
});

test('multiple expressions', () => {
assert.equal(
ConsoleHelper.logpointExpressionToConsoleLog('hi {test + 1} foo {bar(a, b)}'),
`console.log('hi %O foo %O', (test + 1), (bar(a, b)), '${ConsoleHelper.LOGPOINT_SPECIAL_MESSAGE}')`);
`console.log('hi %O foo %O', (test + 1), (bar(a, b)), '${ConsoleHelper.LOGPOINT_INDICATOR_MESSAGE}')`);
});
});
});
Expand Down

0 comments on commit 2ba57c9

Please sign in to comment.