Logging: Simple Text File Logging Functions and Macros

From NSIS Wiki
Revision as of 08:16, 23 December 2005 by MikeSchinkel (talk | contribs) (Macro For Simple Text File Log moved to Logging: Simple Text File Logging Macro)
Jump to navigationJump to search
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
 
!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 Three
In Section -CleanUp


And this is the macro file TextLog.nsh:

# TextLog.nsh
# Written by Mike Schinkel [http://www.mikeschinkel.com/blog/]
# 12/23/2005
 
Var /GLOBAL __TextLogFileHandle
Var /GLOBAL __TextLogFileName
Var /GLOBAL __TextLogState
 
!define LogText '!insertmacro LOG_TEXT'
!define LogSetFileName '!insertmacro LOG_SET_FILE_NAME'
!define LogSetOn '!insertmacro LOG_SET_ON'
!define LogSetOff '!insertmacro LOG_SET_OFF'
 
!macro LOG_TEXT TEXT
	FileWrite $__TextLogFileHandle "${TEXT}$\r$\n"
!macroend
 
!macro LOG_SET_FILE_NAME filename
	StrCpy $__TextLogFileName "${filename}"
	StrCmp $__TextLogState "open" +1 +3
	!insertmacro LOG_SET_OFF
 	!insertmacro LOG_SET_ON
!macroend
 
!macro LOG_SET_ON
	!define UniqueID ${__LINE__}
	StrCmp $__TextLogFileName "" +1 AlreadySet_${UniqueID}
	StrCpy $__TextLogFileName "$INSTDIR\install.log"
AlreadySet_${UniqueID}:
	StrCmp $__TextLogState "open" +2
	FileOpen $__TextLogFileHandle  "$__TextLogFileName"  a
	StrCpy $__TextLogState "open"
	!undef UniqueID
!macroend
 
!macro LOG_SET_OFF
	StrCmp $__TextLogState "open" +1 +2
	FileClose $__TextLogFileHandle
	StrCpy $__TextLogState ""
!macroend

Written by MikeSchinkel.