FileStream 类实现日志
FileStream类几乎可以处理所有的文件操作.以下为一个日志类,除了配置不太灵活外,挺好用的.
type
TBuffer = array [0..2000] of char;
TGameLogFile = class
private
FFullPath:string;//完整路径,用这个路径来判断当前的打开的日志的大小.
FileDate:TDateTime;
FFileParth: string; //路径
FText: Text;
FLogFileStream:TFileStream;
FIsCreateToNew: boolean; //是否是每次启动程序都创建新的记录文件 否则就是当天只会有1个文件
FIsControlFileSize:Boolean;//是否控制文件大小,true,超出文件大小时,重新创建一个log文件
public
{带入日志文件存放的目录位置}
constructor Create(Iparth: string);
destructor Destroy; override;
{写入内容即可自动记录}
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;//完整路径,用这个路径来判断当前的打开的日志的大小.
FileDate:TDateTime;
FFileParth: string; //路径
FText: Text;
FLogFileStream:TFileStream;
FIsCreateToNew: boolean; //是否是每次启动程序都创建新的记录文件 否则就是当天只会有1个文件
FIsControlFileSize:Boolean;//是否控制文件大小,true,超出文件大小时,重新创建一个log文件
public
{带入日志文件存放的目录位置}
constructor Create(Iparth: string);
destructor Destroy; override;
{写入内容即可自动记录}
procedure init(Iparth: string);
procedure AddLog(Icon: string; const LogLevel: Integer = 0);
property IsCreateToNew: boolean read FIsCreateToNew write FIsCreateToNew;
end;
implementation
uses StdCtrls;
const
{分割符号}
CSplitStr = '===============================================================';
ClogFileName = '.log';
{ TGameLogFile }
uses StdCtrls;
const
{分割符号}
CSplitStr = '===============================================================';
ClogFileName = '.log';
{ TGameLogFile }
procedure TGameLogFile.AddLog(Icon: string; const LogLevel: integer = 0);
var
txt:string;
buffer:TBuffer; //开一个2K的缓存
begin
try
if FIsCreateToNew then
if Date - FileDate >= 1 then //超过一天.强制换掉日志文件
begin
CloseFile(FText);
init(FFileParth);
end;
if FIsControlFileSize then
begin
if FLogFileStream.Size > 3 * 1000 * 1000 then //这里的单位是M,有时间改成可配置
init(FFileParth); //重新切换一个日志
end;
StrCopy(buffer,PChar(Icon));
FLogFileStream.Write(buffer,Length(Icon));//如果直接write(Icon,Length(Icon)),会产生乱码.
except
IOResult;
end;
end;
constructor TGameLogFile.Create(Iparth: string);
begin
FIsCreateToNew := false;
FIsControlFileSize := not (FIsCreateToNew xor False); //当FIsCreateToNew为true时,此变量为假
FFileParth := Iparth;
init(FFileParth);
end;
//在这里创建一个日志文件
procedure TGameLogFile.init(Iparth: string);
var
Ltep: string;
begin
if not DirectoryExists(FFileParth) then
if not CreateDir(FFileParth) then begin
raise Exception.Create('错误的路径,日志类对象不能被创建');
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用来控制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;
//读写日志文件
FLogFileStream := TFileStream.Create(FFullPath,fmOpenReadWrite or fmShareDenyNone);
end;
destructor TGameLogFile.Destroy;
begin
try
if Assigned(FLogFileStream) then
FreeAndNil(FLogFileStream);
except
end;
inherited;
end;
end.