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.

相关阅读 >>

Delphi 判断目录是否可写

Delphi 获取cpuid的函数

Delphi 2009 之 tcategorypanelgroup[2]: headeralignment、gradientdirection

Delphi 结构体的使用、tbitmap和tmemorystream的转换

Delphi android 中 toast 的实现(老外写的unit)

Delphi里formatfloat的用法

Delphi压缩图片代码

Delphi获取flash文件的影片时长,原始尺寸,帧数等信息

Delphi tms web core 实现下载

Delphi 打包文件到apk安装包中

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...