From db1a2c40419f7137c7e5c64f6c3323cb65c6c4cf Mon Sep 17 00:00:00 2001 From: Vindaar Date: Tue, 9 Oct 2018 18:25:55 +0200 Subject: [PATCH] allow echoing internal stacktrace if compiling with `d:debug` `internalError` and `yAssert` now allow to echo the internal stack traces if the `d:debug` compile flag is set. --- README.md | 2 ++ yaml/private/internal.nim | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0545ecb..e45bb77 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ nim build # build a library NimYAML needs at least Nim 0.17.0 in order to compile. Version 0.9.1 is the last release to support 0.15.x and 0.16.0. +When debugging crashes in this library, use the `d:debug` compile flag to enable printing of the internal stack traces for calls to `internalError` and `yAssert`. + ## License [MIT][2] diff --git a/yaml/private/internal.nim b/yaml/private/internal.nim index d4b85e2..372da2e 100644 --- a/yaml/private/internal.nim +++ b/yaml/private/internal.nim @@ -5,16 +5,24 @@ # distribution, for details about the copyright. template internalError*(s: string) = + # Note: to get the internal stacktrace that caused the error + # compile with the `d:debug` flag. when not defined(release): let ii = instantiationInfo() echo "[NimYAML] Error in file ", ii.filename, " at line ", ii.line, ":" echo s when not defined(JS): echo "[NimYAML] Stacktrace:" - try: writeStackTrace() + try: + writeStackTrace() + when defined(debug): + echo "Internal stacktrace:" + let exc = getCurrentException() + echo getStackTrace(exc.parent) except: discard echo "[NimYAML] Please report this bug." quit 1 + template yAssert*(e: typed) = when not defined(release): if not e: @@ -23,7 +31,12 @@ template yAssert*(e: typed) = echo "assertion failed!" when not defined(JS): echo "[NimYAML] Stacktrace:" - try: writeStackTrace() + try: + writeStackTrace() + when defined(debug): + echo "Internal stacktrace:" + let exc = getCurrentException() + echo getStackTrace(exc.parent) except: discard echo "[NimYAML] Please report this bug." quit 1