• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

2.2代码块--delphi写日志模块

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//2.2 代码块--写日志

//调用例句如:LogMsg('FTP上传线程终止',False,true);
procedure LogMsg(AMsg: string; const blnIsErrorMsg: boolean = False; const BoolSaveToFile: Boolean = True);
var
  strMsg: string;
begin
  try
    //FLogMemoCallFlag.Enter;
    strMsg := AMsg;

    if blnIsErrorMsg and (Pub_ProgramLog.IndexOfName(AMsg) = -1) then
      Pub_ProgramLog.Add(AMsg +'=' + FormatDateTime('yyyy-mm-dd hh:nn:ss',Now) );

    if blnIsErrorMsg then strMsg := '******' + strMsg + '******';
    if Assigned(Pub_Memo) and (Pub_BlnShowLog) then
    begin
      if Pub_Memo.Lines.Count >= 500 then Pub_Memo.Lines.Clear;
      Pub_Memo.Lines.Add(#13#10 + FormatDateTime('yyyy-mm-dd hh:nn:ss',Now) + ' -> ' + strMsg);
    end;
  finally
    //FLogMemoCallFlag.Leave;
  end;
  if BoolSaveToFile then
  begin
    try
      WriteLog(SlashSep(ExtractFilePath(Application.ExeName), 'PostContentRunLog\' + FormatDateTime('yyyy-mm-dd',Now) + '.Txt'),
        FormatDateTime('hh:nn:ss',Now) + ':' + strMsg);
    except
      ;
    end;
  end;
end;


function SlashSep(const Path, S: string): string;
begin
  if (Trim(Path) = '') or (Trim(S) = '') then
    Result := path + s
  else begin
        //检查"Path"字符串是否是以"\"结尾
    if AnsiLastChar(Path)^ <> '\' then
      Result := Path + '\' + s
    else
      Result := Path + s;
  end
end;

function WriteLog(AFileName: string; ALogCont: string; const BoolOverride: Boolean = false): integer;
var
  //TmpTextFile: TextFile;
  FileFullName: string;
  TmpStr: string;
  TmpFullDir: string;
begin
  result := -1;
  try
    TmpStr := ALogCont;
    FileFullName := AFileName;
    TmpFullDir := ExtractFilePath(FileFullName);
    if not DirectoryExists(TmpFullDir) then
      CreateDir(TmpFullDir);
    try
      AssignFile(Pub_LogTextFile, FileFullName);
      if (not BoolOverride) and FileExists(FileFullName) then
        Append(Pub_LogTextFile)
      else begin
        Rewrite(Pub_LogTextFile);
      end;
      try
        Writeln(Pub_LogTextFile, trim(TmpStr));
      finally
        CloseFile(Pub_LogTextFile);
      end;
      result := 1;
    except
      result := -1;
    end;
  except
    ;
  end;
end;

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi如何通过配置ini文件去连接SQL发布时间:2022-07-18
下一篇:
Delphi结束进程模块发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap