delphi日志模块代码

unit LOG_logging;


interface
uses
  System.Win.ScktComp,  System.SysUtils, Winsock, Windows, System.Classes,
  UGlobalvariable_type;


type


  TLOG_logging = class
    txt: TextFile;  {文件}
    txtfile: AnsiString; {文件名}
    procedure ONLOG(VVGFlogtype: integer; VVGFlogstr: AnsiString);


  strict private
    VGFlogstr: AnsiString;
    VGFlogtype: integer;


    procedure Setlogstr(const Value: AnsiString);
    procedure Setlogtype(const Value: integer);
    procedure log_save;   {日志函数}
  published


  public
    constructor  Create;
    destructor destroy; override;
    procedure Execute;


    property logtype:integer read VGFlogtype write Setlogtype;
    property logstr:AnsiString read VGFlogstr write Setlogstr;
  end;


implementation


constructor TLOG_logging.Create;
begin
  inherited Create;
  if not DirectoryExists(ExtractFilePath(ParamStr(0))+'log\') then
    ForceDirectories(ExtractFilePath(ParamStr(0))+'log\');
  txtfile := ExtractFilePath(ParamStr(0))+'log\'+ FormatDateTime('YYYYMMDD',Now ) +'.log' ;
  AssignFile( txt,txtfile);
  //Resume ; //唤醒线程,开始执行Execute里的内容
end;


destructor TLOG_logging.destroy;
begin
  inherited;
end;


procedure TLOG_logging.Execute;    {建立TCP服务线程}
begin
  TRY
    txtfile := ExtractFilePath(ParamStr(0))+'log\'+ FormatDateTime('YYYYMMDD',Now ) +'.log' ;
    if fileexists(txtfile) then
    begin


    end
    else
    begin
      AssignFile( txt,txtfile);
      Rewrite(txt);    //日志文件新建或者重写原有的文件
      CloseFile(txt);
    end;


    if VGFlogtype > 0 then
    begin
      log_save;  {日志函数}
      VGFlogtype := 0;
    end;


  FINALLY
  END;


end;


procedure TLOG_logging.log_save;  {日志函数}
begin
  try
    try
      Append(txt);  //写打开文件,指针到尾
    except
      Exit;
    end;


    case VGFlogtype of
      1://异常记录
      begin
        Writeln(txt,'systemerr:'+FormatDateTime('yy-mm-dd hh:nn:ss',now) +'|'+string(VGFlogstr) );  //写入记录文件
        Writeln(txt,'' );


      end;
      2://TCP记录
      begin


        Writeln(txt,'TCP_LOG:'+FormatDateTime('yy-mm-dd hh:nn:ss',now) +'|'+string(VGFlogstr));  //写入记录文件
        Writeln(txt,'' );


      end;
      3://记录日志
      begin


        Writeln(txt,'LOG:'+FormatDateTime('yy-mm-dd hh:nn:ss',now) +'|'+string(VGFlogstr) );  //写入记录文件
        Writeln(txt,'' );


      end;
    end;


  finally
    CloseFile(txt);
  end;


end;


procedure TLOG_logging.Setlogstr(const Value: AnsiString);
begin
  VGFlogstr := Value;
end;


procedure TLOG_logging.Setlogtype(const Value: integer);
begin
  VGFlogtype := Value;
end;


procedure TLOG_logging.ONLOG(VVGFlogtype: integer; VVGFlogstr: AnsiString);
begin


  VGFlogstr := VVGFlogstr;
  VGFlogtype := VVGFlogtype;
  Execute;


end;




end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值