想法是在靜默模式下將NSIS腳本運行到遠程計算機,並且一旦安裝成功完成比將日誌文件返回主機。如何將DetailPrint命令消息保存在NSIS腳本中的文件中?
我有很多Detailprint
命令消息顯示腳本的進度。現在問題是我如何將這些消息保存到日誌文件。我正在研究這個http://nsis.sourceforge.net/Dump_log_to_file,但它表示它不會在靜音模式下工作。
想法是在靜默模式下將NSIS腳本運行到遠程計算機,並且一旦安裝成功完成比將日誌文件返回主機。如何將DetailPrint命令消息保存在NSIS腳本中的文件中?
我有很多Detailprint
命令消息顯示腳本的進度。現在問題是我如何將這些消息保存到日誌文件。我正在研究這個http://nsis.sourceforge.net/Dump_log_to_file,但它表示它不會在靜音模式下工作。
這是正確的,列表視圖窗口不存在於靜默模式下。
我想你有兩個選擇,推出自己的日誌記錄:
var hFileLog
!macro Log_Init logfile
FileOpen $hFileLog "${logfile}" w ;Or "a" for append
!macroend
!macro Log_String msg
DetailPrint "${msg}"
FileWrite $hFileLog "${msg}$\r$\n"
!macroend
!macro Log_Close
FileWrite $hFileLog 'Done.'
FileClose $hFileLog
!macroend
!macro File src
File "${src}"
FileWrite $hFileLog 'Extracting "${src}" to $outdir$\r$\n'
!macroend
Section "Main Program"
!insertmacro Log_Init "$instdir\install.log"
!insertmacro Log_String "Starting install..."
SetOutPath $instdir
!insertmacro File "${__FILE__}"
SectionEnd
Section "Optional Foo Component"
!insertmacro Log_String "Installing Foo"
SectionEnd
Section
!insertmacro Log_Close
SectionEnd
或增加更多的黑客,使DumpLog工作:
Function .onInit
IfSilent 0 +2
StrCpy $0 1 ;We need to know if we are in "real silent" mode
SetSilent normal ;Turn off real silent mode so we can fake it
FunctionEnd
Function .onGuiInit
StrCmp $0 1 0 +3
HideWindow
SetSilent silent ;The docs say this is only supported in .onInit but all we care about is the internal flag used by IfSilent
FunctionEnd
LicenseData "${__FILE__}"
page license skipifsilent ; All pages except instfiles needs this hack
page directory skipifsilent
page instfiles
Function skipifsilent
IfSilent 0 +3
HideWindow
Abort
FunctionEnd
Section
IfSilent 0 +2
HideWindow
SetOutPath $instdir
File "${__FILE__}"
Sleep 2222 ;So we can see that it is hidden and not just finishing quickly
DetailPrint "$(^Completed)" ; Fake the completed message show it shows in the log
Push "$EXEDIR\install.log"
Call DumpLog
IfSilent 0 +2
Quit
SetDetailsPrint textonly
SectionEnd
!define LVM_GETITEMCOUNT 0x1004
!define LVM_GETITEMTEXT 0x102D
Function DumpLog
Exch $5
Push $0
Push $1
Push $2
Push $3
Push $4
Push $6
FindWindow $0 "#32770" "" $HWNDPARENT
GetDlgItem $0 $0 1016
StrCmp $0 0 exit
FileOpen $5 $5 "w"
StrCmp $5 "" exit
SendMessage $0 ${LVM_GETITEMCOUNT} 0 0 $6
System::Alloc ${NSIS_MAX_STRLEN}
Pop $3
StrCpy $2 0
System::Call "*(i, i, i, i, i, i, i, i, i) i \
(0, 0, 0, 0, 0, r3, ${NSIS_MAX_STRLEN}) .r1"
loop: StrCmp $2 $6 done
System::Call "User32::SendMessageA(i, i, i, i) i \
($0, ${LVM_GETITEMTEXT}, $2, r1)"
System::Call "*$3(&t${NSIS_MAX_STRLEN} .r4)"
FileWrite $5 "$4$\r$\n"
IntOp $2 $2 + 1
Goto loop
done:
FileClose $5
System::Free $1
System::Free $3
exit:
Pop $6
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
Exch $5
FunctionEnd
我用這個方法來保存日誌。我不知道這是否是最好的方法,但對我來說效果很好。
!ifndef DEBUG_MODE
!define DEBUG_MODE true
!endif
!define LOGFILE "$TEMP\my_logfile.log"
!if ${DEBUG_MODE} == true
!define DetailPrint '!insertmacro _debugMsg'
!else
!define DetailPrint '!insertmacro _nodebugMsg'
!endif
!macro _debugMsg MSG
push $3
push $2
push $1
push $0
push $R0
strcpy $R0 "${MSG}"
ClearErrors
FileOpen $1 ${LOGFILE} a
FileSeek $1 0 END
IfErrors +8
${GetTime} "" "L" $0 $0 $0 $0 $0 $2 $3
FileWrite $1 "$0:$2:$3$\t"
FileWrite $1 "${__FUNCTION__}$\t"
FileWrite $1 "$R0"
FileWrite $1 "$\t(${__FILE__},${__FUNCTION__},${__LINE__})"
FileWrite $1 "$\n"
FileClose $1
pop $R0
pop $0
pop $1
pop $2
pop $3
!macroend
!macro _nodebugMsg _MSG
;you can put here nothing or you can put the regular DetailPrint
DetailPrint "${MSG}"
!macroend
之後,你只需要找到與$替換DetailPrint {} DetailPrint