(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)
unit AHMTLogFile;
interface

uses SysUtils, Windows, Messages, Classes,AHMSystemClass;

type
  TAHMLogFile = class(TAHMSystemsComponent)
  private
    FLogFile: String;
    FOnClear : TNotifyevent;
    FLogHeader,FLogTime,FLogDate,FLogSpecial : Boolean;
    FSpecial,FHeader : String;
  protected
  public
    constructor Create(AOwner: TComponent); override;
    procedure Log(Logstr: String);
    procedure ClearLog;
  published
    property LogFile : String read FLogFile write FLogFile;
    property Header  : String read FHeader write FHeader;
    property LogTime : Boolean read FLogTime write FLogTime default false;
    property LogDate : Boolean read FLogDate write FLogDate default false;
    property LogSpecial : Boolean read FLogSpecial write FLogSpecial default false;
    property LogHeader  : Boolean read FLogHeader write FLogHeader default false;
    property Special    : String read FSpecial write FSpecial;
    property OnClear : TNotifyEvent read FOnClear write FOnClear;
  end;

implementation

constructor TAHMLogFile.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fLogFile := '';
  FSpecial:='Logged by User';
  FHeader:='Generic Log File for my Application';
end;

procedure TAHMLogFile.Log(Logstr: String);
var tLogFile: Text;
    Loggedstr : String;
begin
  Loggedstr:='';
  if FLogFile = '' then Exit;
  AssignFile(tLogFile, FLogFile);
  if FileExists(FLogFile) then Append(tLogFile) else
  begin
    Rewrite(tLogFile);
    if FLogHeader then writeln(tLogFile,FHeader);
  end;
  if FLogSpecial then LoggedStr:=Loggedstr+FSpecial;
  if FLogTime then LoggedStr:=Loggedstr+' '+TimetoStr(Time);
  if FLogDate then LoggedStr:=Loggedstr+' '+DateToStr(date);
  if LoggedStr<>'' then LoggedStr:=Loggedstr+' '+LogStr
  else LoggedStr:=LogStr;
  Writeln(tLogFile,Loggedstr);
  Flush(tLogFile);
  CloseFile(tLogFile);
end;

procedure TAHMLogFile.ClearLog;
var tLogFile: Text;
begin
  if FLogFile = '' then Exit;
  if assigned(FOnClear) then FOnClear(self);
  if FileExists(FLogFile) then
  begin
     AssignFile(tLogFile, FLogFile);
     Rewrite(tLogFile);
     if FLogHeader then writeln(tLogFile,FHeader);
     Flush(tLogFile);
     CloseFile(tLogFile);
  end;
end;

end.
