delphi TLog 日志  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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.

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.038029193878174 seconds