type
TBuffer = array [0..2000] of char;
TGameLogFile = class
private
FFullPath:string;//瀹屾暣璺緞,鐢ㄨ繖涓礬寰勬潵鍒ゆ柇褰撳墠鐨勬墦寮鐨勬棩蹇楃殑澶у皬.
FileDate:TDateTime;
FFileParth: string; //璺緞
FText: Text;
FLogFileStream:TFileStream;
FIsCreateToNew: boolean; //鏄惁鏄瘡嬈″惎鍔ㄧ▼搴忛兘鍒涘緩鏂扮殑璁板綍鏂囦歡 鍚﹀垯灝辨槸褰撳ぉ鍙細(xì)鏈?涓枃浠?br /> FIsControlFileSize:Boolean;//鏄惁鎺у埗鏂囦歡澶у皬,true,瓚呭嚭鏂囦歡澶у皬鏃?閲嶆柊鍒涘緩涓涓猯og鏂囦歡
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 }
procedure TGameLogFile.AddLog(Icon: string; const LogLevel: integer = 0);
var
txt:string;
buffer:TBuffer; //寮涓涓?K鐨勭紦瀛?br />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); //閲嶆柊鍒囨崲涓涓棩蹇?br /> end;
StrCopy(buffer,PChar(Icon));
FLogFileStream.Write(buffer,Length(Icon));//濡傛灉鐩存帴write(Icon,Length(Icon)),浼?xì)漶旂敓湄曠?
except
IOResult;
end;
end;
constructor TGameLogFile.Create(Iparth: string);
begin
FIsCreateToNew := false;
FIsControlFileSize := not (FIsCreateToNew xor False); //褰揊IsCreateToNew涓簍rue鏃?姝ゅ彉閲忎負(fù)鍋?br /> FFileParth := Iparth;
init(FFileParth);
end;
//鍦ㄨ繖閲屽垱寤轟竴涓棩蹇楁枃浠?br />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鏃ュ織鏂囦歡鐨勫ぇ灝?nbsp; 2011騫?鏈?4鏃?: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.