On Wed, 01 Nov 2006 15:31:09 GMT, "Rico" <you@me.comwrote:
Quote:
>Hello,
>
>I have a generic process that logs errors from different sources. When I
>call this code, I'd like to also submit the name of the function or sub that
>is raising the error without having to manually type this in for each
>instance. For instance, (air code to follow)
>
>Public Sub LogMyError( errNumber as int, errDescription as string,
>SubFuncName as string)
[Code to log my error]
>end sub
>
>To call this right now, I'd have to type in the SubFuncName as in
>
>LogMyError err.number,err.description,"MySubOrFunction"
>
>I'd like to find a way of dynamically determining the name so I don't have
>to use the typed string.
>
>Any way to do that? Any direction would be greatly appreciated.
>
>Thanks!
>Rick
>
This is the first thing you would think of if developing an interpretive programming system but it
seems to be impossible at least for VBA.
I once did an extensive search for (classic) VB and the following is all I found: (unfortunately
the web link is now dead). It is OT for this group but may amuse you:
Debugging tip: Getting the name of the current and calling function at runtime
http://www.constantthought.com/yabbse/index.php?board=4;action=display;threadid=240
ThoughtForum
Development
Visual Basic 5/6 and VBScript
Debugging tip: Getting the name of the current and
calling function at runtime
Author Murphy
« on: December 04, 2003, 12:18:21 AM » Reply with quote
This post is based on one that I wrote in
response to newsgroup question regarding getting
the name of the function that called the current
function for the purposes of error logging.
Here's the story...
Yes, this is possible, especially if you're only
concerned with doing it for debugging. That is,
I wouldn't really want to do it for a release
build myself.
I think the easiest way would be to tell the
linker to generate a map file, and then load and
parse it at runtime (this will give you a fairly
easy way to map addresses to function names).
Then get the value of the EBP register -- the
stack entry right before it will hold the return
address (which is somewhere inside the caller
function). Search through the map file entries
for the function with the closest address less
than or equal to the return address, and that's
who called you.
Getting the linker to generate a map file is
pretty easy if you use a little trick I thought
up after doing things The Hard Way for quite a
while. From the Immediate window, execute...
SetEnv "LINK", "/MAP"
When you compile, you'll get a map file to go
with your project. Note that this is a very,
very good trick and is useful for more than just
this. You're welcome.
Getting the value of EBP is not much more
difficult. Look in the new MurfLib for
GetEBP(). It's a function that overwrites
itself with a little tiny machine language
function (mov eax, ebp; ret). Just call it and
it'll return the value of EBP.
The next step is being able to parse the map
file and build a list of friendlier names (the
ones in the map file are mangled), and using
that to get the name of the current function,
the name of the caller function, and the files
they're from. In the attached code module,
there's a function that takes care of this with
a single function which returns a string of info
-- DebugInfo().
As for why I wouldn't want to do it with a
release build:
First, you need the map file at runtime. This
may not be a big deal. Second, you may need to
compile with optimizations off in order to get
reliably meaningful results. If the compiler
optimizes-out the stack frame, the wrong
function name will be returned. Though...
hmm... does VB -ever- drop the stack frame? It
does so much housekeeping, it seems like the
chances of it doing so seem pretty low.
By the way -- the modDebugInfo uses some of the
new MurfLib String stuff (hooray for
CutString()!) extensively, as well as showing
how to use QSort() with a callback to sort an
array of structures based on one field.
modDebugStuff.bas
« Last Edit: December 04, 2003, 12:25:00 AM by
Murphy »
Be sure to check out the new
ConstantThought.com!