Skip to content

Commit 42c4652

Browse files
logging enhancement
1 parent cd42934 commit 42c4652

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

codeflash/api/aiservice.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def optimize_python_code( # noqa: D417
133133
"repo_name": git_repo_name,
134134
}
135135

136+
logger.info("!lsp|tags|Generating optimized candidates…")
136137
console.rule()
137138
try:
138139
response = self.make_ai_service_request("/optimize", payload=payload, timeout=600)
@@ -143,8 +144,10 @@ def optimize_python_code( # noqa: D417
143144

144145
if response.status_code == 200:
145146
optimizations_json = response.json()["optimizations"]
147+
logger.info(f"!lsp|tags|Generated {len(optimizations_json)} candidate optimizations.")
148+
console.rule()
146149
end_time = time.perf_counter()
147-
logger.debug(f"Generating optimizations took {end_time - start_time:.2f} seconds.")
150+
logger.debug(f"!lsp|tags|Generating optimizations took {end_time - start_time:.2f} seconds.")
148151
return self._get_valid_candidates(optimizations_json)
149152
try:
150153
error = response.json()["error"]
@@ -191,7 +194,6 @@ def optimize_python_code_line_profiler( # noqa: D417
191194
"lsp_mode": is_LSP_enabled(),
192195
}
193196

194-
logger.info("loading|tags|Generating optimized candidates using line profiler information…")
195197
console.rule()
196198
if line_profiler_results == "":
197199
logger.info("No LineProfiler results were provided, Skipping optimization.")
@@ -206,6 +208,9 @@ def optimize_python_code_line_profiler( # noqa: D417
206208

207209
if response.status_code == 200:
208210
optimizations_json = response.json()["optimizations"]
211+
logger.info(
212+
f"!lsp|tags|Generated {len(optimizations_json)} candidate optimizations using line profiler information."
213+
)
209214
console.rule()
210215
return self._get_valid_candidates(optimizations_json)
211216
try:

codeflash/cli_cmds/console.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,8 @@ def code_print(code_str: str, file_name: Optional[str] = None, function_name: Op
8787
"""Print code with syntax highlighting."""
8888
from rich.syntax import Syntax
8989

90-
formatted_code = Syntax(code_str, "python", line_numbers=True, theme="github-dark")
91-
9290
console.rule()
93-
console.print(formatted_code)
91+
console.print(Syntax(code_str, "python", line_numbers=True, theme="github-dark"))
9492
console.rule()
9593

9694

codeflash/lsp/lsp_logger.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
class LspMessageTags:
1212
# always set default values for message tags
1313
not_lsp: bool = False # !lsp (prevent the message from being sent to the LSP)
14-
force_lsp: bool = False # lsp (you can use this to force a message to be sent to the LSP even if the level is not supported)
14+
lsp: bool = False # lsp (lsp only)
15+
force_lsp: bool = False # force_lsp (you can use this to force a message to be sent to the LSP even if the level is not supported)
1516
loading: bool = False # loading (you can use this to indicate that the message is a loading message)
1617

1718
h1: bool = False # h1
@@ -33,16 +34,15 @@ def add_heading_tags(msg: str, tags: LspMessageTags) -> str:
3334

3435

3536
def extract_tags(msg: str) -> tuple[Optional[LspMessageTags], str]:
36-
if not isinstance(msg, str):
37-
return None, msg
38-
3937
parts = msg.split("|tags|")
4038
if len(parts) == 2:
4139
message_tags = LspMessageTags()
4240
tags = {tag.strip() for tag in parts[0].split(",")}
4341
if "!lsp" in tags:
4442
message_tags.not_lsp = True
4543
if "lsp" in tags:
44+
message_tags.lsp = True
45+
if "force_lsp" in tags:
4646
message_tags.force_lsp = True
4747
if "loading" in tags:
4848
message_tags.loading = True
@@ -63,16 +63,21 @@ def extract_tags(msg: str) -> tuple[Optional[LspMessageTags], str]:
6363

6464

6565
def enhanced_log(
66-
msg: str,
66+
msg: str | Any, # noqa: ANN401
6767
actual_log_fn: Callable[[str, Any, Any], None],
6868
level: str,
6969
*args: Any, # noqa: ANN401
7070
**kwargs: Any, # noqa: ANN401
7171
) -> None:
72-
lsp_enabled = is_LSP_enabled()
72+
if not isinstance(msg, str):
73+
actual_log_fn(msg, *args, **kwargs)
74+
return
75+
7376
tags, clean_msg = extract_tags(msg)
77+
lsp_enabled = is_LSP_enabled()
78+
lsp_only = tags and tags.lsp
7479

75-
if not lsp_enabled or not isinstance(clean_msg, str):
80+
if not lsp_enabled and not lsp_only:
7681
actual_log_fn(clean_msg, *args, **kwargs)
7782
return
7883

codeflash/lsp/server_entry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Configure logging to stderr for VS Code output channel
1717
def setup_logging() -> logging.Logger:
1818
# Clear any existing handlers to prevent conflicts
19-
root_logger = logging.getLogger("rich")
19+
root_logger = logging.getLogger()
2020
root_logger.handlers.clear()
2121

2222
# Set up stderr handler for VS Code output channel with [LSP-Server] prefix

codeflash/optimization/function_optimizer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ def _process_line_profiler_results(self) -> OptimizedCandidate | None:
152152
concurrent.futures.wait([self.future_line_profile_results])
153153
line_profile_results = self.future_line_profile_results.result()
154154

155-
logger.info(f"Generated {len(line_profile_results)} candidate optimizations using line profiler information.")
156-
157155
for candidate in line_profile_results:
158156
self.candidate_queue.put(candidate)
159157

@@ -517,7 +515,7 @@ def determine_best_candidate(
517515
)
518516
if not did_update:
519517
logger.warning(
520-
"lsp|tags|No functions were replaced in the optimized code. Skipping optimization candidate."
518+
"force_lsp|tags|No functions were replaced in the optimized code. Skipping optimization candidate."
521519
)
522520
console.rule()
523521
continue
@@ -1085,7 +1083,7 @@ def generate_tests_and_optimizations(
10851083

10861084
# Retrieve results
10871085
candidates: list[OptimizedCandidate] = future_optimization_candidates.result()
1088-
logger.info(f"Generated '{len(candidates)}' candidate optimizations.")
1086+
logger.info(f"lsp|tags|Generated '{len(candidates)}' candidate optimizations.")
10891087
console.rule()
10901088

10911089
if not candidates:

0 commit comments

Comments
 (0)