Archive

Archive for October, 2011

Numbing the pain of VBScript with a generic logging-class

October 18th, 2011 FighterHayabusa No comments

VBScript is a pain the ass. However when working on Windows I’ve found it useful plenty of times since it’s the only scripting language supported out of the box by most Windows-flavors (although that’s changed now with PowerShell I suppose). There’s also the whole VBA-thing which crosses over into VBScript-territory in various applications. QlikView for example uses VBScript as an internal scripting language for macros and exposes a VBA-esque API for that purpose.

Anyway, the other day at work while hacking away at a humongous VBScript/VBA-macro I got tired of how much debugging my 600+ lines of VBScript-code sucked (mainly because of QlikView’s useless debugger) and threw together a simple generic logging-class that I thought I’d share. So here you go:

Class Logger

Private mLogFile
Private EFSO
Private EFP
Private mIsEnabled

Private Sub Class_Initialize()
'Do nothing
End Sub

Private Sub Class_Terminate()
If IsObject(EFP) AND TypeName(EFP) = "TextStream" Then
EFP.Close
End If
Set EFP = Nothing
Set EFSO = Nothing
End Sub

Public Default Function Init(pLogFile)
mLogFile = pLogFile
mIsEnabled = True
Call InitFileObjects
Set Init = Me
End Function

Private Sub InitFileObjects
If IsObject(EFP) AND TypeName(EFP) = "TextStream" Then
EFP.Close
End If
Set EFSO = CreateObject("Scripting.FileSystemObject")
Set EFP = EFSO.OpenTextFile(mLogFile, 8, True)
End Sub

Public Property Get logFile
logFile = mLogFile
End Property

Public Property Let logFile(pLogFile)
mLogFile = pLogFile
Call InitFileObjects
End Property

Public Property Get IsEnabled
IsEnabled = mIsEnabled
End Property

Public Property Let IsEnabled(pIsEnabled)
mIsEnabled = pIsEnabled
End Property

Public Sub doLog(pMessage)
If (mIsEnabled = True) Then
EFP.WriteLine(pMessage)
End If
End Sub

End Class

Not the hottest piece of code you’ll ever see but maybe somebody besides me will find it useful.