From cfa2353e1a4706d4ae10b0cc12d655d2836e3e09 Mon Sep 17 00:00:00 2001 From: Romain Failliot Date: Fri, 7 Apr 2023 10:52:50 -0400 Subject: [PATCH] feat: improve log stack trace * Remove calls to log functions in the stack trace * Log to stdout for `logDebug` * Log to stderr for `logError` --- CHANGELOG.md | 7 +++++++ src/diffuse/utils.py | 13 +++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a90289..7a3af1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Changed + +- Remove log function calls from the stack trace when calling `logDebug` or + `logError` (@MightyCreak) +- Log functions used to log only on stderr, now `logDebug` logs on stdout and + `logError` logs on stderr (@MightyCreak) + ## 0.8.0 - 2023-04-03 ### Added diff --git a/src/diffuse/utils.py b/src/diffuse/utils.py index 63d38af..b4fdc11 100644 --- a/src/diffuse/utils.py +++ b/src/diffuse/utils.py @@ -17,6 +17,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import inspect import os import sys import locale @@ -82,11 +83,15 @@ def isWindows() -> bool: return os.name == 'nt' -def _logPrintOutput(msg: str) -> None: +def _logPrintOutput(msg: str, file: Optional[TextIO] = None) -> None: if theResources.getOptionAsBool('log_print_output'): - print(msg, file=sys.stderr) + print(msg, file=file) if theResources.getOptionAsBool('log_print_stack'): - traceback.print_stack() + # Show the stack trace, but remove the past 2 calls as it is just log functions noise + frames = inspect.stack() + frameIdx = min(2, len(frames) - 1) + print('Traceback (most recent call last):', file=file) + traceback.print_stack(frames[frameIdx].frame, file=file) def logDebug(msg: str) -> None: @@ -96,7 +101,7 @@ def logDebug(msg: str) -> None: def logError(msg: str) -> None: '''Report error message.''' - _logPrintOutput(f'ERROR: {msg}') + _logPrintOutput(f'ERROR: {msg}', sys.stderr) def logErrorAndDialog(msg: str, parent: Gtk.Widget = None) -> None: