Inno Setup 在安装程序中指定日志名称

Inno Setup specify log name within the installer

设置SetupLogging=yes创建一个文件:

%TEMP%\Setup Log YYYY-MM-DD #NNN.txt

有什么方法可以指定文件名吗?请注意,我知道我可以在安装结束时使用 FileCopy 重命名它 (How can I log Inno Setup installations?),但我只是想在开始时指定文件的名称,就像使用切换 /log=%TEMP%\ProductInstall.log。这可能吗?

没有。这是不可能的。 SetupLogging 的日志文件名格式是硬编码的。

如果在命令行中指定了 /LOG=,您可以做的就是检查 InitializeSetup,如果没有,则使用 /LOG= 重新生成安装程序。

虽然有点矫枉过正。

function ShellExecute(hwnd: HWND; lpOperation: string; lpFile: string;
  lpParameters: string; lpDirectory: string; nShowCmd: Integer): THandle;
  external 'ShellExecuteW@shell32.dll stdcall';

function InitializeSetup(): Boolean;
var
  HasLog: Boolean;
  Params: string;
  I: Integer;
  S: string;
  RetVal: Integer;
begin
  HasLog := False;
  Params := '';
  for I := 1 to ParamCount do
  begin
    S := ParamStr(I);
    if CompareText(Copy(S, 1, 5), '/LOG=') = 0 then
    begin
      HasLog := True;
      break;
    end;
    // Do not pass our /SL5 switch
    // This should not be needed since Inno Setup 6.2,
    // see https://groups.google.com/g/innosetup/c/pDSbgD8nbxI
    if CompareText(Copy(S, 1, 5), '/SL5=') = 0 then
    begin
      Params := Params + AddQuotes(S) + ' ';
    end;
  end;

  Result := True;
  if HasLog then
  begin
    Log('Log specified, continuing.');
  end
    else
  begin
    // add selected language, so that user is not prompted again
    Params := Params + ' /LANG=' + ActiveLanguage;
    // force logging
    Params :=
      Params + ' /LOG="' + ExpandConstant('{%TEMP}\ProductInstall.log') + '"';
    Log(Format('Log file not specified, restarting setup with [%s]', [Params]));
    RetVal :=
      ShellExecute(0, '', ExpandConstant('{srcexe}'), Params, '', SW_SHOW);
    Log(Format('Restarting setup returned [%d]', [RetVal]));
    if RetVal > 32 then
    begin
      Log('Restart with logging succeeded, aborting this instance');
      Result := False;
    end
      else
    begin
      Log(Format('Restarting with logging failed [%s], keeping this instance', [
        SysErrorMessage(RetVal)]));
    end;
  end;
end;

这是我在安装完成后想出的重命名日志的方法。这有点棘手,因为您无法在 Inno Setup 仍在使用它时重命名它,但是使用 starttimeout 我能够分离出一个单独的 cmd 进程等待一秒钟,然后重命名。

[Run]
; Rename the log file to My_setup_log.txt.
Filename: "{cmd}"; WorkingDir: "{%TEMP}"; Parameters: "/d /c start "" "" /b cmd /d /c ""timeout 1 >NUL & del My_setup_log.txt 2>NUL & ren """"""{log}"""""" My_setup_log.txt"""; Flags: runhidden

如您所见,要在引用的 cmd 参数中使用适当数量的引号将文件名括起来,需要在 Inno Setup 命令中使用 6 个引号。如果您希望在重命名的日志文件中有空格,请将六个引号也放在它的两边。是的,这意味着字符串将以九个连续的引号结束!