I call a method in TForm.ButtonClick:
procedure TForm1.Button1Click(Sender: TObject);
var
Logger: TLogger;
begin
Logger := TLogger.Create;
Logger.Log;
end;
I the Log method of TLogger, I want to get the classname of the caller
(TForm) and the method name Button1Click), but without pass the
TObject.MethodName() and TObject.ClassName as parameter.
How to get this informations in the Log method ?
Thanks a lot !
Vince
Since you have this information when you call log, why not simply pass
it? You probably can make Logger.Log dig through the stack, find the
caller's address, search through a map file, walk through memory...
but why not simply write:
Logger.Log('TForm1', 'Button1Click');
Besides, there's no assurance that Logger.Log will be called from an
object's method.
Good luck.
Kurt
Thank you for your answer.
It's just for easier use of the TLogger component. It's boring to write this
parameters each time user want to log.
Hmmm this'll require asm code: I need more thant luck ;-)
You can use MadExcept or the Jedi Code Library. Both come with routines
to get information about the caller.
--
Rob
I've tried with JCL and "Caller" & "GetLocationInfo" like this:
var
CallerInfo: TJclLocationInfo;
begin
CallerInfo := GetLocationInfo(Caller(1));
// After that CallerInfo is always empty ! (CallerInfo.Address = nil :-/ )
ShowMessage(CallerInfo.MethodName);
end;
What the bug ? With the demo "SourceLocExample.dpr" it seems to be the
same...
Vince
"Rob Kennedy" <.> a écrit dans le message news:
3fd88de7$1...@newsgroups.borland.com...
> It's just for easier use of the TLogger component. It's boring to write this
> parameters each time user want to log.
OK then, just write LOG!
Then write a program that reads *.pas, line by line, remembering the last
time it found a line beginning with 'procedure T' and splitting out the
stuff from the T to '.' and from there to the next semicolon or ( and
replaces "LOG!" with
"Logger.Log(''''+currentobj+''','''+currentmeth+''');'''"
.. actually you might make it {LOG!} - then the program will still compile
without using the replacer first.
Still boring?
I'm surprised that even compiled. TJclLocationInfo doesn't have a
MethodName field.
If the single-argument version of GetLocationInfo fails, it just returns
an empty record -- all the fields are filled with zeroes. Call the
two-argument version, and it will return a Boolean telling you whether
it succeeded.
> What the bug ? With the demo "SourceLocExample.dpr" it seems to be the
> same...
Did you remember to generate a detailed map file for your project?
The ProcByLevel function is a simpler way to get at the same information
returned by GetLocationInfo.
--
Rob