- 人气:
- 放大
- 缩小
- 二维码
- 赞赏
delphi TLog 日志
var
Lg: TLogs;
Lg := TLogs.Create('_sql.log');Lg.Log('日志内容');
Lg.Destroy();
unit TLog;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;
type
TLogs = class
private
FileName: string;
public
FRTL:TRTLCriticalSection;
function Log(const AText:AnsiString):Boolean;
public
constructor Create (FileNM:string) ; overload;
destructor Destroy;
end;
implementation
//constructor TLogs.Create;
constructor TLogs.Create (FileNM:string) ;
//var
// i:Integer;
begin
inherited Create;
FileName:= FileNM;
InitializeCriticalSection(FRTL);
end;
destructor TLogs.Destroy;
begin
DeleteCriticalSection(FRTL);
inherited Destroy;
end;
//===============
function TLogs.Log(const AText:AnsiString):Boolean;
var
LStr:AnsiString;
LFile:TextFile;
begin
try
try
EnterCriticalSection(FRTL);
Result:=True;
// LStr:=ExtractFilePath(Application.ExeName)+'log.txt';
LStr:=FileName;
AssignFile(LFile,LStr);
if FileExists(LStr) then Append(LFile) else Rewrite(LFile);
LStr:=Format('%s - %s',[DateTimeToStr(Now),AText]);
Writeln(LFile,LStr);
finally
CloseFile(LFile);
LeaveCriticalSection(FRTL);
end;
except
Result:=False;
end;
end;
end.