Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

星期一, 10月 15, 2007

如何寫入程式報告的Log檔記錄


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
m_Report: TStringList;
{ Private declarations }
public
procedure ReportLog(str: string); overload;
procedure ReportLog(str: string; Args: array of const); overload;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
//---------------------------------------------------------

procedure TForm1.FormCreate(Sender: TObject);
begin
m_Report:=TStringList.Create();
end;
//---------------------------------------------------------

procedure TForm1.FormDestroy(Sender: TObject);
begin
m_Report.SaveToFile('Report.log');
m_Report.Free();
m_Report:=nil;
end;
//---------------------------------------------------------

procedure TForm1.ReportLog(str: string);
begin
if(not Assigned(m_Report)) then
Exit;
m_Report.Add(FormatDateTime('yyyy"/"mm"/"dd" "hh:nn:ss ', Now())+str);
end;
//---------------------------------------------------------

procedure TForm1.ReportLog(str: string; Args: array of const);
var
buf: string;

begin
//Format string.
buf:=Format(str, Args);
//Put Log to Log Factory.
ReportLog(buf);
end;
//---------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);
const
sFilename = 'test.txt';
sPath = 'c:\';
begin
m_Report.Clear();
ReportLog('Prepare to clear.'); //未來修改此行
ReportLog('Begin to show %s file(s) in %s .', [sFilename, sPath]); //sFilename改sPath
ReportLog('End.');
end;

end.

沒有留言: