Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ENV variables for disabling colour stacktrace printing #40232

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,12 @@ stacktrace_contract_userdir()::Bool =
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_CONTRACT_HOMEDIR", "true")) === true
stacktrace_linebreaks()::Bool =
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_LINEBREAKS", "false")) === true
stacktrace_color_lines()::Bool =
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_COLOR_LINES", "true")) === true
stacktrace_color_modules()::Bool =
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_COLOR_MODULES", "true")) === true

stacktrace_line_color() = stacktrace_color_lines() ? :light_black : :normal

function show_full_backtrace(io::IO, trace::Vector; print_linebreaks::Bool)
num_frames = length(trace)
Expand Down Expand Up @@ -660,7 +666,8 @@ function show_reduced_backtrace(io::IO, t::Vector)
popfirst!(repeated_cycle)
printstyled(io,
"--- the last ", cycle_length, " lines are repeated ",
repetitions, " more time", repetitions>1 ? "s" : "", " ---", color = :light_black)
repetitions, " more time", repetitions>1 ? "s" : "", " ---",
color=stacktrace_line_color())
if i < length(displayed_stackframes)
println(io)
stacktrace_linebreaks() && println(io)
Expand All @@ -677,7 +684,7 @@ end
# from `modulecolorcycler`.
function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, modulecolordict, modulecolorcycler)
m = Base.parentmodule(frame)
if m !== nothing
if m !== nothing && stacktrace_color_modules()
while parentmodule(m) !== m
pm = parentmodule(m)
pm == Main && break
Expand Down Expand Up @@ -715,12 +722,12 @@ function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, m

StackTraces.show_spec_linfo(IOContext(io, :backtrace=>true), frame)
if n > 1
printstyled(io, " (repeats $n times)"; color=:light_black)
printstyled(io, " (repeats $n times)"; color=stacktrace_line_color())
end
println(io)

# @
printstyled(io, " " ^ (digit_align_width + 2) * "@ ", color = :light_black)
printstyled(io, " " ^ (digit_align_width + 2) * "@ "; color=stacktrace_line_color())

# module
if modul !== nothing
Expand All @@ -732,22 +739,23 @@ function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, m
pathparts = splitpath(file)
folderparts = pathparts[1:end-1]
if !isempty(folderparts)
printstyled(io, joinpath(folderparts...) * (Sys.iswindows() ? "\\" : "/"), color = :light_black)
printstyled(io, joinpath(folderparts...) * (Sys.iswindows() ? "\\" : "/");
color=stacktrace_line_color())
end

# filename, separator, line
# use escape codes for formatting, printstyled can't do underlined and color
# codes are bright black (90) and underlined (4)
function print_underlined(io::IO, s...)
colored = get(io, :color, false)::Bool
colored = get(io, :color, false)::Bool && stacktrace_hascolor()
start_s = colored ? "\033[90;4m" : ""
end_s = colored ? "\033[0m" : ""
print(io, start_s, s..., end_s)
end
print_underlined(io, pathparts[end], ":", line)

# inlined
printstyled(io, inlined ? " [inlined]" : "", color = :light_black)
printstyled(io, inlined ? " [inlined]" : ""; color=stacktrace_line_color())
end


Expand Down
6 changes: 3 additions & 3 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,7 @@ function show_tuple_as_call(io::IO, name::Symbol, sig::Type;
first || print(io, ", ")
first = false
if show_argnames
print_within_stacktrace(io, argnames[i]; color=:light_black)
print_within_stacktrace(io, argnames[i]; color=stacktrace_line_color())
end
print(io, "::")
print_type_stacktrace(env_io, sig[i])
Expand All @@ -2285,7 +2285,7 @@ function show_tuple_as_call(io::IO, name::Symbol, sig::Type;
for (k, t) in kwargs
first || print(io, ", ")
first = false
print_within_stacktrace(io, k; color=:light_black)
print_within_stacktrace(io, k; color=stacktrace_line_color())
print(io, "::")
print_type_stacktrace(io, t)
end
Expand All @@ -2302,7 +2302,7 @@ function print_type_stacktrace(io, type; color=:normal)
printstyled(io, str; color=color)
else
printstyled(io, str[1:prevind(str,i)]; color=color)
printstyled(io, str[i:end]; color=:light_black)
printstyled(io, str[i:end]; color=stacktrace_line_color())
end
end

Expand Down