Logging: Simple Text File Logging Functions and Macros

From NSIS Wiki

Author: MikeSchinkel (talk, contrib)


Description

This code provides an simple set of macros for creating a text log. Also, you can examine the log file written by the example code to see what order code is executed by NSIS.

How To Use

The is the example code that uses TextLog.nsh:

# TextLog.nsh
# Written by Mike Schinkel [http://www.mikeschinkel.com/blog/]
# 12/23/2005
#
# 9/15/2006 Added seek to end of file so it's not overwritten on each call
#           Added LogMsg so file is not locked between writes
 
!include "textlog.nsh"
 
OutFile "test.exe"
 
# This so the log will be written to same dir as the test file. 
# NOT recommended for actual installation files.
InstallDir "$EXEDIR"  
 
Page custom OnCustom AfterCustom
Page components
Page instfiles
Page custom ShowLog
 
Function .onInit
	${LogSetFileName} "$INSTDIR\MyInstallLog.txt"
	${LogSetOn}
	${LogText} "In .onInit"
FunctionEnd
 
Function OnCustom
	${LogText} "In Function OnCustom"
FunctionEnd
 
Function AfterCustom
	${LogText} "In Function AfterCustom"
FunctionEnd
 
Section one
	${LogText} "In Section One"
SectionEnd
 
Section two
	${LogText} "In Section Two"
SectionEnd
 
Section three
	${LogText} "In Section Three"
SectionEnd
 
Function ShowLog
	${LogText} "In Function ShowLog"
	ExecShell "open" "$INSTDIR\MyInstallLog.txt"
FunctionEnd
 
Section "-CleanUp"
	${LogText} "In Section -CleanUp"
	${LogSetOff}
SectionEnd

This is the result that gets written to "$INSTDIR\MyInstallLog.txt" when running the previous example:

In .onInit
In Function OnCustom
In Section One
In Section Two
In Section Three
In Section -CleanUp


And this is the macro file TextLog.nsh:

# TextLog.nsh v1.1 - 2005-12-26
# Written by Mike Schinkel [http://www.mikeschinkel.com/blog/]
 
Var /GLOBAL __TextLog_FileHandle
Var /GLOBAL __TextLog_FileName
Var /GLOBAL __TextLog_State
 
!define LogMsg '!insertmacro LogMsgCall'
!macro LogMsgCall _text
        Call LogSetOn
	Push "${_text}"
	Call LogText
	Call LogSetOff
!macroend
 
 
!define LogText '!insertmacro LogTextCall'
!macro LogTextCall _text
	Push "${_text}"
	Call LogText
!macroend
 
Function LogText
	Exch $0   	; pABC -> 0ABC
	FileWrite $__TextLog_FileHandle "$0$\r$\n"
	Pop $0		; 0ABC -> ABC
FunctionEnd
 
!define LogSetFileName '!insertmacro LogSetFileNameCall'
!macro LogSetFileNameCall _filename
	Push "${_filename}"
	Call LogSetFileName
!macroend
 
Function LogSetFileName
	Exch $0   	; pABC -> 0ABC
	StrCpy $__TextLog_FileName "$0"
	StrCmp $__TextLog_State "open" +1 +3
	Call LogSetOff
	Call LogSetOn
	Pop $0		; 0ABC -> ABC
FunctionEnd
 
!define LogSetOn '!insertmacro LogSetOnCall'
!macro LogSetOnCall
	Call LogSetOn
!macroend
 
Function LogSetOn
	StrCmp $__TextLog_FileName "" +1 AlreadySet
	StrCpy $__TextLog_FileName "$INSTDIR\install.log"
AlreadySet:
	StrCmp $__TextLog_State "open" +2
	FileOpen $__TextLog_FileHandle  "$__TextLog_FileName"  a
        FileSeek $__TextLog_FileHandle 0 END
	StrCpy $__TextLog_State "open"
FunctionEnd
 
!define LogSetOff '!insertmacro LogSetOffCall'
!macro LogSetOffCall
 	Call LogSetOff
!macroend
 
Function LogSetOff
	StrCmp $__TextLog_State "open" +1 +2
	FileClose $__TextLog_FileHandle
	StrCpy $__TextLog_State ""
FunctionEnd

Written by MikeSchinkel.

Personal tools
donate
ads