Get the current execution location in format 'module.class.function'. Args: skip_frames: Number of stack frames to skip (default 2 to skip this function and its caller) Returns: String in format 'module.class.function' or 'module.function' if not in a class
(skip_frames: int = 2)
| 67 | |
| 68 | |
| 69 | def get_current_location(skip_frames: int = 2) -> str: |
| 70 | """ |
| 71 | Get the current execution location in format 'module.class.function'. |
| 72 | |
| 73 | Args: |
| 74 | skip_frames: Number of stack frames to skip (default 2 to skip this function and its caller) |
| 75 | |
| 76 | Returns: |
| 77 | String in format 'module.class.function' or 'module.function' if not in a class |
| 78 | """ |
| 79 | stack = inspect.stack() |
| 80 | if len(stack) <= skip_frames: |
| 81 | return "unknown" |
| 82 | |
| 83 | frame = stack[skip_frames] |
| 84 | module_name = frame.frame.f_globals.get('__name__', 'unknown') |
| 85 | function_name = frame.function |
| 86 | |
| 87 | # Try to determine if we're in a class method |
| 88 | class_name = None |
| 89 | if 'self' in frame.frame.f_locals: |
| 90 | # This is likely an instance method |
| 91 | obj = frame.frame.f_locals['self'] |
| 92 | class_name = obj.__class__.__name__ |
| 93 | elif 'cls' in frame.frame.f_locals: |
| 94 | # This might be a class method |
| 95 | cls = frame.frame.f_locals['cls'] |
| 96 | if inspect.isclass(cls): |
| 97 | class_name = cls.__name__ |
| 98 | |
| 99 | # Build the location string |
| 100 | if class_name: |
| 101 | return f"{module_name}.{class_name}.{function_name}" |
| 102 | else: |
| 103 | return f"{module_name}.{function_name}" |
| 104 | |
| 105 | |
| 106 | def logger_debug(message, |