Skip to content

Commit

Permalink
Add setting to control markdown LS log level (#176472)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed Mar 8, 2023
1 parent b2ea6aa commit da15b03
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
11 changes: 11 additions & 0 deletions extensions/markdown-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,17 @@
"default": "off",
"description": "%markdown.trace.server.desc%"
},
"markdown.server.log": {
"type": "string",
"scope": "window",
"enum": [
"off",
"debug",
"trace"
],
"default": "off",
"description": "%markdown.server.log.desc%"
},
"markdown.editor.drop.enabled": {
"type": "boolean",
"default": true,
Expand Down
1 change: 1 addition & 0 deletions extensions/markdown-language-features/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"markdown.showPreviewSecuritySelector.title": "Change Preview Security Settings",
"markdown.trace.extension.desc": "Enable debug logging for the Markdown extension.",
"markdown.trace.server.desc": "Traces the communication between VS Code and the Markdown language server.",
"markdown.server.log.desc": "Controls the logging level of the Markdown language server.",
"markdown.preview.refresh.title": "Refresh Preview",
"markdown.preview.toggleLock.title": "Toggle Preview Locking",
"markdown.findAllFileReferences": "Find File References",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export type ValidateEnabled = 'ignore' | 'warning' | 'error' | 'hint';

export interface Settings {
readonly markdown: {
readonly server: {
readonly log: 'off' | 'debug' | 'trace';
};

readonly preferredMdPathExtensionStyle: 'auto' | 'includeExtension' | 'removeExtension';

readonly occurrencesHighlight: {
Expand Down
47 changes: 33 additions & 14 deletions extensions/markdown-language-features/server/src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { ILogger, LogLevel } from 'vscode-markdown-languageservice';
import * as md from 'vscode-markdown-languageservice';
import { ConfigurationManager } from './configuration';
import { Disposable } from './util/dispose';

export class LogFunctionLogger implements ILogger {
export class LogFunctionLogger extends Disposable implements md.ILogger {

private static now(): string {
const now = new Date();
Expand All @@ -27,16 +29,35 @@ export class LogFunctionLogger implements ILogger {
return JSON.stringify(data, undefined, 2);
}

private _logLevel: md.LogLevel;

constructor(
private readonly _logFn: typeof console.log
) { }
private readonly _logFn: typeof console.log,
private readonly _config: ConfigurationManager,
) {
super();

this._register(this._config.onDidChangeConfiguration(() => {
this._logLevel = LogFunctionLogger.readLogLevel(this._config);
}));

get level(): LogLevel {
return LogLevel.Debug; // TODO: remove hardcoding
this._logLevel = LogFunctionLogger.readLogLevel(this._config);
}

public log(level: LogLevel, message: string, data?: any): void {
if (level < this.level) {
private static readLogLevel(config: ConfigurationManager): md.LogLevel {
switch (config.getSettings()?.markdown.server.log) {
case 'trace': return md.LogLevel.Trace;
case 'debug': return md.LogLevel.Debug;
case 'off':
default:
return md.LogLevel.Off;
}
}

get level(): md.LogLevel { return this._logLevel; }

public log(level: md.LogLevel, message: string, data?: any): void {
if (this.level < level) {
return;
}

Expand All @@ -46,17 +67,15 @@ export class LogFunctionLogger implements ILogger {
}
}

private toLevelLabel(level: LogLevel): string {
private toLevelLabel(level: md.LogLevel): string {
switch (level) {
case LogLevel.Off: return 'Off';
case LogLevel.Debug: return 'Debug';
case LogLevel.Trace: return 'Trace';
case md.LogLevel.Off: return 'Off';
case md.LogLevel.Debug: return 'Debug';
case md.LogLevel.Trace: return 'Trace';
}
}

private appendLine(value: string): void {
this._logFn(value);
}
}

export const consoleLogger = new LogFunctionLogger(console.log);
15 changes: 8 additions & 7 deletions extensions/markdown-language-features/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ interface MdServerInitializationOptions extends LsConfiguration { }
const organizeLinkDefKind = 'source.organizeLinkDefinitions';

export async function startVsCodeServer(connection: Connection) {
const logger = new LogFunctionLogger(connection.console.log.bind(connection.console));
const configurationManager = new ConfigurationManager(connection);
const logger = new LogFunctionLogger(connection.console.log.bind(connection.console), configurationManager);

const parser = new class implements md.IMdParser {
slugifier = md.githubSlugifier;
Expand All @@ -41,7 +42,7 @@ export async function startVsCodeServer(connection: Connection) {
return workspace;
};

return startServer(connection, { documents, notebooks, logger, parser, workspaceFactory });
return startServer(connection, { documents, notebooks, configurationManager, logger, parser, workspaceFactory });
}

type WorkspaceFactory = (config: {
Expand All @@ -53,6 +54,7 @@ type WorkspaceFactory = (config: {
export async function startServer(connection: Connection, serverConfig: {
documents: TextDocuments<md.ITextDocument>;
notebooks?: NotebookDocuments<md.ITextDocument>;
configurationManager: ConfigurationManager;
logger: md.ILogger;
parser: md.IMdParser;
workspaceFactory: WorkspaceFactory;
Expand All @@ -62,7 +64,6 @@ export async function startServer(connection: Connection, serverConfig: {
let mdLs: md.IMdLanguageService | undefined;

connection.onInitialize((params: InitializeParams): InitializeResult => {
const configurationManager = new ConfigurationManager(connection);
const initOptions = params.initializationOptions as MdServerInitializationOptions | undefined;

const mdConfig = getLsConfiguration(initOptions ?? {});
Expand All @@ -74,7 +75,7 @@ export async function startServer(connection: Connection, serverConfig: {
logger: serverConfig.logger,
...mdConfig,
get preferredMdPathExtensionStyle() {
switch (configurationManager.getSettings()?.markdown.preferredMdPathExtensionStyle) {
switch (serverConfig.configurationManager.getSettings()?.markdown.preferredMdPathExtensionStyle) {
case 'includeExtension': return md.PreferredMdPathExtensionStyle.includeExtension;
case 'removeExtension': return md.PreferredMdPathExtensionStyle.removeExtension;
case 'auto':
Expand All @@ -84,9 +85,9 @@ export async function startServer(connection: Connection, serverConfig: {
}
});

registerCompletionsSupport(connection, documents, mdLs, configurationManager);
registerDocumentHighlightSupport(connection, documents, mdLs, configurationManager);
registerValidateSupport(connection, workspace, documents, mdLs, configurationManager, serverConfig.logger);
registerCompletionsSupport(connection, documents, mdLs, serverConfig.configurationManager);
registerDocumentHighlightSupport(connection, documents, mdLs, serverConfig.configurationManager);
registerValidateSupport(connection, workspace, documents, mdLs, serverConfig.configurationManager, serverConfig.logger);

return {
capabilities: {
Expand Down

0 comments on commit da15b03

Please sign in to comment.