Mega Code Archive

 
Categories / Delphi / Files
 

Persistant logfile

Title: Persistant logfile Question: I realise this can be achived by opening a file when you open your app and closing it when you end your app but i carn't be bothered to cut and paste ;-) Anyway this component is simple and useful for apps that need to create a log file. Answer: // ***************************************************************************** // Text file Logging component for Delphi32. // Copyright 2001, Gavin Uttley. All Rights Reserved. // This component can be freely used and distributed in commercial and private // environments, provided this notice is not modified in any way. // // ***************************************************************************** // Feel free to contact me if you have any questions, comments or suggestions at // gav@rootpeg.co.uk // You can always find the latest version of this component at: // http://www.rootpeg.co.uk // if you modify the code please send me a copy with an updated revision history // // ***************************************************************************** // Date last modified: 17/08/01 // ***************************************************************************** // TLogit v1.0 // ***************************************************************************** // Description: // A component that allows application wide persistant log files // Properties: // property Active : Boolean; // property LogName : string; // property AppendToLog : Boolean; // property DateTimeStamp : Boolean; // Procedures: // procedure WriteMsg(Sender: TObject; Msg: String); // procedure WriteLogln(Msg: string); // ***************************************************************************** // Revision History: // 1.0: Initial release // ***************************************************************************** unit Logit; interface uses SysUtils, Classes; type TLogit = class(TComponent) public constructor Create(AOwner: TComponent); override; procedure Free; private FLogFile : TextFile; FActive : Boolean; FLogName : String; FAppend : Boolean; FStamp : Boolean; procedure WriteLog(s: string); //internal write procedure SetActive(Value: Boolean); published property Active : Boolean read FActive write SetActive default False; property LogName : string read FLogName write FLogName; property AppendToLog : Boolean read FAppend write FAppend default True; //Append to log if it exists property DateTimeStamp: Boolean read FStamp write FStamp default False; //first writes date/time start of ln. procedure WriteMsg(Sender: TObject; Msg: String); // use this for Callback fired logging events. params must be same. procedure WriteLogln(Msg: string); // use this for any other writing to the log end; procedure Register; implementation procedure Register; begin RegisterComponents('Misc', [TLogit]); end; constructor TLogIt.Create(AOwner: TComponent); begin inherited Create(AOwner); end; procedure TLogIt.Free; begin if FLogName '' then CloseFile(FLogFile); end; procedure TLogIt.SetActive(Value: Boolean); begin if Value then begin //if set active to true if FLogName = '' then //does the log have name? raise Exception.Create('Log name not set'); //warn user Assignfile(FLogFile, FLogName); //assign log to filename if FAppend and (fileexists(FLogName))then //if file exists AND to be appended Append(FLogFile) else Rewrite(FLogFile); //else create file if new or not appended end; FActive := Value; //Set Active value end; procedure TLogIt.WriteLog(s: String); begin if FActive then begin //is the log open? if FStamp then //do you want a timestamp? WriteLn(FLogFile, FormatDateTime('dd/mm/yy hh:nn:ss',now) +' '+ s) else WriteLn(FLogFile, s); end else begin Free; //just in case. raise Exception.Create('Cannot writeln, Log not open'); //warn user end; end; procedure TLogit.WriteMsg(Sender: TObject; msg:string); begin WriteLog(Msg); //use internal write procedure end; procedure TLogit.WriteLogln(msg: string); begin WriteLog(Msg); //use internal write procedure end; end.