Skip to content

Commit 086e0c8

Browse files
committed
1.8 with some cleaning
1 parent f9bad7f commit 086e0c8

2 files changed

Lines changed: 43 additions & 214 deletions

File tree

src/RelevanceStacktrace.jl

Lines changed: 39 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module RelevanceStacktrace
2-
import Base: find_source_file, print_stackframe, StackFrame, fixup_stdlib_path,
2+
import Base: find_source_file, show_full_backtrace, StackFrame, fixup_stdlib_path,
33
stacktrace_expand_basepaths, stacktrace_contract_userdir, contractuser
44

55
KNOWN_MODULES = ["VSCodeServer", "Core", "Distributed"]
@@ -12,19 +12,15 @@ is_project_file(modul, pathparts) = (
1212
!(length(pathparts)>1 && pathparts[2] in KNOWN_MODULES_PATHS_2))
1313

1414
# This is useful because of
15-
# function Base.print_stackframe(io, i, frame::Base.StackFrame, n::Int, digit_align_width, modulecolordict, ownmodulescounter)
16-
# RelevanceStacktrace.print_stackframe(io, i, frame, n, digit_align_width, ownmodulescounter, false)
17-
# end
15+
function print_stackframe_relevance(io, i, frame::Base.StackFrame, n::Int, digit_align_width, modulecolordict, ownmodulescounter)
16+
print_stackframe_relevance_print(io, i, frame, n, digit_align_width, modulecolordict, ownmodulescounter)
17+
end
1818

1919
@info "Overloading Base.print_stackframe(...), Base.show_full_backtrace(io::IO, trace::Vector, print_linebreaks::Bool) and Base.showerror(...) with Experimental version"
2020

21-
ownmodulescounter::IdDict{Any, Any} = IdDict()
22-
2321
# Print a stack frame where the module color is set manually with `modulecolor`.
24-
function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, modulecolor)
22+
function print_stackframe_relevance_print(io, i, frame::Base.StackFrame, n::Int, digit_align_width, modulecolor, ownmodulescounter)
2523

26-
modulecolor = copy(Base.STACKTRACE_FIXEDCOLORS) # We don't color the module!
27-
2824
file, line = string(frame.file), frame.line
2925
file = fixup_stdlib_path(file)
3026
stacktrace_expand_basepaths() && (file = something(find_source_file(file), file))
@@ -59,10 +55,9 @@ function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, m
5955
if n > 1
6056
printstyled(io, " (repeats $n times)"; color=:light_black)
6157
end
62-
println(io)
6358

6459
# @
65-
printstyled(io, " " ^ (digit_align_width + 2) * "@ ", color = :light_black)
60+
printstyled(io, " @ ", color = :light_black)
6661

6762
# module
6863
if modul !== nothing
@@ -76,215 +71,48 @@ function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, m
7671
printstyled(io, joinpath(folderparts...) * (Sys.iswindows() ? "\\" : "/"), color = :light_black)
7772
end
7873

74+
colored = get(io, :color, false)::Bool
7975
# filename, separator, line
8076
# use escape codes for formatting, printstyled can't do underlined and color
8177
# codes are bright black (90) and underlined (4)
82-
printstyled(io, pathparts[end], ":", line; color = :light_black, underline = true)
83-
84-
colored = get(io, :color, false)::Bool
85-
start_s = colored && isprojectfile ? "\u001b[32;" * (firstofitsmodule ? "1;4m" : "4m") : ""
86-
end_s = colored && isprojectfile ? "\033[0m" : ""
87-
print(io, start_s, s..., end_s)
78+
printstyled(io, pathparts[end], ":", line, ; color = isprojectfile && colored ? :green : :default, bold=firstofitsmodule, underline = true)
8879

8980
# inlined
9081
printstyled(io, inlined ? " [inlined]" : "", color = :light_black)
9182
end
9283

9384

85+
function Base.show_full_backtrace(io::IO, trace::Vector; print_linebreaks::Bool)
86+
num_frames = length(trace)
87+
ndigits_max = ndigits(num_frames)
88+
89+
modulecolordict = copy(Base.STACKTRACE_FIXEDCOLORS)
90+
ownmodulescounter = IdDict()
91+
92+
println(io, "\nStacktrace:")
93+
94+
try
95+
for (i, (frame, n)) in enumerate(trace)
96+
print_stackframe_relevance(io, i, frame, n, ndigits_max, modulecolordict, ownmodulescounter)
97+
if i < num_frames
98+
println(io)
99+
print_linebreaks && println(io)
100+
end
101+
end
102+
catch e
103+
println(io)
104+
@error "during custom show_full_backtrace in RelevanceStacktrace.jl, FALLBACK to RAW format:"
105+
println(io, e)
106+
bt = catch_backtrace()
107+
filtered = Base.process_backtrace(bt)
108+
frames = map(x->first(x)::Base.StackFrame, filtered)
109+
for (i, frame) in enumerate(frames)
110+
print(io, lpad("[$i] ", 6))
111+
StackTraces.show_spec_linfo(IOContext(io, :backtrace=>true), frame)
112+
println(io, " @ $(frame.file):$(frame.line)")
113+
end
114+
end
115+
end
94116

