-
Notifications
You must be signed in to change notification settings - Fork 7
/
VBSLogger.vbs
153 lines (128 loc) · 5.86 KB
/
VBSLogger.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'A lightweight VBScript logger
'Instantiation
'<pre> With CreateObject( "VBScripting.Includer" ) <br /> Execute .Read( "VBSLogger" ) <br /> End With <br /> Dim log : Set log = New VBSLogger </pre>
'
'Usage method one. This method has the advantage that the log doesn't remain open, allowing other scripts to write to the log.
' <pre> log "test one" </pre>
'Usage method two. This method has the advantage that the name of the calling script is not written on each line of the log.
' <pre> log.Open <br /> log.Write "test two" <br /> log.Close </pre>
'
Class VBSLogger
Private streamer, dt, fs
Private sh
Private fso
Private stream, logFile, logFolder, viewer
Private scriptName, scriptFullName
Sub Class_Initialize
With CreateObject( "VBScripting.Includer" ) 'get class dependencies
Execute .Read( "TimeFunctions" )
Execute .Read( "TextStreamer" )
Execute .Read( "VBSFileSystem" )
End With
Set dt = New TimeFunctions
Set streamer = New TextStreamer
Set fs = New VBSFileSystem
Set sh = CreateObject( "WScript.Shell" )
Set fso = CreateObject( "Scripting.FileSystemObject" )
On Error Resume Next
scriptFullName = WScript.ScriptFullName
If Err Then scriptFullName = Replace(Replace(document.location.href, "file:///", ""), "%20", "") '.hta file
On Error Goto 0
scriptName = fso.GetFileName(scriptFullName)
SetLogFolder GetDefaultLogFolder
SetViewer Notepad
dt.LetDOWBeAbbreviated = True 'DOW = day of the week
UpdateLogFilePath Now
End Sub
Property Get Notepad : Notepad = "Notepad" : End Property
'Method Log
'Parameter: a string
'Remark: Opens the log file, writes the specified string, then closes the log file. This is the default method for the VBSLogger class.
Public Default Sub Log(msg) 'open the log file for appending, write the message, and then close the text stream
PrivateOpen
stream.WriteLine dt.GetFormattedTime(Now) & " - " & scriptName & ": " & msg
PrivateClose
End Sub
'Method SetLogFolder
'Parameter: a folder path
'Remark: Optional. Customize the log folder. The folder will be created if it does not exist. Environment variables are allowed. See GetDefaultLogFolder.
Sub SetLogFolder(newLogFolder) 'set the log folder; create it if necesssary
logFolder = fs.Expand(newLogFolder)
If Not fs.MakeFolder(logFolder) Then Err.Raise 17, "VBSLogger.SetLogFolder", "Failed to create log folder " & logFolder
End Sub
Property Get GetLogFolder : GetLogFolder = logFolder : End Property
Sub UpdateLogFilePath(date_) 'ensure that the filespec for the log file reflects the specified/current date
logFile = logFolder & "\" & dt.GetFormattedDay(date_) & ".txt"
End Sub
Private Sub PrivateOpen 'open the log file as a text stream for appending
UpdateLogFilePath Now
streamer.SetFile logFile
PrivateClose 'attempt to close the stream, in case it is already open
Set stream = streamer.Open
End Sub
'Method Open
'Remark: Opens the log file for writing. The log file is opened and remains open for writing. While it is open, other processes/scripts will be unable to write to it.
Sub Open
PrivateOpen
stream.WriteLine dt.GetFormattedTime(Now) & " - log opened by " & scriptName
End Sub
'Method Write
'Parameter: a string
'Remark: Writes the specified string to the log file.
Sub Write(msg) 'write to the log with timestamp
stream.WriteLine dt.GetFormattedTime(Now) & " - " & msg
End Sub
'Method Close
'Remark: Closes the log file text stream, enabling other process to write to it.
Sub Close
stream.WriteLine dt.GetFormattedTime(Now) & " - log closed by " & scriptName
PrivateClose
End Sub
Private Sub PrivateClose 'close the text stream and free up object memory
On Error Resume Next
stream.Close
Set stream = Nothing
On Error Goto 0
End Sub
'Method View
'Remark: Opens the log file for viewing. Notepad is the default editor. See SetViewer.
Sub View
If fso.FileExists(GetLogFilePath) Then
sh.Run """" & viewer & """ """ & logFile & """"
Else
Dim msg : msg = "Today's log file hasn't been created " & _
"yet. Do you want to open the log folder?"
If vbOK = MsgBox(msg, vbOKCancel + vbInformation, scriptName) Then
ViewFolder
End If
End If
End Sub
'Method SetViewer
'Parameter: a filespec
'Remark: Optional. Customize the program that the View method uses to view log files. Default: Notepad.
Sub SetViewer(newViewer) : viewer = newViewer : End Sub
'Method ViewFolder
'Remark: Open the log folder
Sub ViewFolder
sh.Run "explorer """ & logFolder & """"
End Sub
'Property WordPad
'Returns a filespec
'Remark: Can be used as the argument for the SetViewer method in order to open files with WordPad when the View method is called.
Property Get WordPad : Wordpad = "%ProgramFiles%\Windows NT\Accessories\wordpad.exe" : End Property
'Property GetDefaultLogFolder
'Returns a folder
'Remark: Retrieves the default log folder, %AppData%\VBScripting\logs
Property Get GetDefaultLogFolder
GetDefaultLogFolder = "%AppData%\VBScripting\logs"
End Property
'Property GetLogFilePath
'Returns a filespec
'Remark: Retreives the filespec for the log file, with environment variables expanded. Default: <GetDefaultLogFolder>\YYYY-MM-DD-DayOfWeek.txt
Property Get GetLogFilePath : GetLogFilePath = logFile : End Property
Sub Class_Terminate
PrivateClose
Set sh = Nothing
Set fso = Nothing
End Sub
End Class