频道分类

delphi TLog 日志

作者:admin 来源: 日期:2020/3/28 21:54:02 人气: 标签:

 
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.

上一篇:delphi 泛型数组 StrSplit 字符串分割下一篇:没有资料