File size: 1,021 Bytes
a4b70d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
from typing import Callable, List, Optional, Any

logging: bool = False
version_check: bool = True
version: Optional[str] = None
log_handler: Callable[..., None] = print
logs: List[str] = []


def enable_logging(handler: Callable[..., None] = print) -> None:
    """Enable debug logging with optional handler."""
    global logging, log_handler
    logging = True
    log_handler = handler


def disable_logging() -> None:
    """Disable debug logging."""
    global logging
    logging = False


def log(*text: Any, file: Optional[Any] = None) -> None:
    """Log a message if logging is enabled."""
    if logging:
        message = " ".join(map(str, text))
        logs.append(message)
        log_handler(*text, file=file)


def error(*error_args: Any, name: Optional[str] = None) -> None:
    """Log an error message to stderr."""
    formatted_errors = [
        e if isinstance(e, str) else f"{name or type(e).__name__}: {e}"
        for e in error_args
    ]
    log(*formatted_errors, file=sys.stderr)