debug.py 656 B

12345678910111213141516171819
  1. import sys
  2. from typing import Callable, List, Optional, Any
  3. logging: bool = False
  4. version_check: bool = True
  5. version: Optional[str] = None
  6. log_handler: Callable = print # More specifically: Callable[[Any, Optional[Any]], None]
  7. logs: List[str] = []
  8. def log(*text: Any, file: Optional[Any] = None) -> None:
  9. """Log a message if logging is enabled."""
  10. if logging:
  11. log_handler(*text, file=file)
  12. def error(*error: Any, name: Optional[str] = None) -> None:
  13. """Log an error message to stderr."""
  14. error = [e if isinstance(e, str) else f"{type(e).__name__ if name is None else name}: {e}" for e in error]
  15. log(*error, file=sys.stderr)