From 7a0c80293f66d9d0116ceb4c697cf820066d4c7a Mon Sep 17 00:00:00 2001 From: Sorah Fukumori Date: Wed, 29 Nov 2017 17:50:44 +0900 Subject: [PATCH] Log exception with bold and underline for TTYs --- eval_error.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/eval_error.c b/eval_error.c index e8a1691..a94d262 100644 --- a/eval_error.c +++ b/eval_error.c @@ -73,8 +73,11 @@ error_print(rb_execution_context_t *ec) } static void -print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg) +print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, int colored) { + static const char underline[] = "\033[4;1m"; + static const char bold[] = "\033[1m"; + static const char reset[] = "\033[m"; const char *einfo = ""; long elen = 0; VALUE mesg; @@ -88,6 +91,10 @@ print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg) warn_print_str(mesg); warn_print(": "); } + if (colored) { + warn_print(bold); + } + if (!NIL_P(emesg)) { einfo = RSTRING_PTR(emesg); @@ -96,6 +103,9 @@ print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg) } if (eclass == rb_eRuntimeError && elen == 0) { + if (colored) { + warn_print(underline); + } warn_print("unhandled exception\n"); } else { @@ -103,6 +113,7 @@ print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg) epath = rb_class_name(eclass); if (elen == 0) { + if (colored) warn_print(underline); warn_print_str(epath); warn_print("\n"); } @@ -119,7 +130,10 @@ print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg) warn_print_str(tail ? rb_str_subseq(emesg, 0, len) : emesg); if (epath) { warn_print(" ("); + if (colored) warn_print(underline); warn_print_str(epath); + if (colored) warn_print(reset); + if (colored) warn_print(bold); warn_print(")\n"); } if (tail) { @@ -128,6 +142,7 @@ print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg) if (tail ? einfo[elen-1] != '\n' : !epath) warn_print2("\n", 1); } } + if (colored) warn_print(reset); } static void @@ -192,12 +207,12 @@ rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo) } } if (rb_stderr_tty_p()) { - warn_print("Traceback (most recent call last):\n"); + warn_print("\033[1mTraceback \033[m(most recent call last):\n"); print_backtrace(eclass, errat, TRUE); - print_errinfo(eclass, errat, emesg); + print_errinfo(eclass, errat, emesg, TRUE); } else { - print_errinfo(eclass, errat, emesg); + print_errinfo(eclass, errat, emesg, FALSE); print_backtrace(eclass, errat, FALSE); } error: -- 2.10.2