95117

96-
# A different version of print_stackframe
97-
# function Base.print_stackframe(io, i, frame::Base.StackFrame, n::Int, digit_align_width, ownmodulescounter, debug=false)
98-
# file, line = string(frame.file), frame.line
99-
# Base.stacktrace_expand_basepaths() && (file = something(find_source_file(file), file))
100-
# Base.stacktrace_contract_userdir() && (file = Base.contractuser(file))
101-
# # Used by the REPL to make it possible to open
102-
# # the location of a stackframe/method in the editor.
103-
# if haskey(io, :last_shown_line_infos)
104-
# push!(io[:last_shown_line_infos], (string(frame.file), frame.line))
105-
# end
106-
107-
# inlined = getfield(frame, :inlined)
108-
# modul = parentmodule(frame)
109-
# pathparts = splitpath(file)
110-
111-
# (debug && println(io,"Stack preprocessing is ok"))
112-
# isprojectfile = is_project_file(modul, pathparts)
113-
114-
# (debug && println(io,"Project's file check is ok"))
115-
# firstofitsmodule = false
116-
# if isprojectfile
117-
# if !haskey(ownmodulescounter, modul)
118-
# ownmodulescounter[modul] = 0
119-
# firstofitsmodule = length(ownmodulescounter) == 1
120-
# end
121-
# ownmodulescounter[modul] += 1
122-
# end
123-
# (debug && println(io,"Module counter is ok"))
124-
# # frame number
125-
# print(io, lpad(" [" * string(i) * "] ", digit_align_width + 4))
126-
127-
# StackTraces.show_spec_linfo(IOContext(io, :backtrace=>true), frame)
128-
# if n > 1
129-
# printstyled(io, " (repeats $n times)"; color=:white)
130-
# end
131-
# # @
132-
# printstyled(io, " @ ", color = :light_black)
133-
134-
# # module
135-
# if modul !== nothing
136-
# printstyled(io, modul, color = :default)
137-
# print(io, " ")
138-
# end
139-
140-
# # filepath
141-
# folderparts = pathparts[1:end-1]
142-
# if !isempty(folderparts)
143-
# printstyled(io, joinpath(folderparts...) * (Sys.iswindows() ? "\\" : "/"), color = :light_black)
144-
# # printstyled(io, "\033[90;4m" * joinpath(folderparts...) * (Sys.iswindows() ? "\\" : "/") * "\033[0m", color = :light_black)
145-
# end
146-
147-
# # use escape codes for formatting, printstyled can't do underlined and color
148-
# # codes are bright black (90) and underlined (4)
149-
# function print_underlined(io::IO, s...)
150-
# colored = get(io, :color, false)::Bool
151-
# start_s = colored && isprojectfile ? "\u001b[32;" * (firstofitsmodule ? "1;4m" : "4m") : ""
152-
# end_s = colored && isprojectfile ? "\033[0m" : ""
153-
# print(io, start_s, s..., end_s)
154-
# end
155-
# # filename, separator, line
156-
# print_underlined(io, pathparts[end], ":", line)
157-
158-
# # inlined
159-
# printstyled(io, inlined ? " [inlined]" : "", color = :light_black)
160-
161-
# (debug && print(io, "\nPrinting is ok for 1 file as you see..."))
162-
# end
163-
164-
165-
166-
167-
168-
169-
170-
171-
# function show_backtrace(io::IO, t::Vector)
172-
# if haskey(io, :last_shown_line_infos)
173-
# empty!(io[:last_shown_line_infos])
174-
# end
175-
176-
# # t is a pre-processed backtrace (ref #12856)
177-
# if t isa Vector{Any}
178-
# filtered = t
179-
# else
180-
# filtered = process_backtrace(t)
181-
# end
182-
# isempty(filtered) && return
183-
184-
# if length(filtered) == 1 && StackTraces.is_top_level_frame(filtered[1][1])
185-
# f = filtered[1][1]::StackFrame
186-
# if f.line == 0 && f.file === Symbol("")
187-
# # don't show a single top-level frame with no location info
188-
# return
189-
# end
190-
# end
191-
192-
# if length(filtered) > BIG_STACKTRACE_SIZE
193-
# show_reduced_backtrace(IOContext(io, :backtrace => true), filtered)
194-
# return
195-
# end
196-
197-
# try invokelatest(update_stackframes_callback[], filtered) catch end
198-
# # process_backtrace returns a Vector{Tuple{Frame, Int}}
199-
# show_full_backtrace(io, filtered; print_linebreaks = stacktrace_linebreaks())
200-
# return
201-
# end
202-
203-
204-
205-
206-
# function Base.show_full_backtrace(io::IO, trace::Vector; print_linebreaks::Bool)
207-
# num_frames = length(trace)
208-
# ndigits_max = ndigits(num_frames)
209-
210-
# modulecolordict = copy(Base.STACKTRACE_FIXEDCOLORS)
211-
# ownmodulescounter = IdDict()
212-
# println(io, "\nStacktrace:")
213-
# try
214-
# for (i, (frame, n)) in enumerate(trace)
215-
# Base.print_stackframe(io, i, frame, n, ndigits_max, modulecolordict, ownmodulescounter)
216-
# if i < num_frames
217-
# println(io)
218-
# print_linebreaks && println(io)
219-
# end
220-
# end
221-
# catch e
222-
# println(io)
223-
# @error "Error: during show_full_backtrace in RelevanceStacktrace.jl, we try to print a the error with a basic format:"
224-
# println(io, e)
225-
# bt = catch_backtrace()
226-
# filtered = Base.process_backtrace(bt)
227-
# frames = map(x->first(x)::Base.StackFrame, filtered)
228-
# for (i, frame) in enumerate(frames)
229-
# print(io, lpad(" [$i] ", 6))
230-
# StackTraces.show_spec_linfo(IOContext(io, :backtrace=>true), frame)
231-
# println(io, " @ $(frame.file):$(frame.line)")
232-
# end
233-
# end
234-
# end
235-
236-
237-
# function show_full_backtrace(io::IO, trace::Vector; print_linebreaks::Bool)
238-
# num_frames = length(trace)
239-
# ndigits_max = ndigits(num_frames)
240-
241-
# modulecolordict = copy(STACKTRACE_FIXEDCOLORS)
242-
# modulecolorcycler = Iterators.Stateful(Iterators.cycle(STACKTRACE_MODULECOLORS))
243-
244-
# println(io, "\nStacktrace:")
245-
246-
# for (i, (frame, n)) in enumerate(trace)
247-
# print_stackframe(io, i, frame, n, ndigits_max, modulecolordict, modulecolorcycler)
248-
# if i < num_frames
249-
# println(io)
250-
# print_linebreaks && println(io)
251-
# end
252-
# end
253-
# end
254-
255-
256-
257-
# function Base.showerror(io::IO, ex::LoadError, bt; backtrace=true)
258-
# try
259-
# print(io, "LoadError: ")
260-
# Base.showerror(io, ex.error, bt, backtrace=backtrace)
261-
# pathparts = splitpath(ex.file)
262-
# folderparts = pathparts[1:end-1]
263-
# if length(folderparts) ==0
264-
# print(io, "\nin expression starting at REPL? \u001b[32;4m$(pathparts[end]):$(ex.line)\033[0m")
265-
# else
266-
# folderpath=(joinpath(folderparts...) * (Sys.iswindows() ? "\\" : "/"))
267-
# print(io, "\nin expression starting at $(folderpath)\u001b[32;4m$(pathparts[end]):$(ex.line)\033[0m")
268-
# end
269-
# catch e
270-
# println(io)
271-
# @error "Error: during showerror in RelevanceStacktrace.jl, we try to print a the error with a basic format:"
272-
# println(io)
273-
# println(io, e)
274-
# println(io)
275-
# bt = catch_backtrace()
276-
# println(io,"k")
277-
# filtered = Base.process_backtrace(bt)
278-
# println(io,"k3")
279-
# frames = map(x->first(x)::Base.StackFrame, filtered)
280-
# println(io,"k35")
281-
# for (i, frame) in enumerate(frames)
282-
# println(io,"k37")
283-
# print(io, lpad(" [$i] ", 6))
284-
# StackTraces.show_spec_linfo(IOContext(io, :backtrace=>true), frame)
285-
# println(io, " @ $(frame.file):$(frame.line)")
286-
# end
287-
# end
288-
# end
289-
290118
end # module

test/RelevanceStacktrace.test.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ using Revise
66
includet("../src/RelevanceStacktrace.jl")
77

88

9-
func4(x) = x.+ ["32",3]
9+
func4(x) = x .* ["32", 3]
1010
func3(x) = x+5 + func4(x)
1111
func2(x) = x+3 + func3(x)
12-
func1(x) = func2(x)
13-
12+
func1(x) = begin
13+
func2(x)
14+
end
1415
func1(3)
1516

1617
#%%

0 commit comments

Comments
 (0)