2017年9月7日 星期四

產生文字檔記錄Log

procedure pr_Log(sString: string);
var
  SearchRec: TSearchRec;
  sSavePath, sLogFileName, sMsg:String;
  txtFile: TextFile;
begin
  try
    sSavePath := ExtractFilePath(Application.ExeName);
    sLogFileName := sSavePath+'pr_Log.log';
    sMsg := FormatDateTime('YYYY/MM/DD HH:NN:SS', Now)+#9+sString;
    //
    if FindFirst(sLogFileName, faAnyFile, SearchRec) = 0 then begin
      if SearchRec.Size > 1000000 then
        DeleteFile(sLogFileName);
      SysUtils.FindClose(SearchRec);
    end;
    //
    if not DirectoryExists(sSavePath) then
      CreateDir(sSavePath);
    AssignFile(txtFile, sLogFileName);
    if FileExists(sLogFileName) then
      Append(txtFile)
    else
      ReWrite(txtFile);
    Writeln(txtFile, sMsg);
    CloseFile(txtFile);
  except
  end;
end;

沒有留言:

張貼留言