Skip to content

Commit

Permalink
refactor: move logging callbacks into language server (#1003)
Browse files Browse the repository at this point in the history
### Summary of Changes

As a first step for #1002, register logging callbacks in the `Runner`
service instead of the extension.
  • Loading branch information
lars-reimann authored Apr 7, 2024
1 parent ca75a87 commit 568ee20
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 59 deletions.
55 changes: 54 additions & 1 deletion packages/safe-ds-lang/src/language/runner/safe-ds-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ProgramCodeMap,
PythonServerMessage,
RuntimeErrorBacktraceFrame,
RuntimeErrorMessage,
} from './messages.js';
import { BasicSourceMapConsumer, SourceMapConsumer } from 'source-map';
import treeKill from 'tree-kill';
Expand Down Expand Up @@ -56,8 +57,60 @@ export class SafeDsRunner {
outputInfo(_value: string) {},
displayError(_value: string) {},
};
this.registerMessageLoggingCallbacks();
}

/* c8 ignore start */
private registerMessageLoggingCallbacks() {
this.addMessageCallback((message) => {
this.logging.outputInfo(
`Placeholder value is (${message.id}): ${message.data.name} of type ${message.data.type} = ${message.data.value}`,
);
}, 'placeholder_value');
this.addMessageCallback((message) => {
this.logging.outputInfo(
`Placeholder was calculated (${message.id}): ${message.data.name} of type ${message.data.type}`,
);
const execInfo = this.getExecutionContext(message.id)!;
execInfo.calculatedPlaceholders.set(message.data.name, message.data.type);
// this.sendMessageToPythonServer(
// messages.createPlaceholderQueryMessage(message.id, message.data.name),
//);
}, 'placeholder_type');
this.addMessageCallback((message) => {
this.logging.outputInfo(`Runner-Progress (${message.id}): ${message.data}`);
}, 'runtime_progress');
this.addMessageCallback(async (message) => {
let readableStacktraceSafeDs: string[] = [];
const execInfo = this.getExecutionContext(message.id)!;
const readableStacktracePython = await Promise.all(
(<RuntimeErrorMessage>message).data.backtrace.map(async (frame) => {
const mappedFrame = await this.tryMapToSafeDSSource(message.id, frame);
if (mappedFrame) {
readableStacktraceSafeDs.push(
`\tat ${URI.file(execInfo.path)}#${mappedFrame.line} (${execInfo.path} line ${
mappedFrame.line
})`,
);
return `\tat ${frame.file} line ${frame.line} (mapped to '${mappedFrame.file}' line ${mappedFrame.line})`;
}
return `\tat ${frame.file} line ${frame.line}`;
}),
);
this.logging.outputError(
`Runner-RuntimeError (${message.id}): ${
(<RuntimeErrorMessage>message).data.message
} \n${readableStacktracePython.join('\n')}`,
);
this.logging.outputError(
`Safe-DS Error (${message.id}): ${(<RuntimeErrorMessage>message).data.message} \n${readableStacktraceSafeDs
.reverse()
.join('\n')}`,
);
}, 'runtime_error');
}
/* c8 ignore stop */

