首页 > 解决方案 > 如何在不覆盖寄存器的情况下为 NSIS exe 生成的日志条目添加当前日期和时间的前缀?

问题描述

我想在 NSIS (Nullsoft) .exe 程序运行期间输入日志的每一行的开头写入时间戳。

调用 GetTime 函数的唯一有效模式似乎是: ${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6

这会导致覆盖调用 .nsh 文件中的寄存器($0、$1 等)。有没有办法防止这种情况?

例如:

global_defines.nsh 包含如下内容:

!macro WriteLog text
...
  FileWrite $mylogfile '${text}'
  ${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6
  FileWrite $mylogfile "$2-$1-$0 $4:$5:$6 '${text}'"
...

调用 .nsh 文件包含如下内容:

StrCpy $1 'run_this_script'
!insertmacro WriteLog "About to run: $1"
ExecDos::exec /TOWINDOW $1
pop $0
!insertmacro WriteLog "script run returned $0"

这导致 $1 设置为宏中设置的月份值。

单独的帖子: https ://stackoverflow.com/a/19622151/6388369 显示使用 $year 等,但这不会编译。

标签: nsis

解决方案


$year是一个自定义变量:

Var year

Section
${GetTime} ... $year ...
FileWrite ...
SectionEnd

如果您需要保留旧数据(通常需要在辅助宏中执行此操作),另一种选择是保存/恢复旧数据:

Section
Push $0
Push $1
Push ...
${GetTime} ... $0 $1 ...
FileWrite ...
Pop ...
Pop $1
Pop $0
SectionEnd

推荐阅读