From dcefa564da34936f5af6356d7ae392a57bcb136e Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 24 Jul 2025 10:50:17 +0900 Subject: [PATCH] Fix stack stack traceback call It now works correct with start and skip_last settings, the method is now called "call_stack" Also added auto reset if no output (start too hight) and optional stack separator --- src/corelibs/debug_handling/debug_helpers.py | 43 ++++++++++++-------- src/corelibs/logging_handling/log.py | 20 ++++----- uv.lock | 2 +- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/corelibs/debug_handling/debug_helpers.py b/src/corelibs/debug_handling/debug_helpers.py index a9b14fa..27c279d 100644 --- a/src/corelibs/debug_handling/debug_helpers.py +++ b/src/corelibs/debug_handling/debug_helpers.py @@ -6,28 +6,39 @@ import traceback import os -def traceback_call_str(start: int = 2, depth: int = 1): +def call_stack( + start: int = 0, + skip_last: int = -1, + separator: str = ' -> ', + reset_start_if_empty: bool = False +) -> str: """ get the trace for the last entry Keyword Arguments: - start {int} -- _description_ (default: {2}) - depth {int} -- _description_ (default: {1}) + start {int} -- start, if too might output will empty until reset_start_if_empty is set (default: {0}) + skip_last {int} -- how many of the last are skipped, defaults to -1 for current method (default: {-1}) + seperator {str} -- add stack separator, if empty defaults to ' -> ' (default: { -> }) + reset_start_if_empty {bool} -- if no stack returned because of too high start, + reset to 0 for full read (default: {False}) Returns: - _type_ -- _description_ + str -- _description_ """ - # can't have more than in the stack for depth - depth = min(depth, start) - depth = start - depth - # 0 is full stack length from start - if depth == 0: - stack = traceback.extract_stack()[-start:] - else: - stack = traceback.extract_stack()[-start:-depth] - return ' -> '.join( - f"{os.path.basename(f.filename)}:{f.name}:{f.lineno}" - for f in stack - ) + # stack = traceback.extract_stack()[start:depth] + # how many of the last entries we skip (so we do not get self), default is -1 + # start cannot be negative + if skip_last > 0: + skip_last = skip_last * -1 + stack = traceback.extract_stack() + __stack = stack[start:skip_last] + # start possible to high, reset start to 0 + if not __stack and reset_start_if_empty: + start = 0 + __stack = stack[start:skip_last] + if not separator: + separator = ' -> ' + # print(f"* HERE: {dump_data(stack)}") + return f"{separator}".join(f"{os.path.basename(f.filename)}:{f.name}:{f.lineno}" for f in __stack) # __END__ diff --git a/src/corelibs/logging_handling/log.py b/src/corelibs/logging_handling/log.py index 3b16675..bad6faa 100644 --- a/src/corelibs/logging_handling/log.py +++ b/src/corelibs/logging_handling/log.py @@ -12,7 +12,7 @@ from pathlib import Path from typing import MutableMapping, TextIO, TypedDict, Any, TYPE_CHECKING, cast from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel from corelibs.string_handling.text_colors import Colors -from corelibs.debug_handling.debug_helpers import traceback_call_str +from corelibs.debug_handling.debug_helpers import call_stack if TYPE_CHECKING: from multiprocessing import Queue @@ -131,7 +131,7 @@ class LogParent: raise ValueError('Logger is not yet initialized') if extra is None: extra = {} - extra['stack_trace'] = traceback_call_str(start=3) + extra['stack_trace'] = call_stack(skip_last=2) self.logger.log(level, msg, *args, extra=extra, stacklevel=2) # MARK: DEBUG 10 @@ -141,7 +141,7 @@ class LogParent: raise ValueError('Logger is not yet initialized') if extra is None: extra = {} - extra['stack_trace'] = traceback_call_str(start=3) + extra['stack_trace'] = call_stack(skip_last=2) self.logger.debug(msg, *args, extra=extra, stacklevel=2) # MARK: INFO 20 @@ -151,7 +151,7 @@ class LogParent: raise ValueError('Logger is not yet initialized') if extra is None: extra = {} - extra['stack_trace'] = traceback_call_str(start=3) + extra['stack_trace'] = call_stack(skip_last=2) self.logger.info(msg, *args, extra=extra, stacklevel=2) # MARK: WARNING 30 @@ -161,7 +161,7 @@ class LogParent: raise ValueError('Logger is not yet initialized') if extra is None: extra = {} - extra['stack_trace'] = traceback_call_str(start=3) + extra['stack_trace'] = call_stack(skip_last=2) self.logger.warning(msg, *args, extra=extra, stacklevel=2) # MARK: ERROR 40 @@ -171,7 +171,7 @@ class LogParent: raise ValueError('Logger is not yet initialized') if extra is None: extra = {} - extra['stack_trace'] = traceback_call_str(start=3) + extra['stack_trace'] = call_stack(skip_last=2) self.logger.error(msg, *args, extra=extra, stacklevel=2) # MARK: CRITICAL 50 @@ -181,7 +181,7 @@ class LogParent: raise ValueError('Logger is not yet initialized') if extra is None: extra = {} - extra['stack_trace'] = traceback_call_str(start=3) + extra['stack_trace'] = call_stack(skip_last=2) self.logger.critical(msg, *args, extra=extra, stacklevel=2) # MARK: ALERT 55 @@ -192,7 +192,7 @@ class LogParent: # extra_dict = dict(extra) if extra is None: extra = {} - extra['stack_trace'] = traceback_call_str(start=3) + extra['stack_trace'] = call_stack(skip_last=2) self.logger.log(LoggingLevel.ALERT.value, msg, *args, extra=extra, stacklevel=2) # MARK: EMERGECNY: 60 @@ -202,7 +202,7 @@ class LogParent: raise ValueError('Logger is not yet initialized') if extra is None: extra = {} - extra['stack_trace'] = traceback_call_str(start=3) + extra['stack_trace'] = call_stack(skip_last=2) self.logger.log(LoggingLevel.EMERGENCY.value, msg, *args, extra=extra, stacklevel=2) # MARK: EXCEPTION: 70 @@ -224,7 +224,7 @@ class LogParent: raise ValueError('Logger is not yet initialized') if extra is None: extra = {} - extra['stack_trace'] = traceback_call_str(start=3) + extra['stack_trace'] = call_stack(skip_last=2) # write to console first with extra flag for filtering in file if log_error: self.logger.log( diff --git a/uv.lock b/uv.lock index e6a058e..9c6c261 100644 --- a/uv.lock +++ b/uv.lock @@ -44,7 +44,7 @@ wheels = [ [[package]] name = "corelibs" -version = "0.14.1" +version = "0.15.0" source = { editable = "." } dependencies = [ { name = "jmespath" },