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

本文介绍了一个用于记录程序运行日志的过程LogMsg,包括错误日志的处理方式、日志文件的保存路径设置及文件写入逻辑。通过该过程可以方便地在控制台显示和保存程序运行时的重要信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//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;

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值