Skip to content

Commit 9f8b100

Browse files
override other log methods and log serialized lsp messages
1 parent d8a7255 commit 9f8b100

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

codeflash/cli_cmds/console.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from __future__ import annotations
22

3+
import json
34
import logging
4-
import os
55
from contextlib import contextmanager
66
from itertools import cycle
7-
from typing import TYPE_CHECKING
7+
from typing import TYPE_CHECKING, Any
88

99
from rich.console import Console
1010
from rich.logging import RichHandler
@@ -20,7 +20,7 @@
2020

2121
from codeflash.cli_cmds.console_constants import SPINNER_TYPES
2222
from codeflash.cli_cmds.logging_config import BARE_LOGGING_FORMAT
23-
from codeflash.lsp.helpers import lsp_log
23+
from codeflash.lsp.helpers import enhanced_log, is_LSP_enabled
2424

2525
if TYPE_CHECKING:
2626
from collections.abc import Generator
@@ -31,7 +31,7 @@
3131

3232
console = Console()
3333

34-
if os.getenv("CODEFLASH_LSP"):
34+
if is_LSP_enabled():
3535
console.quiet = True
3636

3737
logging.basicConfig(
@@ -43,11 +43,17 @@
4343
logger = logging.getLogger("rich")
4444
logging.getLogger("parso").setLevel(logging.WARNING)
4545

46-
real_info_log_fn = logger.info
47-
logger.info = lambda msg, *args, **kwargs: lsp_log(msg, real_info_log_fn, *args, **kwargs)
46+
# override the logger to reformat the messages for the lsp
47+
for level in ("info", "debug", "warning", "error"):
48+
real_fn = getattr(logger, level)
49+
setattr(logger, level, lambda msg, *args, _real_fn=real_fn, **kwargs: enhanced_log(msg, _real_fn, *args, **kwargs))
4850

49-
real_debug_log_fn = logger.debug
50-
logger.debug = lambda msg, *args, **kwargs: lsp_log(msg, real_debug_log_fn, *args, **kwargs)
51+
52+
def lsp_log(serializable: dict[str, Any], *args: Any, **kwargs: Any) -> None: # noqa: ANN401
53+
if not is_LSP_enabled():
54+
return
55+
msg_str = json.dumps(serializable)
56+
logger.info(msg_str, *args, **kwargs)
5157

5258

5359
def paneled_text(
@@ -69,8 +75,13 @@ def code_print(code_str: str) -> None:
6975
"""Print code with syntax highlighting."""
7076
from rich.syntax import Syntax
7177

78+
formatted_code = Syntax(code_str, "python", line_numbers=True, theme="github-dark")
79+
# if is_LSP_enabled():
80+
# logger.debug(formatted_code.__str__())
81+
# return
82+
7283
console.rule()
73-
console.print(Syntax(code_str, "python", line_numbers=True, theme="github-dark"))
84+
console.print(formatted_code)
7485
console.rule()
7586

7687

codeflash/lsp/helpers.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@ def is_LSP_enabled() -> bool:
88
return os.getenv("CODEFLASH_LSP", default="false").lower() == "true"
99

1010

11-
def lsp_log(msg: str, actual_log_fn: Callable[[str, Any, Any], None], *args: Any, **kwargs: Any) -> None: # noqa: ANN401
12-
if is_LSP_enabled():
13-
actual_log_fn(f"::::{msg}", *args, **kwargs)
14-
else:
11+
def enhanced_log(msg: str, actual_log_fn: Callable[[str, Any, Any], None], *args: Any, **kwargs: Any) -> None: # noqa: ANN401
12+
lsp_enabled = is_LSP_enabled()
13+
14+
# normal cli moded
15+
if not lsp_enabled:
1516
actual_log_fn(msg, *args, **kwargs)
17+
return
18+
19+
#### LSP mode ####
20+
if type(msg) != str: # noqa: E721
21+
return
22+
23+
if msg.startswith("Nonzero return code"):
24+
# skip logging the failed tests msg to the client
25+
return
26+
27+
actual_log_fn(f"::::{msg}", *args, **kwargs)

0 commit comments

Comments
 (0)