/**
* Change the command to start the runner process. This will not cause the runner process to restart, if it is already running.
*
Expand Down Expand Up @@ -244,7 +297,7 @@ export class SafeDsRunner {
* @param executionId Id that uniquely identifies the execution that produced this stack frame
* @param frame Stack frame from the python execution
*/
public async tryMapToSafeDSSource(
private async tryMapToSafeDSSource(
executionId: string,
frame: RuntimeErrorBacktraceFrame | undefined,
): Promise<RuntimeErrorBacktraceFrame | undefined> {
Expand Down
12 changes: 6 additions & 6 deletions packages/safe-ds-lang/src/language/safe-ds-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export type SafeDsAddedServices = {
purity: {
PurityComputer: SafeDsPurityComputer;
};
runtime: {
Runner: SafeDsRunner;
};
typing: {
ClassHierarchy: SafeDsClassHierarchy;
CoreTypes: SafeDsCoreTypes;
Expand All @@ -89,9 +92,6 @@ export type SafeDsAddedServices = {
PackageManager: SafeDsPackageManager;
SettingsProvider: SafeDsSettingsProvider;
};
runtime: {
Runner: SafeDsRunner;
};
};

/**
Expand Down Expand Up @@ -151,6 +151,9 @@ export const SafeDsModule: Module<SafeDsServices, PartialLangiumServices & SafeD
ScopeComputation: (services) => new SafeDsScopeComputation(services),
ScopeProvider: (services) => new SafeDsScopeProvider(services),
},
runtime: {
Runner: (services) => new SafeDsRunner(services),
},
typing: {
ClassHierarchy: (services) => new SafeDsClassHierarchy(services),
CoreTypes: (services) => new SafeDsCoreTypes(services),
Expand All @@ -162,9 +165,6 @@ export const SafeDsModule: Module<SafeDsServices, PartialLangiumServices & SafeD
PackageManager: (services) => new SafeDsPackageManager(services),
SettingsProvider: (services) => new SafeDsSettingsProvider(services),
},
runtime: {
Runner: (services) => new SafeDsRunner(services),
},
};

export type SafeDsSharedServices = LangiumSharedServices;
Expand Down
53 changes: 1 addition & 52 deletions packages/safe-ds-vscode/src/extension/mainClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ast, createSafeDsServices, getModuleMembers, messages, SafeDsServices }
import { NodeFileSystem } from 'langium/node';
import { getSafeDSOutputChannel, initializeLog, logError, logOutput, printOutputMessage } from './output.js';
import crypto from 'crypto';
import { LangiumDocument, URI, AstUtils, AstNode } from 'langium';
import { AstNode, AstUtils, LangiumDocument } from 'langium';
import { EDAPanel } from './eda/edaPanel.ts';
import { dumpDiagnostics } from './commands/dumpDiagnostics.js';
import { openDiagnosticsDumps } from './commands/openDiagnosticsDumps.js';
Expand Down Expand Up @@ -91,63 +91,12 @@ const startLanguageClient = function (context: vscode.ExtensionContext): Languag
};

const acceptRunRequests = async function (context: vscode.ExtensionContext) {
// Register logging message callbacks
registerMessageLoggingCallbacks();
// Register VS Code Entry Points
registerVSCodeCommands(context);
// Register watchers
registerVSCodeWatchers();
};

const registerMessageLoggingCallbacks = function () {
services.runtime.Runner.addMessageCallback((message) => {
printOutputMessage(
`Placeholder value is (${message.id}): ${message.data.name} of type ${message.data.type} = ${message.data.value}`,
);
}, 'placeholder_value');
services.runtime.Runner.addMessageCallback((message) => {
printOutputMessage(
`Placeholder was calculated (${message.id}): ${message.data.name} of type ${message.data.type}`,
);
const execInfo = services.runtime.Runner.getExecutionContext(message.id)!;
execInfo.calculatedPlaceholders.set(message.data.name, message.data.type);
// services.runtime.Runner.sendMessageToPythonServer(
// messages.createPlaceholderQueryMessage(message.id, message.data.name),
//);
}, 'placeholder_type');
services.runtime.Runner.addMessageCallback((message) => {
printOutputMessage(`Runner-Progress (${message.id}): ${message.data}`);
}, 'runtime_progress');
services.runtime.Runner.addMessageCallback(async (message) => {
let readableStacktraceSafeDs: string[] = [];
const execInfo = services.runtime.Runner.getExecutionContext(message.id)!;
const readableStacktracePython = await Promise.all(
(<messages.RuntimeErrorMessage>message).data.backtrace.map(async (frame) => {
const mappedFrame = await services.runtime.Runner.tryMapToSafeDSSource(message.id, frame);
if (mappedFrame) {
readableStacktraceSafeDs.push(
`\tat ${URI.file(execInfo.path)}#${mappedFrame.line} (${execInfo.path} line ${
mappedFrame.line
})`,
);
return `\tat ${frame.file} line ${frame.line} (mapped to '${mappedFrame.file}' line ${mappedFrame.line})`;
}
return `\tat ${frame.file} line ${frame.line}`;
}),
);
logOutput(
`Runner-RuntimeError (${message.id}): ${
(<messages.RuntimeErrorMessage>message).data.message
} \n${readableStacktracePython.join('\n')}`,
);
printOutputMessage(
`Safe-DS Error (${message.id}): ${(<messages.RuntimeErrorMessage>message).data.message} \n${readableStacktraceSafeDs
.reverse()
.join('\n')}`,
);
}, 'runtime_error');
};

const registerVSCodeCommands = function (context: vscode.ExtensionContext) {
const registerCommandWithCheck = (commandId: string, callback: (...args: any[]) => Promise<any>) => {
return vscode.commands.registerCommand(commandId, (...args: any[]) => {
Expand Down

0 comments on commit 568ee20

Please sign in to comment.