November 21, 2010

How to create a Custom User friendly Log for InstallScript?

This article explains how to create your own logging for InstallScript / InstallScript MSI project or Custom Actions writted in InstallScript.

Use the following code-snippet to create Custom Logging for InstallScript

// Prototype Definition
prototype ISfn_CreateLogFile();
prototype BOOL ISfn_LogError(STRING, STRING, STRING);


// Global Declaration
STRING gLogFilePath,gLogFileName,sgLogfile, gsvProductName; // For Log file
BOOL IsLogCreated,IsLogWritten,IsTempLogPath; // For Log file


Add the following code in OnBegin event for InstallScript / InstallScript MSI project or just before invoking ISfn_LogError function.
// Read the ProductName to a variable
nvSize = 256;
MsiGetProperty(ISMSI_HANDLE,"ProductName",gsvProductName,nvSize);

//Create the Log File
ISfn_CreateLogFile();

 

How to invoke ISfn_LogError function for Custom Log?
For Example:
ISfn_LogError("Local system name", "From Wscript : '" + svLocalSystemName + "'" ,"Success")
- first parameter states that the particular step is perfomed while getting Local System Name,
- the second parameter logs the retrieved local system name, the third parameter
- the third parameter determines that the actions output is a Success
Function Definition:
Copy the following function definition to your .rul file:
//---------------------------------------------------------------------------
// ISfn_CreateLogFile()
// Description: This function creates the installation log file in the format of
// PRODUCTNAME_Install_<InstallDate>__<InstallTime>.txt under
// Temp folder of the currently logged in user.
//---------------------------------------------------------------------------

function ISfn_CreateLogFile()


STRING sInstallerLogFilePath,sProdName;
STRING sSysDate,sSysTime,sHhr,sMin,sSec,svString,sLogFileName;
NUMBER nvResult,nPos,nHhrLen,nMinLen;
STRING szErrMsg;
NUMBER nvFileHandle;
begin


sInstallerLogFilePath="";
sInstallerLogFilePath = TempFolder;
if (ExistsDir(sInstallerLogFilePath)=0) then
         gLogFilePath = sInstallerLogFilePath;
else
         sInstallerLogFilePath = "";
         GetDisk(TempFolder,sInstallerLogFilePath);
         gLogFilePath = sInstallerLogFilePath + "\\";
         IsTempLogPath = TRUE;
endif;

// Creating the Log File Name
sProdName = gsvProductName;
//Frame the Log File Name
// Get the current date.
GetSystemInfo (DATE, nvResult, sSysDate);
// Get the current time.
GetSystemInfo (TIME, nvResult, sSysTime);
nPos = StrFind(sSysTime,":");
//Get the Hour part from the Date
StrSub ( sHhr, sSysTime, 0, nPos );
nHhrLen = StrLength(sHhr);
nHhrLen = nHhrLen + 1;
//Get the Minutes part from the Date
StrSub ( sMin, sSysTime, nHhrLen, 2 );
nMinLen = StrLength(sMin);
nMinLen = nHhrLen + nMinLen + 1;
//Get the Seconds part from the Date
StrSub ( sSec, sSysTime, nMinLen, 2 );
sSysTime = sHhr + "_" + sMin + "_" + sSec;
//Frame the final Log File name
if (!MAINTENANCE) then
          sLogFileName = sProdName + "_Install_" + sSysDate + "__" + sSysTime +".txt";
else
          sLogFileName = sProdName + "_UnInstall_" + sSysDate + "__" + sSysTime +".txt";
endif;
gLogFileName = sLogFileName;
// Set the file mode to append.
OpenFileMode (FILE_MODE_APPEND);
// Create a new file and leave it open.
if (CreateFile (nvFileHandle, gLogFilePath, sLogFileName) != 0) then
        // Report the error.
       MessageBox ("Error log file creation failed. Installation continues...", INFORMATION);
       IsLogCreated = FALSE;
else
       IsLogCreated = TRUE;endif;
// Set the message to write to the file.
// Append the message to the file.
// Write the Header Informations into the file
szErrMsg = "*******************************************************************************************************************";

if (WriteLine(nvFileHandle, szErrMsg) != 0) then
       // Report the error.
       MessageBox ("Errors couldnot be logged into the log file.     Installation continues...", INFORMATION);
       IsLogWritten = FALSE;
else
        IsLogWritten = TRUE;
        sgLogfile = gLogFilePath ^ sLogFileName;
endif;

if (IsLogWritten = TRUE)then
endif;
szErrMsg = "";
if (!MAINTENANCE) then
        szErrMsg = " "+ gsvProductName +" Installation Log ";
else
        szErrMsg = " "+ gsvProductName +" UnInstallation Log ";
endif;
WriteLine(nvFileHandle, szErrMsg);
szErrMsg = "";
szErrMsg = " Created on " + sSysDate + "__" + sSysTime + " ";
WriteLine(nvFileHandle, szErrMsg);
szErrMsg = "";
szErrMsg = "*******************************************************************************************************************";
WriteLine(nvFileHandle, szErrMsg);
szErrMsg = "";

// Close the file.
CloseFile (nvFileHandle);
end; // end of ISfn_CreateLogFile


 //---------------------------------------------------------------------------
// ISfn_LogError()
// Description: This function logs the installation actions in the Installation
// log file which was created using ISfn_CreateLogFile() function
// Input Parameters:
// ActionRequested: Action name/detail that is related to that particular action
// Parameters: Data that are passed to that particular function / action
// Status: Determines the output of that particular action(Information/Success/Error)
//---------------------------------------------------------------------------
function BOOL ISfn_LogError(ActionRequested, Parameters, Status)


NUMBER nvFileHandle,nvResult;
STRING szErrMsg,sSysDate,sSysTime;
begin
end; // end of ISfn_LogError
if (IsLogCreated=TRUE && IsLogWritten = TRUE) then
GetSystemInfo (DATE, nvResult, sSysDate);
GetSystemInfo (TIME, nvResult, sSysTime);
szErrMsg = "";
if Status = "Error" then
szErrMsg = "[" + sSysDate + "," + sSysTime + "] ERROR " + ActionRequested + " :: " + Parameters + " :: " + Status;
else
szErrMsg = "[" + sSysDate + "," + sSysTime + "] " + ActionRequested + " :: " + Parameters + " :: " + Status;
endif;

OpenFileMode (FILE_MODE_APPEND);
if (OpenFile (nvFileHandle, gLogFilePath, gLogFileName) = 0) then
WriteLine(nvFileHandle, szErrMsg);
endif;
CloseFile (nvFileHandle);
szErrMsg = "";
endif;