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

20 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-07 05:08 +0000

1# This file is part of the Apio project 

2# (C) 2016-present FPGAwars 

3# Project authors: 

4# * Jesús Arroyo (Jesus89) (2016-2019) 

5# * Juan Gonzalez (obijuan) (2019-2024) 

6# * The Apio Team (2024-present) 

7# License: GPLv2 

8 

9"""Misc debug related utilities.""" 

10 

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

12# -- used in any context. 

13import sys 

14import os 

15 

16 

17def debug_level() -> int: 

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

19 

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

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

22 

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

24 # -- env_options.get() does. 

25 if ( 

26 len(level_str) >= 2 

27 and level_str.startswith('"') 

28 and level_str.endswith('"') 

29 ): 

30 level_str = level_str[1:-1] 

31 

32 try: 

33 level_int = int(level_str) 

34 

35 except ValueError: 

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

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

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

39 sys.exit(1) 

40 

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

42 # -- knows how to use it. 

43 return level_int 

44 

45 

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

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

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

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

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

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

52 

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

54 assert 1 <= level <= 10, level 

55 

56 return debug_level() >= level 

57 

58 

59def is_under_vscode_debugger() -> bool: 

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

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

62 return True 

63 return False