Skip to content

Commit 61f65fb

Browse files
authored
Merge pull request #197 from MightyCreak/better-log-stacktrace
feat: improve log stack trace
2 parents 1dd1afe + 4e1ba5c commit 61f65fb

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

.mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[mypy]
22
warn_unused_ignores = True
33
disallow_incomplete_defs = True
4+
disable_error_code = annotation-unchecked

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Changed
11+
12+
- Remove log function calls from the stack trace when calling `logDebug` or
13+
`logError` (@MightyCreak)
14+
- Log functions used to log only on stderr, now `logDebug` logs on stdout and
15+
`logError` logs on stderr (@MightyCreak)
16+
1017
## 0.8.0 - 2023-04-03
1118

1219
### Added

src/diffuse/utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# with this program; if not, write to the Free Software Foundation, Inc.,
1818
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919

20+
import inspect
2021
import os
2122
import sys
2223
import locale
@@ -82,11 +83,15 @@ def isWindows() -> bool:
8283
return os.name == 'nt'
8384

8485

85-
def _logPrintOutput(msg: str) -> None:
86+
def _logPrintOutput(msg: str, file: Optional[TextIO] = None) -> None:
8687
if theResources.getOptionAsBool('log_print_output'):
87-
print(msg, file=sys.stderr)
88+
print(msg, file=file)
8889
if theResources.getOptionAsBool('log_print_stack'):
89-
traceback.print_stack()
90+
# Show the stack trace, but remove the past 2 calls as it is just log functions noise
91+
frames = inspect.stack()
92+
frameIdx = min(2, len(frames) - 1)
93+
print('Traceback (most recent call last):', file=file)
94+
traceback.print_stack(frames[frameIdx].frame, file=file)
9095

9196

9297
def logDebug(msg: str) -> None:
@@ -96,7 +101,7 @@ def logDebug(msg: str) -> None:
96101

97102
def logError(msg: str) -> None:
98103
'''Report error message.'''
99-
_logPrintOutput(f'ERROR: {msg}')
104+
_logPrintOutput(f'ERROR: {msg}', sys.stderr)
100105

101106

102107
def logErrorAndDialog(msg: str, parent: Gtk.Widget = None) -> None:

0 commit comments

Comments
 (0)