Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect console calls to connection.console #1299

Closed
remcohaszing opened this issue Aug 22, 2023 · 0 comments · Fixed by #1301
Closed

Redirect console calls to connection.console #1299

remcohaszing opened this issue Aug 22, 2023 · 0 comments · Fixed by #1301
Labels
help wanted Issues identified as good community contribution opportunities

Comments

@remcohaszing
Copy link
Contributor

A language server may use console methods for various reasons. For example:

  • A third party dependency assumes this is ok
  • A third party plugin assumes this is ok
  • It’s commonly used for logging / debugging.

When using the stdio protocol with vscode-languageserver, this causes issues.

In this case, it would be nice if console calls are patched, so the output gets redirected to connection.console

I think just the following methods would already catch most use cases:

import { inspect } from 'node:util'

function serialize(args: unknown[]) {
  return args.map(arg => typeof arg === 'string' ? arg : inspect(arg)).join(' ')
}

console.assert = (assertion, ...args) => {
  if (assertion) {
    return
  }
  if (args.length === 0) {
    connection.console.log('Assertion failed')
  } else {
    const [message, ...rest] = args
    connection.console.log(`Assertion failed: ${message} ${serialize(rest)}`)
  }
}
console.dir = (arg) => connection.console.log(inspect(arg))
console.log = (...args) => connection.console.log(serialize(args))
console.error = (...args) => connection.console.error(serialize(args))
console.warn = (...args) => connection.console.warn(serialize(args))
@dbaeumer dbaeumer added feature request help wanted Issues identified as good community contribution opportunities labels Aug 22, 2023
@dbaeumer dbaeumer added this to the On Deck milestone Aug 22, 2023
remcohaszing added a commit to remcohaszing/vscode-languageserver-node that referenced this issue Aug 22, 2023
dbaeumer added a commit that referenced this issue Sep 18, 2023
* Patch console methods if --stdio is used

Closes #1299

* Fix stylistic issue

* Implement more console methods

This implements `console.count`, `console.counrReset`, `console.debug`,
and `console.trace`. It also adds support for `console.dir` options.

---------

Co-authored-by: Dirk Bäumer <[email protected]>
@dbaeumer dbaeumer removed this from the On Deck milestone Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues identified as good community contribution opportunities
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants