From b3783b9c6ac0de02ff3da1fb5226d1e7396ecd61 Mon Sep 17 00:00:00 2001 From: James Westman Date: Sat, 15 Oct 2022 11:26:18 -0500 Subject: [PATCH] lsp: Log to stderr rather than a file --- blueprintcompiler/lsp.py | 19 ++++++++----------- blueprintcompiler/main.py | 3 +-- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/blueprintcompiler/lsp.py b/blueprintcompiler/lsp.py index bbe4c1d..b1610d7 100644 --- a/blueprintcompiler/lsp.py +++ b/blueprintcompiler/lsp.py @@ -27,6 +27,10 @@ from .lsp_utils import * from . import tokenizer, parser, utils, xml_reader +def printerr(*args, **kwargs): + print(*args, file=sys.stderr, **kwargs) + + def command(json_method): def decorator(func): func._json_method = json_method @@ -89,10 +93,9 @@ class OpenFile: class LanguageServer: commands: T.Dict[str, T.Callable] = {} - def __init__(self, logfile=None): + def __init__(self): self.client_capabilities = {} self._open_files: {str: OpenFile} = {} - self.logfile = logfile def run(self): # Read tags from gir files. During normal compilation these are @@ -110,7 +113,7 @@ class LanguageServer: if line.startswith("Content-Length:"): content_len = int(line.split("Content-Length:")[1].strip()) line = sys.stdin.buffer.read(content_len).decode() - self._log("input: " + line) + printerr("input: " + line) data = json.loads(line) method = data.get("method") @@ -120,22 +123,16 @@ class LanguageServer: if method in self.commands: self.commands[method](self, id, params) except Exception as e: - self._log(traceback.format_exc()) + printerr(traceback.format_exc()) def _send(self, data): data["jsonrpc"] = "2.0" line = json.dumps(data, separators=(",", ":")) + "\r\n" - self._log("output: " + line) + printerr("output: " + line) sys.stdout.write(f"Content-Length: {len(line.encode())}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{line}") sys.stdout.flush() - def _log(self, msg): - if self.logfile is not None: - self.logfile.write(str(msg)) - self.logfile.write("\n") - self.logfile.flush() - def _send_response(self, id, result): self._send({ "id": id, diff --git a/blueprintcompiler/main.py b/blueprintcompiler/main.py index d528e7e..9f2f4c2 100644 --- a/blueprintcompiler/main.py +++ b/blueprintcompiler/main.py @@ -48,7 +48,6 @@ class BlueprintApp: port = self.add_subcommand("port", "Interactive porting tool", self.cmd_port) lsp = self.add_subcommand("lsp", "Run the language server (for internal use by IDEs)", self.cmd_lsp) - lsp.add_argument("--logfile", dest="logfile", default=None, type=argparse.FileType('a')) self.add_subcommand("help", "Show this message", self.cmd_help) @@ -125,7 +124,7 @@ class BlueprintApp: def cmd_lsp(self, opts): - langserv = LanguageServer(opts.logfile) + langserv = LanguageServer() langserv.run()