How to display or save stack information?
Hello.
For debugging purposes I sometimes need to display stack information to the current device or save it (to a global for example).
There is this snippet offered in documentation:
ClassMethod StackToDisplay()
{
For loop = 0:1:$Stack(-1) {
Write !, "Context level:", loop, ?25, "Context type: ", $Stack(loop)
Write !, ?5, "Current place: ", $Stack(loop, "PLACE")
Write !, ?5, "Current source: ", $Stack(loop, "MCODE")
Write !
}
}
And to save stack to a global there's always an exception method:
ClassMethod StackToGlobal()
{
Set ex = ##class(%Exception.SystemException).%New()
Do ex.StackAsArray(.arr)
Set time = $ZDT($H)
Merge ^StackInfo(time) = arr
}
But I'm interested if someone has a better code (more info) for this purpose? What's your approach to saving stack information?
In Catch block I usually execute Log method of Exception
And then find this in Application Error Log journal with all the stack.
E.g. link to the bugs in USER Namespace
http://localhost:57772/csp/sys/op/UtilSysAppErrorNamespaces.csp?$NAMESPACE=USER
Thank you.
Also note, that e.Log() calls LOG^%ETN.
An easier way to capture the stack and values of variables at all stack levels is:
Then it's possible to view full information on stack and variables from terminal with:
Or in the management portal at System Operation > System Logs > Application Error Log.
If you're logging to globals to track more limited parts of the process state for debugging, it's helpful to use a ^CacheTemp* or ^mtemp* global so that the debugging information (a) isn't rolled back by TROLLBACK and (b) won't accumulate in an important database if the debugging code is accidentally left in.
LOG^%ETN it is very useful but in most cases overkill. And may take several seconds to gather full stack.
and maybe used for any logging, but should have filled $zerror
some time for logging I use such code.
set $ze="test",t=$$LOG^%ETN,$ze=""
You can fill $zerror by passing a name into LOG^%ETN:
It would record your current $zerror value, replace it with "test", execute LOG captue, and at the end restore $zerror to what it was before the call.
Somewhen I used the following function to save stacked calls and all variables defined. Variables are not separated by stack levels, that seems to be the reason of $$getst() quickness.
Formatted your code a little.
Well-well, Ed. The intention of following check up:
was to avoid logging my own variables which names were deliberately started with this nasty prefix. I agree that it's rather naive trick, but it worked.
As to original code, it was taken from the existing code base and complied our internal coding standards; I preferred to publish it "as is", having neither time nor will to re-test it.
I wondered actually, what for do you prefix all variable names with "zzzz".
Just noticed the following "reformatted" code fragment:
Thanks. Removed that as it's useless without zzzz variables.
Social networks
InterSystems resources
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue