ID:2335927
 
Occasionally i've had a debugging need that would have been best served if i had been able to store a stacktrace, and conditionally output it based on info later down the line. (for example if something keeps getting into some invalid state and your trying to figure out, storing the stack traces on things that edited the relevant bits of state and than outputting that only when the invalid state is notice, useful if the code in question is called too often to just have the stack_traces go to the runtime logs)

So I made something to let me do just that.


/proc/stack_trace(msg)
CRASH(msg)

/var/list/stack_trace_storage
/proc/gib_stack_trace()
stack_trace_storage = list()
stack_trace()
stack_trace_storage.Cut(1, min(3,stack_trace_storage.len+1))
. = stack_trace_storage

stack_trace_storage = null

/world/Error(exception/E)
if (stack_trace_storage)
for (var/line in splittext(E.desc, "\n"))
if (text2ascii(line) != 32)
stack_trace_storage += line
return
..()


I actually did something like this a few weeks ago but your solution came out waaay more elegant (forgiving the SS13 syntax of course).
Thank you!