Coverage for apio/common/debug_util.py: 92%

20 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-23 03:53 +0000

1# -*- coding: utf-8 -*- 

2# -- This file is part of the Apio project 

3# -- (C) 2016-2018 FPGAwars 

4# -- Author Jesús Arroyo 

5# -- License GPLv2 

6# -- Derived from: 

7# ---- Platformio project 

8# ---- (C) 2014-2016 Ivan Kravets <me@ikravets.com> 

9# ---- License Apache v2 

10"""Misc debug related utilities.""" 

11 

12# -- We keep the dependencies very minimal to make sure it can be 

13# -- used in any context. 

14import sys 

15import os 

16 

17 

18def debug_level() -> int: 

19 """Returns the current debug level, with 0 as 'off'.""" 

20 

21 # -- We get a fresh value so it can be adjusted dynamically when needed. 

22 level_str = os.environ.get("APIO_DEBUG", "0") 

23 

24 # -- For windows benefit, remove optional quotes, same as 

25 # -- env_options.get() does. 

26 if ( 

27 len(level_str) >= 2 

28 and level_str.startswith('"') 

29 and level_str.endswith('"') 

30 ): 

31 level_str = level_str[1:-1] 

32 

33 try: 

34 level_int = int(level_str) 

35 

36 except ValueError: 

37 # -- This module is intentionally not dependent on apio_console so 

38 # -- we use simple print and sys.exit() instead of calling fatal_error. 

39 print(f"Error: env value APIO_DEBUG [{level_str}] is not an int.") 

40 sys.exit(1) 

41 

42 # -- All done. We don't validate the value, assuming the caller 

43 # -- knows how to use it. 

44 return level_int 

45 

46 

47def is_debug(level: int) -> bool: 

48 """Returns True if apio is in debug mode level 'level' or higher. Use 

49 it to enable printing of debug information but not to modify the behavior 

50 of the code. Also, all apio tests should be performed with debug 

51 disabled. Important debug information should be at level 1 while 

52 less important or spammy should be at higher levels.""" 

53 

54 assert isinstance(level, int), type(level) 

55 assert 1 <= level <= 10, level 

56 

57 return debug_level() >= level 

58 

59 

60def is_under_vscode_debugger() -> bool: 

61 """Returns true if running under VSCode debugger.""" 

62 if os.environ.get("DEBUGPY_RUNNING"): 62 ↛ 63line 62 didn't jump to line 63 because the condition on line 62 was never true

63 return True 

64 return False