feat: improve log stack trace

* Remove calls to log functions in the stack trace
* Log to stdout for `logDebug`
* Log to stderr for `logError`
This commit is contained in:
Romain Failliot 2023-04-07 10:52:50 -04:00
parent 1dd1afe10b
commit cfa2353e1a
2 changed files with 16 additions and 4 deletions

View File

@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased ## 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 ## 0.8.0 - 2023-04-03
### Added ### Added

View File

@ -17,6 +17,7 @@
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import inspect
import os import os
import sys import sys
import locale import locale
@ -82,11 +83,15 @@ def isWindows() -> bool:
return os.name == 'nt' return os.name == 'nt'
def _logPrintOutput(msg: str) -> None: def _logPrintOutput(msg: str, file: Optional[TextIO] = None) -> None:
if theResources.getOptionAsBool('log_print_output'): if theResources.getOptionAsBool('log_print_output'):
print(msg, file=sys.stderr) print(msg, file=file)
if theResources.getOptionAsBool('log_print_stack'): 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: def logDebug(msg: str) -> None:
@ -96,7 +101,7 @@ def logDebug(msg: str) -> None:
def logError(msg: str) -> None: def logError(msg: str) -> None:
'''Report error message.''' '''Report error message.'''
_logPrintOutput(f'ERROR: {msg}') _logPrintOutput(f'ERROR: {msg}', sys.stderr)
def logErrorAndDialog(msg: str, parent: Gtk.Widget = None) -> None: def logErrorAndDialog(msg: str, parent: Gtk.Widget = None) -> None: