FileStream 類實(shí)現(xiàn)日志
FileStream類幾乎可以處理所有的文件操作.
以下為一個(gè)日志類,除了配置不太靈活外,挺好用的.
procedure TGameLogFile.AddLog(Icon: string; const LogLevel: integer = 0);
var
txt:string;
buffer:TBuffer; //開(kāi)一個(gè)2K的緩存
begin
try
if FIsCreateToNew then
if Date - FileDate >= 1 then //超過(guò)一天.強(qiáng)制換掉日志文件
begin
CloseFile(FText);
init(FFileParth);
end;
if FIsControlFileSize then
begin
if FLogFileStream.Size > 3 * 1000 * 1000 then //這里的單位是M,有時(shí)間改成可配置
init(FFileParth); //重新切換一個(gè)日志
end;
StrCopy(buffer,PChar(Icon));
FLogFileStream.Write(buffer,Length(Icon));//如果直接write(Icon,Length(Icon)),會(huì)產(chǎn)生亂碼.
except
IOResult;
end;
end;
constructor TGameLogFile.Create(Iparth: string);
begin
FIsCreateToNew := false;
FIsControlFileSize := not (FIsCreateToNew xor False); //當(dāng)FIsCreateToNew為true時(shí),此變量為假
FFileParth := Iparth;
init(FFileParth);
end;
//在這里創(chuàng)建一個(gè)日志文件
procedure TGameLogFile.init(Iparth: string);
var
Ltep: string;
begin
if not DirectoryExists(FFileParth) then
if not CreateDir(FFileParth) then begin
raise Exception.Create('錯(cuò)誤的路徑,日志類對(duì)象不能被創(chuàng)建');
exit;
end;
if FIsCreateToNew then begin
Ltep := FormatDateTime('yyyymmddhhnnss', Now);
FileClose(FileCreate(FFileParth + ltep + ClogFileName));
end
else
Ltep := FormatDateTime('yyyymmddhhnnss', Now);
if not FileExists(FFileParth + ltep + ClogFileName) then
FileClose(FileCreate(FFileParth + ltep + ClogFileName));
FileDate := Date;
FFullPath := FFileParth + ltep + ClogFileName;
//此處改用TFileStream用來(lái)控制Log日志文件的大小 2011年8月24日9:28:25 ddz
//AssignFile(FText, FFileParth + ltep + ClogFileName);
if Assigned(FLogFileStream) then
FLogFileStream.Free;
//新建日志文件.
FLogFileStream := TFileStream.Create(FFullPath,fmCreate or fmShareDenyNone);
FLogFileStream.free;
//讀寫(xiě)日志文件
FLogFileStream := TFileStream.Create(FFullPath,fmOpenReadWrite or fmShareDenyNone);
end;
destructor TGameLogFile.Destroy;
begin
try
if Assigned(FLogFileStream) then
FreeAndNil(FLogFileStream);
except
end;
inherited;
end;
end.
以下為一個(gè)日志類,除了配置不太靈活外,挺好用的.
type
TBuffer = array [0..2000] of char;
TGameLogFile = class
private
FFullPath:string;//完整路徑,用這個(gè)路徑來(lái)判斷當(dāng)前的打開(kāi)的日志的大小.
FileDate:TDateTime;
FFileParth: string; //路徑
FText: Text;
FLogFileStream:TFileStream;
FIsCreateToNew: boolean; //是否是每次啟動(dòng)程序都創(chuàng)建新的記錄文件 否則就是當(dāng)天只會(huì)有1個(gè)文件
FIsControlFileSize:Boolean;//是否控制文件大小,true,超出文件大小時(shí),重新創(chuàng)建一個(gè)log文件
public
{帶入日志文件存放的目錄位置}
constructor Create(Iparth: string);
destructor Destroy; override;
{寫(xiě)入內(nèi)容即可自動(dòng)記錄}
procedure init(Iparth: string);
procedure AddLog(Icon: string; const LogLevel: Integer = 0);
property IsCreateToNew: boolean read FIsCreateToNew write FIsCreateToNew;
end;
TBuffer = array [0..2000] of char;
TGameLogFile = class
private
FFullPath:string;//完整路徑,用這個(gè)路徑來(lái)判斷當(dāng)前的打開(kāi)的日志的大小.
FileDate:TDateTime;
FFileParth: string; //路徑
FText: Text;
FLogFileStream:TFileStream;
FIsCreateToNew: boolean; //是否是每次啟動(dòng)程序都創(chuàng)建新的記錄文件 否則就是當(dāng)天只會(huì)有1個(gè)文件
FIsControlFileSize:Boolean;//是否控制文件大小,true,超出文件大小時(shí),重新創(chuàng)建一個(gè)log文件
public
{帶入日志文件存放的目錄位置}
constructor Create(Iparth: string);
destructor Destroy; override;
{寫(xiě)入內(nèi)容即可自動(dòng)記錄}
procedure init(Iparth: string);
procedure AddLog(Icon: string; const LogLevel: Integer = 0);
property IsCreateToNew: boolean read FIsCreateToNew write FIsCreateToNew;
end;
implementation
uses StdCtrls;
const
{分割符號(hào)}
CSplitStr = '===============================================================';
ClogFileName = '.log';
{ TGameLogFile }
uses StdCtrls;
const
{分割符號(hào)}
CSplitStr = '===============================================================';
ClogFileName = '.log';
{ TGameLogFile }
procedure TGameLogFile.AddLog(Icon: string; const LogLevel: integer = 0);
var
txt:string;
buffer:TBuffer; //開(kāi)一個(gè)2K的緩存
begin
try
if FIsCreateToNew then
if Date - FileDate >= 1 then //超過(guò)一天.強(qiáng)制換掉日志文件
begin
CloseFile(FText);
init(FFileParth);
end;
if FIsControlFileSize then
begin
if FLogFileStream.Size > 3 * 1000 * 1000 then //這里的單位是M,有時(shí)間改成可配置
init(FFileParth); //重新切換一個(gè)日志
end;
StrCopy(buffer,PChar(Icon));
FLogFileStream.Write(buffer,Length(Icon));//如果直接write(Icon,Length(Icon)),會(huì)產(chǎn)生亂碼.
except
IOResult;
end;
end;
constructor TGameLogFile.Create(Iparth: string);
begin
FIsCreateToNew := false;
FIsControlFileSize := not (FIsCreateToNew xor False); //當(dāng)FIsCreateToNew為true時(shí),此變量為假
FFileParth := Iparth;
init(FFileParth);
end;
//在這里創(chuàng)建一個(gè)日志文件
procedure TGameLogFile.init(Iparth: string);
var
Ltep: string;
begin
if not DirectoryExists(FFileParth) then
if not CreateDir(FFileParth) then begin
raise Exception.Create('錯(cuò)誤的路徑,日志類對(duì)象不能被創(chuàng)建');
exit;
end;
if FIsCreateToNew then begin
Ltep := FormatDateTime('yyyymmddhhnnss', Now);
FileClose(FileCreate(FFileParth + ltep + ClogFileName));
end
else
Ltep := FormatDateTime('yyyymmddhhnnss', Now);
if not FileExists(FFileParth + ltep + ClogFileName) then
FileClose(FileCreate(FFileParth + ltep + ClogFileName));
FileDate := Date;
FFullPath := FFileParth + ltep + ClogFileName;
//此處改用TFileStream用來(lái)控制Log日志文件的大小 2011年8月24日9:28:25 ddz
//AssignFile(FText, FFileParth + ltep + ClogFileName);
if Assigned(FLogFileStream) then
FLogFileStream.Free;
//新建日志文件.
FLogFileStream := TFileStream.Create(FFullPath,fmCreate or fmShareDenyNone);
FLogFileStream.free;
//讀寫(xiě)日志文件
FLogFileStream := TFileStream.Create(FFullPath,fmOpenReadWrite or fmShareDenyNone);
end;
destructor TGameLogFile.Destroy;
begin
try
if Assigned(FLogFileStream) then
FreeAndNil(FLogFileStream);
except
end;
inherited;
end;
end.
posted on 2011-08-24 06:52 嘰哩咕嚕 閱讀(459) 評(píng)論(0) 編輯 收藏 所屬分類: delphi