Skip to content

Commit fbef2ff

Browse files
committed
Improve stdout logging
1 parent b315d52 commit fbef2ff

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

src/process_query.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,33 @@
44

55
from config import MAX_DISPLAY_SIZE, EXAMPLE_REPOS
66
from gitingest import ingest_from_query, clone_repo, parse_query
7-
from server_utils import logSliderToSize
7+
from server_utils import logSliderToSize, Colors
88

99
templates = Jinja2Templates(directory="templates")
1010

11+
def print_query(query, request, max_file_size, pattern_type, pattern):
12+
print(f"{Colors.WHITE}{query['url']:<20}{Colors.END}", end="")
13+
if int(max_file_size/1024) != 50:
14+
print(f" | {Colors.PURPLE}Size: {int(max_file_size/1024)}kb{Colors.END}", end="")
15+
if pattern_type == "include" and pattern != "":
16+
print(f" | {Colors.BLUE}Include {pattern}{Colors.END}", end="")
17+
elif pattern_type == "exclude" and pattern != "":
18+
print(f" | {Colors.YELLOW}Exclude {pattern}{Colors.END}", end="")
19+
20+
21+
def print_error(query, request, e, max_file_size, pattern_type, pattern):
22+
print(f"{Colors.BROWN}WARNING{Colors.END}: {Colors.RED}<- {Colors.END}", end="")
23+
print_query(query, request, max_file_size, pattern_type, pattern)
24+
print(f" | {Colors.RED}{e}{Colors.END}")
25+
26+
def print_success(query, request, max_file_size, pattern_type, pattern, summary):
27+
estimated_tokens = summary[summary.index("Estimated tokens:") + len("Estimated ") :]
28+
print(f"{Colors.GREEN}INFO{Colors.END}: {Colors.GREEN}<- {Colors.END}", end="")
29+
print_query(query, request, max_file_size, pattern_type, pattern)
30+
print(f" | {Colors.PURPLE}{estimated_tokens}{Colors.END}")
31+
32+
33+
1134
async def process_query(request: Request, input_text: str, slider_position: int, pattern_type: str = "exclude", pattern: str = "", is_index: bool = False) -> str:
1235
template = "index.jinja" if is_index else "github.jinja"
1336
max_file_size = logSliderToSize(slider_position)
@@ -23,11 +46,14 @@ async def process_query(request: Request, input_text: str, slider_position: int,
2346
summary, tree, content = ingest_from_query(query)
2447
with open(f"{query['local_path']}.txt", "w") as f:
2548
f.write(tree + "\n" + content)
26-
print(f"{query['slug']:<20}", end="")
27-
if pattern and pattern != "":
28-
print(f"{pattern_type}[{pattern}]", end="")
29-
print(f"\n{query['url']}")
49+
50+
51+
3052
except Exception as e:
53+
if 'query' in locals() and query is not None and isinstance(query, dict):
54+
print_error(query, request, e, max_file_size, pattern_type, pattern)
55+
else:
56+
print(f"{Colors.RED}Error: {e}{Colors.END}")
3157
return templates.TemplateResponse(
3258
template,
3359
{
@@ -43,7 +69,7 @@ async def process_query(request: Request, input_text: str, slider_position: int,
4369

4470
if len(content) > MAX_DISPLAY_SIZE:
4571
content = f"(Files content cropped to {int(MAX_DISPLAY_SIZE/1000)}k characters, download full ingest to see more)\n" + content[:MAX_DISPLAY_SIZE]
46-
72+
print_success(query, request, max_file_size, pattern_type, pattern, summary)
4773
return templates.TemplateResponse(
4874
template,
4975
{

src/server_utils.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
## Rate Limiter
32
from slowapi import Limiter
43
from slowapi.util import get_remote_address
@@ -12,4 +11,32 @@ def logSliderToSize(position):
1211
minv = math.log(1)
1312
maxv = math.log(102400)
1413

15-
return round(math.exp(minv + (maxv - minv) * pow(position / maxp, 1.5))) * 1024
14+
return round(math.exp(minv + (maxv - minv) * pow(position / maxp, 1.5))) * 1024
15+
16+
## Color printing utility
17+
class Colors:
18+
"""ANSI color codes"""
19+
BLACK = "\033[0;30m"
20+
RED = "\033[0;31m"
21+
GREEN = "\033[0;32m"
22+
BROWN = "\033[0;33m"
23+
BLUE = "\033[0;34m"
24+
PURPLE = "\033[0;35m"
25+
CYAN = "\033[0;36m"
26+
LIGHT_GRAY = "\033[0;37m"
27+
DARK_GRAY = "\033[1;30m"
28+
LIGHT_RED = "\033[1;31m"
29+
LIGHT_GREEN = "\033[1;32m"
30+
YELLOW = "\033[1;33m"
31+
LIGHT_BLUE = "\033[1;34m"
32+
LIGHT_PURPLE = "\033[1;35m"
33+
LIGHT_CYAN = "\033[1;36m"
34+
WHITE = "\033[1;37m"
35+
BOLD = "\033[1m"
36+
FAINT = "\033[2m"
37+
ITALIC = "\033[3m"
38+
UNDERLINE = "\033[4m"
39+
BLINK = "\033[5m"
40+
NEGATIVE = "\033[7m"
41+
CROSSED = "\033[9m"
42+
END = "\033[0m"

0 commit comments

Comments
 (0)