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

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