1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 00:10:27 +00:00

Add extra "prefix" parameter to debug/stacktrace.

This commit is contained in:
Calvin Rose 2021-12-04 13:03:05 -06:00
parent 1f8bcadb3b
commit e8c738002b
3 changed files with 23 additions and 21 deletions

View File

@ -2223,7 +2223,7 @@
col
": compile error: ")
(if macrof
(debug/stacktrace macrof msg)
(debug/stacktrace macrof msg "")
(eprint msg))
(when ec
(print-line-col where line col)
@ -2287,12 +2287,13 @@
(default on-compile-warning warn-compile)
(default on-parse-error bad-parse)
(default evaluator (fn evaluate [x &] (x)))
(default default-where "<anonymous>")
(default guard :ydt)
(var where default-where)
(unless (= where "<anonymous>") (put env :current-file where))
(if where
(put env :current-file where)
(set where "<anonymous>"))
# Evaluate 1 source form in a protected manner
(def lints @[])
@ -2659,8 +2660,7 @@
:on-status (fn [f x]
(when (not= (fiber/status f) :dead)
(when exit
(eprint x)
(debug/stacktrace f)
(debug/stacktrace f x "")
(eflush)
(os/exit 1))
(put env :exit true)
@ -3167,7 +3167,7 @@
"Print the current fiber stack"
[]
(print)
(with-dyns [:err-color false] (debug/stacktrace (.fiber) (.signal)))
(with-dyns [:err-color false] (debug/stacktrace (.fiber) (.signal) ""))
(print))
(defn .frame
@ -3364,9 +3364,7 @@
(printf (get e :pretty-format "%q") x)
(flush))
(do
(def ec (dyn :err-color))
(eprint (if ec "\e[31m" "") fs ": " x)
(debug/stacktrace f)
(debug/stacktrace f x "")
(eflush)
(if (e :debug) (enter-debugger f x))))))
@ -3520,8 +3518,7 @@
(try
(dofile path :evaluator flycheck-evaluator ;(kvs kwargs))
([e f]
(eprint e)
(debug/stacktrace f)))
(debug/stacktrace f e "")))
nil)
###

View File

@ -96,15 +96,18 @@ void janet_debug_find(
}
}
void janet_stacktrace(JanetFiber *fiber, Janet err) {
janet_stacktrace_ext(fiber, err, NULL);
}
/* Error reporting. This can be emulated from within Janet, but for
* consitency with the top level code it is defined once. */
void janet_stacktrace(JanetFiber *fiber, Janet err) {
void janet_stacktrace_ext(JanetFiber *fiber, Janet err, const char *prefix) {
int32_t fi;
const char *errstr = (const char *)janet_to_string(err);
JanetFiber **fibers = NULL;
/* Don't print error line if it is nil. */
int wrote_error = janet_checktype(err, JANET_NIL);
int wrote_error = !prefix;
int print_color = janet_truthy(janet_dyn("err-color"));
if (print_color) janet_eprintf("\x1b[31m");
@ -126,9 +129,9 @@ void janet_stacktrace(JanetFiber *fiber, Janet err) {
/* Print prelude to stack frame */
if (!wrote_error) {
JanetFiberStatus status = janet_fiber_status(fiber);
const char *prefix = status == JANET_STATUS_ERROR ? "" : "status ";
const char *override_prefix = prefix ? prefix : "";
janet_eprintf("%s%s: %s\n",
prefix,
override_prefix,
janet_status_names[status],
errstr);
wrote_error = 1;
@ -361,14 +364,15 @@ JANET_CORE_FN(cfun_debug_stack,
}
JANET_CORE_FN(cfun_debug_stacktrace,
"(debug/stacktrace fiber &opt err)",
"(debug/stacktrace fiber &opt err prefix)",
"Prints a nice looking stacktrace for a fiber. Can optionally provide "
"an error value to print the stack trace with. If `err` is nil or not "
"provided, will skip the error line. Returns the fiber.") {
janet_arity(argc, 1, 2);
"provided, and no prefix is given, will skip the error line. Returns the fiber.") {
janet_arity(argc, 1, 3);
JanetFiber *fiber = janet_getfiber(argv, 0);
Janet x = argc == 1 ? janet_wrap_nil() : argv[1];
janet_stacktrace(fiber, x);
const char *prefix = janet_optcstring(argv, argc, 2, NULL);
janet_stacktrace_ext(fiber, x, prefix);
return argv[0];
}

View File

@ -1763,6 +1763,7 @@ JANET_API JanetSignal janet_step(JanetFiber *fiber, Janet in, Janet *out);
JANET_API Janet janet_call(JanetFunction *fun, int32_t argc, const Janet *argv);
JANET_API Janet janet_mcall(const char *name, int32_t argc, Janet *argv);
JANET_API void janet_stacktrace(JanetFiber *fiber, Janet err);
JANET_API void janet_stacktrace_ext(JanetFiber *fiber, Janet err, const char *prefix);
/* Scratch Memory API */
typedef void (*JanetScratchFinalizer)(void *);