6a6247bbf2
* Fix UTF-8 encoding for Priorities 1-2 (Core & Agents - 18 instances) Add encoding="utf-8" to file operations in: - Priority 1: Core Infrastructure (8 instances) - core/progress.py (6 read operations) - core/debug.py (1 append operation) - core/workspace/setup.py (1 read operation) - Priority 2: Agent System (10 instances) - agents/utils.py (1 read) - agents/tools_pkg/tools/subtask.py (1 read, 1 write) - agents/tools_pkg/tools/memory.py (2 read, 1 write, 1 append) - agents/tools_pkg/tools/qa.py (1 read, 1 write) - agents/tools_pkg/tools/progress.py (1 read) All changes use double quotes for ruff format compliance. * Fix UTF-8 encoding for Priorities 3-4 (Spec & Project - 26 instances) Add encoding="utf-8" to file operations in: - Priority 3: Spec Pipeline (21 instances) - spec/context.py (4: 2 read, 2 write) - spec/complexity.py (3: 2 read, 1 write) - spec/requirements.py (3: 2 read, 1 write) - spec/validator.py (3 write operations) - spec/writer.py (2: 1 read, 1 write) - spec/discovery.py (1 read) - spec/pipeline/orchestrator.py (2 read) - spec/phases/requirements_phases.py (1 write) - spec/validate_pkg/auto_fix.py (2: 1 read, 1 write) - Priority 4: Project Analyzer (5 instances) - project/analyzer.py (2: 1 read, 1 write) - project/config_parser.py (2 read operations) - project/stack_detector.py (1 read) All changes use double quotes for ruff format compliance. * Fix UTF-8 encoding for Priorities 5-7 (Services, Analysis, Ideation - 43 instances) Add encoding="utf-8" to file operations in: - Priority 5: Services (12 instances) - services/recovery.py (8: 4 read, 4 write) - services/context.py (4 read operations) - Priority 6: Analysis & QA (6 instances) - analysis/analyzers/__init__.py (2 write) - analysis/insight_extractor.py (1 read) - qa/criteria.py (2: 1 read, 1 write) - qa/report.py (1 read) - Priority 7: Ideation & Roadmap (25 instances) - ideation/analyzer.py (3 read) - ideation/formatter.py (4 read, 1 write) - ideation/phase_executor.py (5: 3 read, 2 write) - ideation/runner.py (1 read) - runners/roadmap/competitor_analyzer.py (3: 1 read, 2 write) - runners/roadmap/graph_integration.py (3 write) - runners/roadmap/orchestrator.py (1 read) - runners/roadmap/phases.py (2 read) - runners/insights_runner.py (3 read) All changes use double quotes for ruff format compliance. * Fix UTF-8 encoding for Priorities 8-14 (All remaining - 85+ instances) Add encoding="utf-8" to file operations across all remaining modules: Priorities 8-10 (Merge, Memory, Integrations - 26 instances): - merge/ (4 files) - memory/ (3 files) - context/ (3 files) - integrations/ (4 files) Priorities 11-14 (GitHub, GitLab, AI, Other - 59 instances): - runners/github/ (19 files) - runners/gitlab/ (3 files) - runners/ai_analyzer/ (1 file) All changes use double quotes for ruff format compliance. Applied using Python regex script for efficiency. * Fix UTF-8 encoding for missed instances (23 instances) Fix remaining instances missed by batch script: - cli/batch_commands.py (3 instances) - cli/followup_commands.py (1 instance) - core/client.py (1 instance) - phase_config.py (1 instance) - planner_lib/context.py (4 instances) - prediction/main.py (1 instance) - prediction/memory_loader.py (1 instance) - prompts_pkg/prompts.py (2 instances) - review/formatters.py (1 instance) - review/state.py (2 instances) - spec/phases/spec_phases.py (1 instance) - spec/pipeline/models.py (1 instance) - spec/validate_pkg/validators/context_validator.py (1 instance) - spec/validate_pkg/validators/implementation_plan_validator.py (1 instance) - ui/status.py (2 instances) All encoding parameters use double quotes for ruff format compliance. Verified: 0 instances without encoding remain in source code. * Fix missed os.fdopen() calls and duplicate encoding bug Thorough verification found 3 additional issues: - runners/github/file_lock.py:462 - os.fdopen missing encoding - runners/github/trust.py:442 - os.fdopen missing encoding - runners/insights_runner.py:372 - duplicate encoding parameter All fixed. Final count: 251 instances with encoding="utf-8" * Fix missed Path.read_text() and Path.write_text() encoding (99 instances) Gemini Code Assist review found instances we missed: - Path.read_text() without encoding: 77 instances → fixed - Path.write_text() without encoding: 22 instances → fixed Total UTF-8 encoding fixes: 350 instances across codebase - open() operations: 251 instances - Path.read_text(): 98 instances - Path.write_text(): 30 instances All text file operations now explicitly use encoding="utf-8". Addresses feedback from PR #782 review. * Fix critical syntax errors from CodeRabbit review - Fix os.getpid() syntax error in core/workspace/models.py (2 instances) Changed: os.getpid(, encoding="utf-8") -> str(os.getpid()) - Fix json.dumps invalid encoding parameter (3 instances) json.dumps() doesn't accept encoding parameter Changed: json.dumps(data, encoding="utf-8") -> json.dumps(data) Files: runners/ai_analyzer/cache_manager.py, runners/github/test_file_lock.py - Fix tempfile.NamedTemporaryFile missing encoding Added encoding="utf-8" to spec/requirements.py:22 - Fix subprocess.run text=True to encoding Changed: text=True -> encoding="utf-8" in core/workspace/setup.py:375 All critical syntax errors from CodeRabbit review resolved. * Fix critical syntax errors in test_context_gatherer.py - Line 78: Move encoding="utf-8" outside of JS string content Changed: write_text("...encoding="utf-8"...") To: write_text("...", encoding="utf-8") - Line 102: Move encoding="utf-8" outside of JS string content Changed: write_text("...encoding="utf-8"...") To: write_text("...", encoding="utf-8") Fixes syntax errors where encoding parameter was incorrectly placed inside the JavaScript code string instead of as write_text() parameter. * Fix CodeRabbit issues: UnicodeDecodeError handling and trailing newlines - Add UnicodeDecodeError to exception handling in agents/utils.py and spec/validate_pkg/auto_fix.py - Fix trailing newline preservation in merge/file_merger.py (2 locations) - Add encoding parameter to atomic_write() in runners/github/file_lock.py These fixes ensure robust error handling for malformed UTF-8 files and preserve file formatting during merge operations. * Fix test fixture to use UTF-8 encoding consistently Update spec_file fixture in tests/conftest.py to write spec file with encoding="utf-8" to match how it's read in validators. This ensures consistency between test fixtures and production code. * Fix linting errors and security vulnerabilities from merge - Remove unused tree-sitter methods in semantic_analyzer.py that caused F821 undefined name errors - Fix regex injection vulnerability in bump-version.js by properly escaping all regex special characters - Add escapeRegex() function to prevent security issues when version string is used in RegExp constructor Resolves ruff linting failures and CodeQL security alerts. * Fix code formatting for ruff compliance Apply formatting fixes to meet line length requirements: - context/builder.py: Split long line with array slicing - planner_lib/context.py: Split long ternary expression - spec/requirements.py: Split long tempfile.NamedTemporaryFile call Resolves ruff format check failures. * Fix missing UTF-8 encoding in init.py gitignore operations Found by pre-commit hook testing in PR #795: - Line 96: Path.read_text() without encoding - Line 122: Path.write_text() without encoding These handle .gitignore file operations and could fail on Windows with special characters in gitignore comments or entries. Total fixes in PR #782: 253 instances (was 251, +2 from init.py) * Add pre-commit hook for UTF-8 encoding enforcement 1. Encoding Check Script (scripts/check_encoding.py): - Validates all file operations have encoding="utf-8" - Checks open(), Path.read_text(), Path.write_text() - Checks json.load/dump with open() - Allows binary mode without encoding - Windows-compatible emoji output with UTF-8 reconfiguration 2. Pre-commit Config (.pre-commit-config.yaml): - Added check-file-encoding hook for apps/backend/ - Runs automatically before commits - Scoped to backend Python files only 3. Tests (tests/test_check_encoding.py): - Comprehensive test coverage (10 tests, all passing) - Tests detection of missing encoding - Tests allowlist for binary files - Tests multiple issues in single file - Tests file type filtering Purpose: - Prevent regression of 251 UTF-8 encoding fixes from PR #782 - Catch missing encoding in new code during development - Fast feedback loop for developers Implementation Notes: - Hook scoped to apps/backend/ to avoid false positives in test code - Uses simple regex matching for speed - Compatible with existing pre-commit infrastructure - Already caught 6 real issues in apps/backend/core/progress.py Related: PR #782 - Fix Windows UTF-8 encoding errors * Address CodeRabbit and Gemini review feedback Fixes based on automated review comments: 1. Binary Mode Detection (Critical Fix): - Replaced brittle regex with robust pattern: r'["'][rwax+]*b[rwax+]*["']' - Now correctly detects all binary modes: rb, wb, ab, r+b, w+b, etc. - Prevents false positives on text mode 'w' without 'b' - Added comprehensive tests for wb, ab, and text w modes 2. Encoding Detection Robustness (Critical Fix): - Changed from 'encoding=' string match to word boundary regex: r'\bencoding\s*=' - Now handles encoding with spaces: encoding = "utf-8" - Prevents false matches of substrings containing 'encoding=' - Applied across all checks (open, read_text, write_text, json.load, json.dump) - Added test for spaces around equals sign 3. Test Coverage Improvements: - Added json.dump() with encoding test (passing case) - Added json.dump() without encoding test (failing case) - Fixed test assertions to match actual behavior (== 1 not == 2) - Added 6 new tests for improved binary/text mode coverage - Total tests increased from 10 to 16, all passing ✅ 4. Code Cleanup: - Removed unused pytest import (CodeQL warning) - Simplified check_files() to remove unused variable tracking All changes validated with comprehensive test suite (16/16 passing). Related: PR #795 review feedback from CodeRabbit and Gemini Code Assist * docs: Add UTF-8 encoding guidelines and Windows development guide 1. CONTRIBUTING.md: - Added concise file encoding section after Code Style - DO/DON'T examples for common file operations - Covers open(), Path methods, json operations - References PR #782 and windows-development.md 2. guides/windows-development.md (NEW): - Comprehensive Windows development guide - File encoding (cp1252 vs UTF-8 issue) - Line endings, path separators, shell commands - Development environment recommendations - Common pitfalls and solutions - Testing guidelines 3. .github/PULL_REQUEST_TEMPLATE.md: - Added encoding checklist item for Python PRs - Helps catch missing encoding during review 4. guides/README.md: - Added windows-development.md to guide index - Organized with CLI-USAGE and linux guides Purpose: Educate developers about UTF-8 encoding requirements to prevent regressions of the 251 encoding issues fixed in PR #782. Automated checking via pre-commit hooks (PR #795) + developer education ensures long-term Windows compatibility. Related: - PR #782: Fix Windows UTF-8 encoding errors (251 instances) - PR #795: Add pre-commit hooks for encoding enforcement * Address review comments from CodeRabbit and Gemini 1. Fix CONTRIBUTING.md markdown linting issues - Add blank lines around code blocks (MD031) - Add JSON write example with ensure_ascii=False (Gemini suggestion) 2. Fix guides/windows-development.md markdown linting (39 violations) - Rename duplicate headings: "The Problem"/"The Solution" → "Problem"/"Solution" (MD024) - Add blank lines around all code blocks (MD031) - Add language specifiers to code blocks (MD040) - Add blank lines before/after headings (MD022) - Wrap long lines to <=80 characters (MD013) - Add blank line before list (MD032) - Use Gemini's idiomatic line ending normalization pattern 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Fix additional UTF-8 encoding issues and improve encoding check script - Add encoding="utf-8" to 5 files that were missing it: - cli/workspace_commands.py: read_text for worktree config - context/pattern_discovery.py: read_text with errors param - context/search.py: read_text with errors param - core/sentry.py: open for package.json version detection - core/workspace/setup.py: open for security profile JSON - Improve check_encoding.py script to reduce false positives: - Use negative lookbehind to exclude os.open(), urlopen(), etc. - Handle nested parentheses correctly when checking args - Skip self.method.read_text() calls (custom methods, not Path) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Fix missing UTF-8 encoding in locked_write() function Add encoding parameter to locked_write() async context manager and use it in os.fdopen() call. This fixes HIGH priority issue from PR review where locked_write() was missing UTF-8 encoding support, which could cause encoding errors on Windows when writing files with non-ASCII content. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Add UnicodeDecodeError handling for file loading resilience Address CodeRabbit review feedback: - runner.py: Add UnicodeDecodeError to exception handling when loading batch files - trust.py: Add exception handling in get_state() and get_all_states() to gracefully handle corrupted state files instead of failing completely Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Fix atomic_write to handle binary mode correctly The atomic_write function was unconditionally passing encoding to os.fdopen, which would crash with ValueError if called with binary mode (e.g., 'wb'). Apply the same fix used in locked_write: only pass encoding for text modes. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Fix run_git() call with invalid parameters in setup.py Remove capture_output and encoding kwargs from run_git() call - these parameters are already handled internally by run_git() and passing them causes TypeError since the function doesn't accept them. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Fix CodeQL warnings and potential double-newline bug - Remove unused is_path_call variables in check_encoding.py - Remove unused failed_count variable in check_encoding.py - Remove unused escapeRegex function in bump-version.js - Fix potential double-newline when adding imports in file_merger.py (strip trailing newlines from content_after before inserting) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Fix Ruff formatting: wrap long line in file_merger.py Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Add UnicodeDecodeError handling to all JSON file loading Comprehensively add UnicodeDecodeError to exception handlers across the codebase to handle legacy-encoded or corrupted files gracefully: - 32+ locations now catch UnicodeDecodeError alongside OSError and json.JSONDecodeError - context/builder.py: Regenerate index on decode failure - planner_lib/context.py: Use empty dicts on decode failure - check_encoding.py: Handle OSError for unreadable files - cleanup.py: Handle decode errors in index pruning This ensures the codebase is robust against non-UTF-8 files that may exist from previous Windows runs with cp1252 encoding. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Add explanatory comments to empty except clauses Address CodeQL notices about empty except clauses with just 'pass' by adding explanatory comments describing the intent. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Fix review issues from Andy's Auto Claude PR Review 1. [HIGH] Fix double-close bug in trust.py:449 - Remove try/except around os.fdopen since it takes ownership of fd - The with statement handles closing, no need for explicit os.close() 2. [LOW] Fix dead code in file_merger.py:87,159 - Simplify endswith check to just '\n' since content is already normalized to LF at that point 3. [LOW] Fix escaped backslash-n in test_context_gatherer.py:150 - Change "\n" (literal backslash-n) to "\n" (actual newline) 4. [LOW] Fix coder.md examples missing encoding parameter - Add encoding="utf-8" to read_text() and open() calls in examples Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: TamerineSky <TamerineSky@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
350 lines
11 KiB
Python
350 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Debug Logging Utility
|
|
=====================
|
|
|
|
Centralized debug logging for the Auto-Claude framework.
|
|
Controlled via environment variables:
|
|
- DEBUG=true Enable debug mode
|
|
- DEBUG_LEVEL=1|2|3 Log verbosity (1=basic, 2=detailed, 3=verbose)
|
|
- DEBUG_LOG_FILE=path Optional file output
|
|
|
|
Usage:
|
|
from debug import debug, debug_detailed, debug_verbose, is_debug_enabled
|
|
|
|
debug("run.py", "Starting task execution", task_id="001")
|
|
debug_detailed("agent", "Agent response received", response_length=1234)
|
|
debug_verbose("client", "Full request payload", payload=data)
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import time
|
|
from datetime import datetime
|
|
from functools import wraps
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
# ANSI color codes for terminal output
|
|
class Colors:
|
|
RESET = "\033[0m"
|
|
BOLD = "\033[1m"
|
|
DIM = "\033[2m"
|
|
|
|
# Debug colors
|
|
DEBUG = "\033[36m" # Cyan
|
|
DEBUG_DIM = "\033[96m" # Light cyan
|
|
TIMESTAMP = "\033[90m" # Gray
|
|
MODULE = "\033[33m" # Yellow
|
|
KEY = "\033[35m" # Magenta
|
|
VALUE = "\033[37m" # White
|
|
SUCCESS = "\033[32m" # Green
|
|
WARNING = "\033[33m" # Yellow
|
|
ERROR = "\033[31m" # Red
|
|
|
|
|
|
def _get_debug_enabled() -> bool:
|
|
"""Check if debug mode is enabled via environment variable."""
|
|
return os.environ.get("DEBUG", "").lower() in ("true", "1", "yes", "on")
|
|
|
|
|
|
def _get_debug_level() -> int:
|
|
"""Get debug verbosity level (1-3)."""
|
|
try:
|
|
level = int(os.environ.get("DEBUG_LEVEL", "1"))
|
|
return max(1, min(3, level)) # Clamp to 1-3
|
|
except ValueError:
|
|
return 1
|
|
|
|
|
|
def _get_log_file() -> Path | None:
|
|
"""Get optional log file path."""
|
|
log_file = os.environ.get("DEBUG_LOG_FILE")
|
|
if log_file:
|
|
return Path(log_file)
|
|
return None
|
|
|
|
|
|
def is_debug_enabled() -> bool:
|
|
"""Check if debug mode is enabled."""
|
|
return _get_debug_enabled()
|
|
|
|
|
|
def get_debug_level() -> int:
|
|
"""Get current debug level."""
|
|
return _get_debug_level()
|
|
|
|
|
|
def _format_value(value: Any, max_length: int = 200) -> str:
|
|
"""Format a value for debug output, truncating if necessary."""
|
|
if value is None:
|
|
return "None"
|
|
|
|
if isinstance(value, (dict, list)):
|
|
try:
|
|
formatted = json.dumps(value, indent=2, default=str)
|
|
if len(formatted) > max_length:
|
|
formatted = formatted[:max_length] + "..."
|
|
return formatted
|
|
except (TypeError, ValueError):
|
|
return str(value)[:max_length]
|
|
|
|
str_value = str(value)
|
|
if len(str_value) > max_length:
|
|
return str_value[:max_length] + "..."
|
|
return str_value
|
|
|
|
|
|
def _write_log(message: str, to_file: bool = True) -> None:
|
|
"""Write log message to stdout and optionally to file."""
|
|
print(message, file=sys.stderr)
|
|
|
|
if to_file:
|
|
log_file = _get_log_file()
|
|
if log_file:
|
|
try:
|
|
log_file.parent.mkdir(parents=True, exist_ok=True)
|
|
# Strip ANSI codes for file output
|
|
import re
|
|
|
|
clean_message = re.sub(r"\033\[[0-9;]*m", "", message)
|
|
with open(log_file, "a", encoding="utf-8") as f:
|
|
f.write(clean_message + "\n")
|
|
except Exception:
|
|
pass # Silently fail file logging
|
|
|
|
|
|
def debug(module: str, message: str, level: int = 1, **kwargs) -> None:
|
|
"""
|
|
Log a debug message.
|
|
|
|
Args:
|
|
module: Source module name (e.g., "run.py", "ideation_runner")
|
|
message: Debug message
|
|
level: Required debug level (1=basic, 2=detailed, 3=verbose)
|
|
**kwargs: Additional key-value pairs to log
|
|
"""
|
|
if not _get_debug_enabled():
|
|
return
|
|
|
|
if _get_debug_level() < level:
|
|
return
|
|
|
|
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
|
|
|
|
# Build the log line
|
|
parts = [
|
|
f"{Colors.TIMESTAMP}[{timestamp}]{Colors.RESET}",
|
|
f"{Colors.DEBUG}[DEBUG]{Colors.RESET}",
|
|
f"{Colors.MODULE}[{module}]{Colors.RESET}",
|
|
f"{Colors.DEBUG_DIM}{message}{Colors.RESET}",
|
|
]
|
|
|
|
log_line = " ".join(parts)
|
|
|
|
# Add kwargs on separate lines if present
|
|
if kwargs:
|
|
for key, value in kwargs.items():
|
|
formatted_value = _format_value(value)
|
|
if "\n" in formatted_value:
|
|
# Multi-line value
|
|
log_line += f"\n {Colors.KEY}{key}{Colors.RESET}:"
|
|
for line in formatted_value.split("\n"):
|
|
log_line += f"\n {Colors.VALUE}{line}{Colors.RESET}"
|
|
else:
|
|
log_line += f"\n {Colors.KEY}{key}{Colors.RESET}: {Colors.VALUE}{formatted_value}{Colors.RESET}"
|
|
|
|
_write_log(log_line)
|
|
|
|
|
|
def debug_detailed(module: str, message: str, **kwargs) -> None:
|
|
"""Log a detailed debug message (level 2)."""
|
|
debug(module, message, level=2, **kwargs)
|
|
|
|
|
|
def debug_verbose(module: str, message: str, **kwargs) -> None:
|
|
"""Log a verbose debug message (level 3)."""
|
|
debug(module, message, level=3, **kwargs)
|
|
|
|
|
|
def debug_success(module: str, message: str, **kwargs) -> None:
|
|
"""Log a success debug message."""
|
|
if not _get_debug_enabled():
|
|
return
|
|
|
|
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
|
|
log_line = f"{Colors.TIMESTAMP}[{timestamp}]{Colors.RESET} {Colors.SUCCESS}[OK]{Colors.RESET} {Colors.MODULE}[{module}]{Colors.RESET} {message}"
|
|
|
|
if kwargs:
|
|
for key, value in kwargs.items():
|
|
log_line += f"\n {Colors.KEY}{key}{Colors.RESET}: {Colors.VALUE}{_format_value(value)}{Colors.RESET}"
|
|
|
|
_write_log(log_line)
|
|
|
|
|
|
def debug_info(module: str, message: str, **kwargs) -> None:
|
|
"""Log an info debug message."""
|
|
if not _get_debug_enabled():
|
|
return
|
|
|
|
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
|
|
log_line = f"{Colors.TIMESTAMP}[{timestamp}]{Colors.RESET} {Colors.DEBUG}[INFO]{Colors.RESET} {Colors.MODULE}[{module}]{Colors.RESET} {message}"
|
|
|
|
if kwargs:
|
|
for key, value in kwargs.items():
|
|
log_line += f"\n {Colors.KEY}{key}{Colors.RESET}: {Colors.VALUE}{_format_value(value)}{Colors.RESET}"
|
|
|
|
_write_log(log_line)
|
|
|
|
|
|
def debug_error(module: str, message: str, **kwargs) -> None:
|
|
"""Log an error debug message (always shown if debug enabled)."""
|
|
if not _get_debug_enabled():
|
|
return
|
|
|
|
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
|
|
log_line = f"{Colors.TIMESTAMP}[{timestamp}]{Colors.RESET} {Colors.ERROR}[ERROR]{Colors.RESET} {Colors.MODULE}[{module}]{Colors.RESET} {Colors.ERROR}{message}{Colors.RESET}"
|
|
|
|
if kwargs:
|
|
for key, value in kwargs.items():
|
|
log_line += f"\n {Colors.KEY}{key}{Colors.RESET}: {Colors.VALUE}{_format_value(value)}{Colors.RESET}"
|
|
|
|
_write_log(log_line)
|
|
|
|
|
|
def debug_warning(module: str, message: str, **kwargs) -> None:
|
|
"""Log a warning debug message."""
|
|
if not _get_debug_enabled():
|
|
return
|
|
|
|
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
|
|
log_line = f"{Colors.TIMESTAMP}[{timestamp}]{Colors.RESET} {Colors.WARNING}[WARN]{Colors.RESET} {Colors.MODULE}[{module}]{Colors.RESET} {Colors.WARNING}{message}{Colors.RESET}"
|
|
|
|
if kwargs:
|
|
for key, value in kwargs.items():
|
|
log_line += f"\n {Colors.KEY}{key}{Colors.RESET}: {Colors.VALUE}{_format_value(value)}{Colors.RESET}"
|
|
|
|
_write_log(log_line)
|
|
|
|
|
|
def debug_section(module: str, title: str) -> None:
|
|
"""Log a section header for organizing debug output."""
|
|
if not _get_debug_enabled():
|
|
return
|
|
|
|
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
|
|
separator = "─" * 60
|
|
log_line = f"\n{Colors.TIMESTAMP}[{timestamp}]{Colors.RESET} {Colors.DEBUG}{Colors.BOLD}┌{separator}┐{Colors.RESET}"
|
|
log_line += f"\n{Colors.TIMESTAMP} {Colors.RESET} {Colors.DEBUG}{Colors.BOLD}│ {module}: {title}{' ' * (58 - len(module) - len(title) - 2)}│{Colors.RESET}"
|
|
log_line += f"\n{Colors.TIMESTAMP} {Colors.RESET} {Colors.DEBUG}{Colors.BOLD}└{separator}┘{Colors.RESET}"
|
|
|
|
_write_log(log_line)
|
|
|
|
|
|
def debug_timer(module: str):
|
|
"""
|
|
Decorator to time function execution.
|
|
|
|
Usage:
|
|
@debug_timer("run.py")
|
|
def my_function():
|
|
...
|
|
"""
|
|
|
|
def decorator(func):
|
|
@wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
if not _get_debug_enabled():
|
|
return func(*args, **kwargs)
|
|
|
|
start = time.time()
|
|
debug_detailed(module, f"Starting {func.__name__}()")
|
|
|
|
try:
|
|
result = func(*args, **kwargs)
|
|
elapsed = time.time() - start
|
|
debug_success(
|
|
module,
|
|
f"Completed {func.__name__}()",
|
|
elapsed_ms=f"{elapsed * 1000:.1f}ms",
|
|
)
|
|
return result
|
|
except Exception as e:
|
|
elapsed = time.time() - start
|
|
debug_error(
|
|
module,
|
|
f"Failed {func.__name__}()",
|
|
error=str(e),
|
|
elapsed_ms=f"{elapsed * 1000:.1f}ms",
|
|
)
|
|
raise
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
def debug_async_timer(module: str):
|
|
"""
|
|
Decorator to time async function execution.
|
|
|
|
Usage:
|
|
@debug_async_timer("ideation_runner")
|
|
async def my_async_function():
|
|
...
|
|
"""
|
|
|
|
def decorator(func):
|
|
@wraps(func)
|
|
async def wrapper(*args, **kwargs):
|
|
if not _get_debug_enabled():
|
|
return await func(*args, **kwargs)
|
|
|
|
start = time.time()
|
|
debug_detailed(module, f"Starting {func.__name__}()")
|
|
|
|
try:
|
|
result = await func(*args, **kwargs)
|
|
elapsed = time.time() - start
|
|
debug_success(
|
|
module,
|
|
f"Completed {func.__name__}()",
|
|
elapsed_ms=f"{elapsed * 1000:.1f}ms",
|
|
)
|
|
return result
|
|
except Exception as e:
|
|
elapsed = time.time() - start
|
|
debug_error(
|
|
module,
|
|
f"Failed {func.__name__}()",
|
|
error=str(e),
|
|
elapsed_ms=f"{elapsed * 1000:.1f}ms",
|
|
)
|
|
raise
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
def debug_env_status() -> None:
|
|
"""Print debug environment status on startup."""
|
|
if not _get_debug_enabled():
|
|
return
|
|
|
|
debug_section("debug", "Debug Mode Enabled")
|
|
debug(
|
|
"debug",
|
|
"Environment configuration",
|
|
DEBUG=os.environ.get("DEBUG", "not set"),
|
|
DEBUG_LEVEL=_get_debug_level(),
|
|
DEBUG_LOG_FILE=os.environ.get("DEBUG_LOG_FILE", "not set"),
|
|
)
|
|
|
|
|
|
# Print status on import if debug is enabled
|
|
if _get_debug_enabled():
|
|
debug_env_status()
|