Coverage for src/workstack/cli/debug.py: 67%

12 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-19 09:31 -0400

1"""Debug logging utilities for workstack. 

2 

3Provides debug logging functionality when WORKSTACK_DEBUG=1 is set. 

4Debug logs are written to /tmp/workstack-debug.log for troubleshooting. 

5""" 

6 

7import os 

8from datetime import datetime 

9from pathlib import Path 

10 

11 

12def is_debug() -> bool: 

13 """Check if debug mode is enabled via WORKSTACK_DEBUG environment variable.""" 

14 return os.getenv("WORKSTACK_DEBUG") == "1" 

15 

16 

17def debug_log(message: str) -> None: 

18 """Write a timestamped debug message to the debug log file. 

19 

20 Only writes if WORKSTACK_DEBUG=1 is set in the environment. 

21 Logs are appended to /tmp/workstack-debug.log. 

22 

23 Args: 

24 message: The debug message to log 

25 """ 

26 if not is_debug(): 

27 return 

28 

29 log_file = Path("/tmp/workstack-debug.log") 

30 timestamp = datetime.now().isoformat() 

31 

32 with log_file.open("a", encoding="utf-8") as f: 

33 f.write(f"[{timestamp}] {message}\n")