PDA

View Full Version : logger in a dll



tanffn
18-01-2007, 10:19 AM
I'm using the logger unit in several dlls and in the main application, the problem is it initializes <log> upon loading.
As all of the dlls are called from the same app they all want to create “myApp.log” (only the 1st one will win, and the rest won’t log)

Any ideas for a clean nice fix?
:arrow: Maybe if it fails add _# (# = available number) to the app name, but then you don’t know the corresponding log for a dll.
:arrow: Or we can remove the initialization part and do it our self, this will allow us to add any name we want.

grudzio
18-01-2007, 11:27 AM
How about changing the initialization and finalization sections so they check if Log is nil and if yes then create it?


initialization
begin
if Log = nil then begin
Log := TLogger.Create;
Log.LogStatus( 'Starting Application', 'Initialization' );
end;
end;

finalization
begin
if Log <> nil then begin
Log.LogStatus( 'Terminating Application', 'Finalization' );
Log.Free;
Log := nil;
end;
end;

I also like your second idea.

technomage
18-01-2007, 11:43 AM
I used the logger in 1 dll only and added an API to that dll

Core_LogStatus
Core_LogWarning
Core_LogError


all the other dll's and the main application use this API so they all write to the same log file.

This works really well in my applications :D

tanffn
18-01-2007, 01:53 PM
grudzio: Your solution is completely wrong, you most likely get several AV, and even if you won’t you will still end up with one file. you’ll tell me why :)
technomage: Best solution so far, but there is an advantage in having a log for each module you create...

technomage
18-01-2007, 02:20 PM
How about you overload the constructor of the TLogger class to take then name of the log file, if it's blank then user the ParamStr(0) filename.

Or just give it a parameter with a default of EmptrStr


constructor TLogger.Create(ALogName: string = EmptyStr);

grudzio
18-01-2007, 02:39 PM
grudzio: Your solution is completely wrong, you most likely get several AV

If the Log variable is defined like this


Log &#58; TLogger = nil;

I dont see how can I get access violation.


you will still end up with one file.

I thought that is what you wanted to get. My mistake.