unit LogFile;
interface
uses sysutils, Classes, Forms;
type
ILogFile = interface(IInterface)
['{6BDBB5CF-77B2-4E0F-8D2C-3DD06DC1AE55}']
Function openLogFile(alogFilePath : String) : boolean;
Function Log(aText : String) : boolean;
Function getLogFilepath : String;
Function getIsOpen : boolean;
Function getStringsOut : TStrings;
Function getLastLogTime : TDatetime;
Procedure setStringsOut(aLogList : TStrings);
end;
TCustomLogFile = Class(TInterfacedobject, ILogFile)
Private
fLogFile: String;
fIsOpen: Boolean;
fStringsOut: TStrings;
fFile: TextFile;
fLastLogTime: TDateTime;
Public
Constructor Create;virtual;
Destructor Destroy;override;
Function OpenLogFile(alogFilePath : String) : boolean;
Function Log(aText : String) : boolean;
Function getLogFilePath : String;
Function getIsOpen : boolean;
Function getStringsOut : TStrings;
Procedure setStringsOut(aLogList : TStrings);
Function getLastLogTime : TDatetime;
Property IsOpen: boolean read getIsOpen;
Property LogFilePath : String read getLogFilePath;
Property StringsOut : TStrings read getStringsOut write setStringsOut;
Property LastLogTime : TDateTime read getLastLogTime;
end;
TLogfile = class(TCustomLogFile)
public
Constructor Create(aLogfile:String;aStringsOut:TStrings);Virtual;
Destructor Destroy;override;
end;
var StandardLogFile:ILogFile;
implementation
Constructor TLogfile.Create(aLogfile:String;aStringsOut:TStrings);
Begin
inherited Create;
If alogFile <> '' then
OpenLogFile(aLogFile);
StringsOut := aStringsOut;
if isOpen or assigned(StringsOut) then
Log('Log gestartet');
end;
Destructor TLogfile.Destroy;
Begin
try
Log('Log beendet');
finally
inherited Destroy;
end;
end;
Function TCustomLogFile.getLastLogTime : TDatetime;
begin
Result := fLastLogTime;
end;
Constructor TCustomLogFile.Create;
Begin
inherited;
fLogFile:= '';
fIsOpen:= false;
fStringsOut:= nil;
fLastLogTime := Now;
end;
Destructor TCustomLogFile.Destroy;
Begin
if IsOpen then
begin
Closefile(fFile);
fIsOpen := false;
end;
inherited;
end;
Function TCustomLogFile.openLogFile(alogFilePath : String) : boolean;
Begin
Try
fLogFile := alogFilePath;
AssignFile(FFile, fLogFile);
If not FileExists(fLogFile) then
Rewrite(FFile)
else
Append(FFile);
except
On e:
Exception do
Begin
flogfile := '';
raise
Exception.create(e.Message);
fIsOpen := false;
end;
end;
fIsOpen := true;
end;
Function TCustomLogFile.Log(aText : String) : boolean;
var s:String;
Begin
try
if StringsOut <> nil then
StringsOut.Add(aText);
except
on e:
Exception do
Begin
s := 'Interner Fehler beim Schreiben in Log-Ausgabe-Objekt. '+
'Dies ist nur dann ein normaler Vorgang wenn die Anwendung gerade '+
'beendet wird. Das Log-Ausgabe-Objekt wird nun nicht mehr '+
'angesprochen : '+E.message;
StringsOut := nil;
try
If isOpen then
Writeln(ffile,DateTimeToStr(now)+#9#9+s);
except
end;
end;
end;
if isOpen then
Begin
WriteLn(fFile,DateTimeToStr(now)+#9#9+aText);
Flush(fFile);
end;
fLastLogTime := now;
end;
Function TCustomLogFile.getLogFilePath : String;
Begin
result := fLogFile;
end;
Function TCustomLogFile.getIsOpen : boolean;
Begin
result := fIsOpen;
end;
Function TCustomLogFile.getStringsOut : TStrings;
Begin
Result := fStringsOut;
end;
Procedure TCustomLogFile.setStringsOut(aLogList : TStrings);
Begin
fStringsOut := aLogList;
end;
end.