Skip to content

Commit

Permalink
Change how logging level is passed to native process (#11438)
Browse files Browse the repository at this point in the history
  • Loading branch information
Colengms authored Sep 18, 2023
1 parent dec5059 commit 4d1a2a0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
41 changes: 22 additions & 19 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,6 @@ function logTelemetry(notificationBody: TelemetryPayload): void {
telemetry.logLanguageServerEvent(notificationBody.event, notificationBody.properties, notificationBody.metrics);
}

/**
* listen for logging messages from the language server and print them to the Output window
*/
function setupOutputHandlers(): void {
console.assert(languageClient !== undefined, "This method must not be called until this.languageClient is set in \"onReady\"");

languageClient.onNotification(DebugProtocolNotification, logDebugProtocol);
languageClient.onNotification(DebugLogNotification, logLocalized);
}

/** Note: We should not await on the following functions,
* or any function that returns a promise acquired from them,
* vscode.window.showInformationMessage, vscode.window.showWarningMessage, vscode.window.showErrorMessage
Expand Down Expand Up @@ -537,7 +527,11 @@ export interface TextDocumentWillSaveParams {
reason: vscode.TextDocumentSaveReason;
}

interface InitializationOptions {
interface LspInitializationOptions {
loggingLevel: number;
}

interface CppInitializationParams {
packageVersion: string;
extensionPath: string;
cacheStoragePath: string;
Expand All @@ -559,6 +553,7 @@ interface TagParseStatus {
}

// Requests
const InitializationRequest: RequestType<CppInitializationParams, void, void> = new RequestType<CppInitializationParams, void, void>('cpptools/initialize');
const QueryCompilerDefaultsRequest: RequestType<QueryDefaultCompilerParams, configs.CompilerDefaults, void> = new RequestType<QueryDefaultCompilerParams, configs.CompilerDefaults, void>('cpptools/queryCompilerDefaults');
const QueryTranslationUnitSourceRequest: RequestType<QueryTranslationUnitSourceParams, QueryTranslationUnitSourceResult, void> = new RequestType<QueryTranslationUnitSourceParams, QueryTranslationUnitSourceResult, void>('cpptools/queryTranslationUnitSource');
const SwitchHeaderSourceRequest: RequestType<SwitchHeaderSourceParams, string, void> = new RequestType<SwitchHeaderSourceParams, string, void>('cpptools/didSwitchHeaderSource');
Expand Down Expand Up @@ -597,7 +592,6 @@ const PreviewReferencesNotification: NotificationType<void> = new NotificationTy
const RescanFolderNotification: NotificationType<void> = new NotificationType<void>('cpptools/rescanFolder');
const FinishedRequestCustomConfig: NotificationType<FinishedRequestCustomConfigParams> = new NotificationType<FinishedRequestCustomConfigParams>('cpptools/finishedRequestCustomConfig');
const DidChangeSettingsNotification: NotificationType<SettingsParams> = new NotificationType<SettingsParams>('cpptools/didChangeSettings');
const InitializationNotification: NotificationType<InitializationOptions> = new NotificationType<InitializationOptions>('cpptools/initialize');

const CodeAnalysisNotification: NotificationType<CodeAnalysisParams> = new NotificationType<CodeAnalysisParams>('cpptools/runCodeAnalysis');
const PauseCodeAnalysisNotification: NotificationType<void> = new NotificationType<void>('cpptools/pauseCodeAnalysis');
Expand Down Expand Up @@ -828,7 +822,7 @@ export class DefaultClient implements Client {
private isSupported: boolean = true;
private inactiveRegionsDecorations = new Map<string, DecorationRangesPair>();
private settingsTracker: SettingsTracker;
private loggingLevel: string | undefined;
private loggingLevel: number = 1;
private configurationProvider?: string;

public lastCustomBrowseConfiguration: PersistentFolderState<WorkspaceBrowseConfiguration | undefined> | undefined;
Expand Down Expand Up @@ -1522,7 +1516,7 @@ export class DefaultClient implements Client {
const databaseStoragePath: string = (cacheStoragePath.length > 0) && (workspaceHash.length > 0) ?
path.join(cacheStoragePath, workspaceHash) : "";

const initializationOptions: InitializationOptions = {
const cppInitializationParams: CppInitializationParams = {
packageVersion: util.packageJson.version,
extensionPath: util.extensionPath,
databaseStoragePath: databaseStoragePath,
Expand All @@ -1538,12 +1532,18 @@ export class DefaultClient implements Client {
settings: this.getAllSettings()
};

this.loggingLevel = util.getNumericLoggingLevel(cppInitializationParams.settings.loggingLevel);
const lspInitializationOptions: LspInitializationOptions = {
loggingLevel: this.loggingLevel
};

const clientOptions: LanguageClientOptions = {
documentSelector: [
{ scheme: 'file', language: 'c' },
{ scheme: 'file', language: 'cpp' },
{ scheme: 'file', language: 'cuda-cpp' }
],
initializationOptions: lspInitializationOptions,
middleware: createProtocolFilter(),
errorHandler: {
error: (_error, _message, _count) => ({ action: ErrorAction.Continue }),
Expand Down Expand Up @@ -1576,13 +1576,16 @@ export class DefaultClient implements Client {
};

// Create the language client
this.loggingLevel = initializationOptions.settings.loggingLevel;
languageClient = new LanguageClient(`cpptools`, serverOptions, clientOptions);
setupOutputHandlers();
languageClient.onNotification(DebugProtocolNotification, logDebugProtocol);
languageClient.onNotification(DebugLogNotification, logLocalized);
languageClient.registerProposedFeatures();
await languageClient.start();

// Move initialization to a separate message, so we can see log output from it.
await languageClient.sendNotification(InitializationNotification, initializationOptions);
// A request is used in order to wait for completion and ensure that no subsequent
// higher priority message may be processed before the Initialization request.
await languageClient.sendRequest(InitializationRequest, cppInitializationParams);
}

public async sendDidChangeSettings(): Promise<void> {
Expand All @@ -1607,9 +1610,9 @@ export class DefaultClient implements Client {
updateLanguageConfigurations();
}
if (changedSettings.loggingLevel) {
const oldLoggingLevelLogged: boolean = !!this.loggingLevel && this.loggingLevel !== "None" && this.loggingLevel !== "Error";
const oldLoggingLevelLogged: boolean = !!this.loggingLevel && this.loggingLevel !== 0 && this.loggingLevel !== 1;
const newLoggingLevel: string | undefined = changedSettings.loggingLevel;
this.loggingLevel = newLoggingLevel;
this.loggingLevel = util.getNumericLoggingLevel(newLoggingLevel);
const newLoggingLevelLogged: boolean = !!newLoggingLevel && newLoggingLevel !== "None" && newLoggingLevel !== "Error";
if (oldLoggingLevelLogged || newLoggingLevelLogged) {
const out: Logger = getOutputChannelLogger();
Expand Down
27 changes: 27 additions & 0 deletions Extension/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1519,3 +1519,30 @@ export function hasMsvcEnvironment(): boolean {
];
return msvcEnvVars.every((envVarName) => process.env[envVarName] !== undefined && process.env[envVarName] !== '');
}

function isIntegral(str: string): boolean {
const regex = /^-?\d+$/;
return regex.test(str);
}

export function getNumericLoggingLevel(loggingLevel: string | undefined): number {
if (!loggingLevel) {
return 1;
}
if (isIntegral(loggingLevel)) {
return parseInt(loggingLevel, 10);
}
const lowerCaseLoggingLevel: string = loggingLevel.toLowerCase();
switch (lowerCaseLoggingLevel) {
case "error":
return 1;
case "warning":
return 3;
case "information":
return 5;
case "debug":
return 6;
default:
return 0;
}
}

0 comments on commit 4d1a2a0

Please sign in to comment.