Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2e7882bfa | ||
|
|
4f9c2b9d5f |
@@ -1,7 +1,7 @@
|
|||||||
# MARK: Project info
|
# MARK: Project info
|
||||||
[project]
|
[project]
|
||||||
name = "corelibs"
|
name = "corelibs"
|
||||||
version = "0.19.1"
|
version = "0.20.0"
|
||||||
description = "Collection of utils for Python scripts"
|
description = "Collection of utils for Python scripts"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.13"
|
requires-python = ">=3.13"
|
||||||
|
|||||||
@@ -4,7 +4,12 @@ Various debug helpers
|
|||||||
|
|
||||||
import traceback
|
import traceback
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
from typing import Tuple, Type
|
||||||
|
from types import TracebackType
|
||||||
|
|
||||||
|
# _typeshed.OptExcInfo
|
||||||
|
OptExcInfo = Tuple[None, None, None] | Tuple[Type[BaseException], BaseException, TracebackType]
|
||||||
|
|
||||||
def call_stack(
|
def call_stack(
|
||||||
start: int = 0,
|
start: int = 0,
|
||||||
@@ -41,4 +46,30 @@ def call_stack(
|
|||||||
# print(f"* HERE: {dump_data(stack)}")
|
# print(f"* HERE: {dump_data(stack)}")
|
||||||
return f"{separator}".join(f"{os.path.basename(f.filename)}:{f.name}:{f.lineno}" for f in __stack)
|
return f"{separator}".join(f"{os.path.basename(f.filename)}:{f.name}:{f.lineno}" for f in __stack)
|
||||||
|
|
||||||
|
|
||||||
|
def exception_stack(
|
||||||
|
exc_stack: OptExcInfo | None = None,
|
||||||
|
separator: str = ' -> '
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Exception traceback, if no sys.exc_info is set, run internal
|
||||||
|
|
||||||
|
Keyword Arguments:
|
||||||
|
exc_stack {OptExcInfo | None} -- _description_ (default: {None})
|
||||||
|
separator {str} -- _description_ (default: {' -> '})
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str -- _description_
|
||||||
|
"""
|
||||||
|
if exc_stack is not None:
|
||||||
|
_, _, exc_traceback = exc_stack
|
||||||
|
else:
|
||||||
|
exc_traceback = None
|
||||||
|
_, _, exc_traceback = sys.exc_info()
|
||||||
|
stack = traceback.extract_tb(exc_traceback)
|
||||||
|
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__
|
# __END__
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from pathlib import Path
|
|||||||
from typing import MutableMapping, TextIO, TypedDict, Any, TYPE_CHECKING, cast
|
from typing import MutableMapping, TextIO, TypedDict, Any, TYPE_CHECKING, cast
|
||||||
from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel
|
from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel
|
||||||
from corelibs.string_handling.text_colors import Colors
|
from corelibs.string_handling.text_colors import Colors
|
||||||
from corelibs.debug_handling.debug_helpers import call_stack
|
from corelibs.debug_handling.debug_helpers import call_stack, exception_stack
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from multiprocessing import Queue
|
from multiprocessing import Queue
|
||||||
@@ -225,11 +225,12 @@ class LogParent:
|
|||||||
if extra is None:
|
if extra is None:
|
||||||
extra = {}
|
extra = {}
|
||||||
extra['stack_trace'] = call_stack(skip_last=2)
|
extra['stack_trace'] = call_stack(skip_last=2)
|
||||||
|
extra['exception_trace'] = exception_stack()
|
||||||
# write to console first with extra flag for filtering in file
|
# write to console first with extra flag for filtering in file
|
||||||
if log_error:
|
if log_error:
|
||||||
self.logger.log(
|
self.logger.log(
|
||||||
LoggingLevel.ERROR.value,
|
LoggingLevel.ERROR.value,
|
||||||
f"<=EXCEPTION> {msg} [{extra['stack_trace']}]",
|
f"<=EXCEPTION={extra['exception_trace']}> {msg} [{extra['stack_trace']}]",
|
||||||
*args, extra=dict(extra) | {'console': True}, stacklevel=2
|
*args, extra=dict(extra) | {'console': True}, stacklevel=2
|
||||||
)
|
)
|
||||||
self.logger.log(LoggingLevel.EXCEPTION.value, msg, *args, exc_info=True, extra=extra, stacklevel=2)
|
self.logger.log(LoggingLevel.EXCEPTION.value, msg, *args, exc_info=True, extra=extra, stacklevel=2)
|
||||||
|
|||||||
@@ -3,9 +3,11 @@ Log logging_handling.log testing
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# import atexit
|
# import atexit
|
||||||
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
# this is for testing only
|
# this is for testing only
|
||||||
from corelibs.logging_handling.log import Log, Logger
|
from corelibs.logging_handling.log import Log, Logger
|
||||||
|
from corelibs.debug_handling.debug_helpers import exception_stack, call_stack
|
||||||
from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel
|
from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel
|
||||||
|
|
||||||
|
|
||||||
@@ -78,6 +80,8 @@ def main():
|
|||||||
__test = 5 / 0
|
__test = 5 / 0
|
||||||
print(f"Divied: {__test}")
|
print(f"Divied: {__test}")
|
||||||
except ZeroDivisionError as e:
|
except ZeroDivisionError as e:
|
||||||
|
print(f"** sys.exec_info(): {sys.exc_info()}")
|
||||||
|
print(f"** sys.exec_info(): [{exception_stack()}] | [{exception_stack(sys.exc_info())}] | [{call_stack()}]")
|
||||||
log.logger.critical("Divison through zero: %s", e)
|
log.logger.critical("Divison through zero: %s", e)
|
||||||
log.exception("Divison through zero: %s", e)
|
log.exception("Divison through zero: %s", e)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user