summaryrefslogtreecommitdiff
path: root/eval_error.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-03-24 10:57:34 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-03-24 10:57:34 +0000
commited9c67c91ff1e4667c20517e43222cf65328a561 (patch)
tree9813a5f21059a4d18664cd12b89c8ba4a4b103ba /eval_error.c
parentc40df5a76941d3a8c2cff46432b23401b6ffffbc (diff)
merge revision(s) 62548,62894: [Backport #14324]
eval_error.c: rb_error_write flags * eval_error.c (rb_error_write): add highlight and reverse mode flags. defaulted to rb_stderr_tty_p() if Qnil. error.c: full_message options * error.c (exc_full_message): add highlight: and reverse: keyword options. [Bug #14324] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_5@62905 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'eval_error.c')
-rw-r--r--eval_error.c42
1 files changed, 33 insertions, 9 deletions
diff --git a/eval_error.c b/eval_error.c
index 646e55ffb3..449024b516 100644
--- a/eval_error.c
+++ b/eval_error.c
@@ -85,12 +85,16 @@ error_print(rb_execution_context_t *ec)
rb_ec_error_print(ec, ec->errinfo);
}
+#define CSI_BEGIN "\033["
+#define CSI_SGR "m"
+
+static const char underline[] = CSI_BEGIN"1;4"CSI_SGR;
+static const char bold[] = CSI_BEGIN"1"CSI_SGR;
+static const char reset[] = CSI_BEGIN""CSI_SGR;
+
static void
print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, const VALUE str, 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;
@@ -188,7 +192,7 @@ print_backtrace(const VALUE eclass, const VALUE errat, const VALUE str, int reve
}
void
-rb_error_write(VALUE errinfo, VALUE errat, VALUE str)
+rb_error_write(VALUE errinfo, VALUE errat, VALUE str, VALUE highlight, VALUE reverse)
{
volatile VALUE eclass = Qundef, emesg = Qundef;
@@ -205,13 +209,33 @@ rb_error_write(VALUE errinfo, VALUE errat, VALUE str)
emesg = e;
}
}
- if (rb_stderr_tty_p()) {
- write_warn(str, "\033[1mTraceback \033[m(most recent call last):\n");
+ if (NIL_P(reverse) || NIL_P(highlight)) {
+ VALUE tty = (VALUE)rb_stderr_tty_p();
+ if (NIL_P(reverse)) reverse = tty;
+ if (NIL_P(highlight)) highlight = tty;
+ }
+ if (reverse) {
+ static const char traceback[] = "Traceback "
+ "(most recent call last):\n";
+ const int bold_part = rb_strlen_lit("Traceback");
+ char buff[sizeof(traceback)+sizeof(bold)+sizeof(reset)-2], *p = buff;
+ const char *msg = traceback;
+ long len = sizeof(traceback) - 1;
+ if (highlight) {
+#define APPEND(s, l) (memcpy(p, s, l), p += (l))
+ APPEND(bold, sizeof(bold)-1);
+ APPEND(traceback, bold_part);
+ APPEND(reset, sizeof(reset)-1);
+ APPEND(traceback + bold_part, sizeof(traceback)-bold_part-1);
+#undef APPEND
+ len = p - (msg = buff);
+ }
+ write_warn2(str, msg, len);
print_backtrace(eclass, errat, str, TRUE);
- print_errinfo(eclass, errat, emesg, str, TRUE);
+ print_errinfo(eclass, errat, emesg, str, highlight!=0);
}
else {
- print_errinfo(eclass, errat, emesg, str, FALSE);
+ print_errinfo(eclass, errat, emesg, str, highlight!=0);
print_backtrace(eclass, errat, str, FALSE);
}
}
@@ -231,7 +255,7 @@ rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo)
errat = rb_get_backtrace(errinfo);
}
- rb_error_write(errinfo, errat, Qnil);
+ rb_error_write(errinfo, errat, Qnil, Qnil, Qnil);
EC_POP_TAG();
ec->errinfo = errinfo;