I have a function which logs a message to a log file. This function
will be called from many functions within my .vbs script. In this
function I want to find the name of the function which has called it
(its parent function). This is so that the name of the caller gets
logged.
Is there a way to obtain the name of the caller or the entire call
stack in .vbs?
Thanks,
Yash
You could pass the name of the calling function as an additional parameter:
Sub SetPrinter
...
LogEvent (55, "SetPrinter")
...
End Sub
Function LogEvent (iEventID, sCaller)
wscript.echo "ID=" & iEventID & " Caller=" & sCaller
...
End Function
In the past I've tried to do similar tracing activity within scripts, and
the approach that I've used _may_ be helpful, although it's not as
straightforward as what you're after. I created a generic linewriter
function to make calls to the logger easier. Then within each
function/routine in the code I was tracing I would insert a logger call at
the beginning and end like this:
function GetFoo(args)
WriteLog "<Entering GetFoo"
<code here>
GetFoo = ...
WriteLog ">Exiting GetFoo"
end function
This is, of course, quite cumbersome. You need to look through the log for
matching enter/exit statements, and the function doesn't even know its own
name automatically, making it a bit irksome to update.
Out of curiosity, what's your end objective with this? I don't know if
there's a palatable solution for your problem, but there may be another
angle on it you haven't considered.
"Yash" <yas...@gmail.com> wrote in message
news:a24d885b-defe-4983...@s9g2000prg.googlegroups.com...
hi Yash,
You might try running your script under a debugger.
One debugger which might work is the "Microsoft Script
Editor". The MSE comes as part of msOffice, but may
also be available elsewhere. Earlier versions are
named MSE.EXE, and later versions are named MSE7.EXE,
(or maybe MSE8 or MSE9???.
MSE does have a Call Stack Viewer. Look under:
View => Debug Widows => Call Stack.
I suspect there may be other debuggers which also
have a call stack viewer. I have no experience with
Sapien's PrimalScript, but it is the "Cadillac" of
scripting tools, and has just about every feature
you can think of, so you may find a call stack viewer
in PrimalScript too.
As far as "programatically" getting at the call stack
-- that would be problematical at best. You may be
able to find a 3re-party control to do it for you, but
I have never seen one. You may also be able to "do-it-
yourself" -- but I strongly suspect that some assembly
code will have to come into play somewhere along the
way.
cheers, jw
____________________________________________________________
You got questions? WE GOT ANSWERS!!! ..(but, no guarantee
the answers will be applicable to the questions)
For anybody who would like to "do-it-yourself", I suggest
reading Jochen Kalmbach's article (with code) entitled:
"Walking the CallStack":
http://www.codeproject.com/KB/threads/StackWalker.aspx
Jochen is using microsoft's StackWalker/StackWalker64 tool,
which I understand is part of the microsoft "Debugging Tools
for Windows" package, which is part of the microsoft
"Windows Developer Kit" (WDK).
http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx
If you don't have this installed, it is apparently downloadable
(for free) from msdn.
cheers, jw