Merge pull request #197 from MightyCreak/better-log-stacktrace

feat: improve log stack trace
This commit is contained in:
Creak 2023-04-07 11:34:53 -04:00 committed by GitHub
commit 61f65fb826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View File

@ -1,3 +1,4 @@
[mypy]
warn_unused_ignores = True
disallow_incomplete_defs = True
disable_error_code = annotation-unchecked

View File

@ -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

View File

@ -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: