1
0
mirror of https://github.com/janet-lang/janet synced 2025-06-07 00:54:12 +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 col
": compile error: ") ": compile error: ")
(if macrof (if macrof
(debug/stacktrace macrof msg) (debug/stacktrace macrof msg "")
(eprint msg)) (eprint msg))
(when ec (when ec
(print-line-col where line col) (print-line-col where line col)
@ -2287,12 +2287,13 @@
(default on-compile-warning warn-compile) (default on-compile-warning warn-compile)
(default on-parse-error bad-parse) (default on-parse-error bad-parse)
(default evaluator (fn evaluate [x &] (x))) (default evaluator (fn evaluate [x &] (x)))
(default default-where "<anonymous>")
(default guard :ydt) (default guard :ydt)
(var where default-where) (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 # Evaluate 1 source form in a protected manner
(def lints @[]) (def lints @[])
@ -2659,8 +2660,7 @@
:on-status (fn [f x] :on-status (fn [f x]
(when (not= (fiber/status f) :dead) (when (not= (fiber/status f) :dead)
(when exit (when exit
(eprint x) (debug/stacktrace f x "")
(debug/stacktrace f)
(eflush) (eflush)
(os/exit 1)) (os/exit 1))
(put env :exit true) (put env :exit true)
@ -3167,7 +3167,7 @@
"Print the current fiber stack" "Print the current fiber stack"
[] []
(print) (print)
(with-dyns [:err-color false] (debug/stacktrace (.fiber) (.signal))) (with-dyns [:err-color false] (debug/stacktrace (.fiber) (.signal) ""))
(print)) (print))
(defn .frame (defn .frame
@ -3364,9 +3364,7 @@
(printf (get e :pretty-format "%q") x) (printf (get e :pretty-format "%q") x)
(flush)) (flush))
(do (do
(def ec (dyn :err-color)) (debug/stacktrace f x "")
(eprint (if ec "\e[31m" "") fs ": " x)
(debug/stacktrace f)
(eflush) (eflush)
(if (e :debug) (enter-debugger f x)))))) (if (e :debug) (enter-debugger f x))))))
@ -3520,8 +3518,7 @@
(try (try
(dofile path :evaluator flycheck-evaluator ;(kvs kwargs)) (dofile path :evaluator flycheck-evaluator ;(kvs kwargs))
([e f] ([e f]
(eprint e) (debug/stacktrace f e "")))
(debug/stacktrace f)))
nil) 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 /* Error reporting. This can be emulated from within Janet, but for
* consitency with the top level code it is defined once. */ * 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; int32_t fi;
const char *errstr = (const char *)janet_to_string(err); const char *errstr = (const char *)janet_to_string(err);
JanetFiber **fibers = NULL; JanetFiber **fibers = NULL;
int wrote_error = !prefix;
/* Don't print error line if it is nil. */
int wrote_error = janet_checktype(err, JANET_NIL);
int print_color = janet_truthy(janet_dyn("err-color")); int print_color = janet_truthy(janet_dyn("err-color"));
if (print_color) janet_eprintf("\x1b[31m"); if (print_color) janet_eprintf("\x1b[31m");
@ -126,9 +129,9 @@ void janet_stacktrace(JanetFiber *fiber, Janet err) {
/* Print prelude to stack frame */ /* Print prelude to stack frame */
if (!wrote_error) { if (!wrote_error) {
JanetFiberStatus status = janet_fiber_status(fiber); 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", janet_eprintf("%s%s: %s\n",
prefix, override_prefix,
janet_status_names[status], janet_status_names[status],
errstr); errstr);
wrote_error = 1; wrote_error = 1;
@ -361,14 +364,15 @@ JANET_CORE_FN(cfun_debug_stack,
} }
JANET_CORE_FN(cfun_debug_stacktrace, 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 " "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 " "an error value to print the stack trace with. If `err` is nil or not "
"provided, will skip the error line. Returns the fiber.") { "provided, and no prefix is given, will skip the error line. Returns the fiber.") {
janet_arity(argc, 1, 2); janet_arity(argc, 1, 3);
JanetFiber *fiber = janet_getfiber(argv, 0); JanetFiber *fiber = janet_getfiber(argv, 0);
Janet x = argc == 1 ? janet_wrap_nil() : argv[1]; 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]; 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_call(JanetFunction *fun, int32_t argc, const Janet *argv);
JANET_API Janet janet_mcall(const char *name, int32_t argc, 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(JanetFiber *fiber, Janet err);
JANET_API void janet_stacktrace_ext(JanetFiber *fiber, Janet err, const char *prefix);
/* Scratch Memory API */ /* Scratch Memory API */
typedef void (*JanetScratchFinalizer)(void *); typedef void (*JanetScratchFinalizer)(void *);