简体   繁体   English

在VSCode扩展中检测调试模式

[英]Detect debug mode in VSCode Extension

I am developing a VSCode Extension and I would like to write a simple logging utility that only logs to the console during debugging, otherwise, it's a no-op. 我正在开发VSCode扩展,并且我想编写一个简单的日志记录实用程序,该实用程序仅在调试期间记录到控制台,否则,它是无操作的。

Is there a flag or value somewhere available in an extension that indicated debugging is currently ongoing? 扩展中某处是否有标志或值表示调试正在进行中?

In case someone still needs it, one solution is to use custom environment variable when starting a client in a debug mode. 如果有人仍然需要它,一种解决方案是在调试模式下启动客户端时使用自定义环境变量。

In your launch.json file: 在您的launch.json文件中:

{
    "type": "extensionHost",
    "request": "launch",
    "name": "Launch Client",
    "runtimeExecutable": "${execPath}",
    "args": ["--extensionDevelopmentPath=${workspaceRoot}"],
    "outFiles": ["${workspaceRoot}/client/out/**/*.js"],
    "env": {
        "VSCODE_DEBUG_MODE": "true"
    }
}

Then, you can check it in your code like this: 然后,您可以像下面这样在代码中检查它:

const isDebugMode = () => process.env.VSCODE_DEBUG_MODE === "true";

export function activate(context: ExtensionContext) {
  if (isDebugMode()) {
    // Debug ...
  } else {
    // Else ...
  }
}

This is now officially supported: 现在已正式支持:

https://github.com/Microsoft/vscode/blob/master/src/vs/vscode.d.ts#L6431 https://github.com/Microsoft/vscode/blob/master/src/vs/vscode.d.ts#L6431

export namespace debug {

    /**
     * The currently active [debug session](#DebugSession) or `undefined`. The active debug session is the one
     * represented by the debug action floating window or the one currently shown in the drop down menu of the debug action floating window.
     * If no debug session is active, the value is `undefined`.
     */
    export let activeDebugSession: DebugSession | undefined;

    /**
     * The currently active [debug console](#DebugConsole).
     */
    export let activeDebugConsole: DebugConsole;

    /**
     * List of breakpoints.
     */
    export let breakpoints: Breakpoint[];


    /**
     * An [event](#Event) which fires when the [active debug session](#debug.activeDebugSession)
     * has changed. *Note* that the event also fires when the active debug session changes
     * to `undefined`.
     */
    export const onDidChangeActiveDebugSession: Event<DebugSession | undefined>;

    /**
     * An [event](#Event) which fires when a new [debug session](#DebugSession) has been started.
     */
    export const onDidStartDebugSession: Event<DebugSession>;

    /**
     * An [event](#Event) which fires when a custom DAP event is received from the [debug session](#DebugSession).
     */
    export const onDidReceiveDebugSessionCustomEvent: Event<DebugSessionCustomEvent>;

    /**
     * An [event](#Event) which fires when a [debug session](#DebugSession) has terminated.
     */
    export const onDidTerminateDebugSession: Event<DebugSession>;

    /**
     * An [event](#Event) that is emitted when the set of breakpoints is added, removed, or changed.
     */
    export const onDidChangeBreakpoints: Event<BreakpointsChangeEvent>;


    /**
     * Register a [debug configuration provider](#DebugConfigurationProvider) for a specifc debug type.
     * More than one provider can be registered for the same type.
     *
     * @param type The debug type for which the provider is registered.
     * @param provider The [debug configuration provider](#DebugConfigurationProvider) to register.
     * @return A [disposable](#Disposable) that unregisters this provider when being disposed.
     */
    export function registerDebugConfigurationProvider(debugType: string, provider: DebugConfigurationProvider): Disposable;

    /**
     * Start debugging by using either a named launch or named compound configuration,
     * or by directly passing a [DebugConfiguration](#DebugConfiguration).
     * The named configurations are looked up in '.vscode/launch.json' found in the given folder.
     * Before debugging starts, all unsaved files are saved and the launch configurations are brought up-to-date.
     * Folder specific variables used in the configuration (e.g. '${workspaceFolder}') are resolved against the given folder.
     * @param folder The [workspace folder](#WorkspaceFolder) for looking up named configurations and resolving variables or `undefined` for a non-folder setup.
     * @param nameOrConfiguration Either the name of a debug or compound configuration or a [DebugConfiguration](#DebugConfiguration) object.
     * @return A thenable that resolves when debugging could be successfully started.
     */
    export function startDebugging(folder: WorkspaceFolder | undefined, nameOrConfiguration: string | DebugConfiguration): Thenable<boolean>;

    /**
     * Add breakpoints.
     * @param breakpoints The breakpoints to add.
    */
    export function addBreakpoints(breakpoints: Breakpoint[]): void;

    /**
     * Remove breakpoints.
     * @param breakpoints The breakpoints to remove.
     */
    export function removeBreakpoints(breakpoints: Breakpoint[]): void;
}

It seems it is not officially supported: https://github.com/Microsoft/vscode/issues/10077 似乎它不受官方支持: https//github.com/Microsoft/vscode/issues/10077

Anyway I found this peace of code, no idea how good it is: 无论如何,我发现了这种代码的安宁,不知道它有多好:

function startedInDebugMode() {
  let args = process.execArgv;
  if (args) {
    return args.some((arg) => /^--debug=?/.test(arg) || /^--debug-brk=?/.test(arg));
  }
  return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM