-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclsOpenAILogger.cls
144 lines (113 loc) · 4.49 KB
/
clsOpenAILogger.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsOpenAILogger"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'-----------------------------------------------------------------------------
' Project: OpenAI VBA Framework
' Class: clsOpenAILogger
' Description: Handles developer messages in the framework for Immediate Window
'
' Author: Zaid Qureshi
' GitHub: https://github.com/zq99
'
' Classes / Modules in the Framework:
' - clsOpenAI
' - clsOpenAILogger
' - clsOpenAIMessage
' - clsOpenAIMessages
' - clsOpenAIRequest
' - clsOpenAIResponse
' - IOpenAINameProvider
'
' - mdOpenAI_Tests
' - mdOpenAI_Examples
'
' This work is licensed under the MIT License. The full license text
' can be found in the LICENSE file in the root of this repository.
'
'-----------------------------------------------------------------------------
Option Explicit
Implements IOpenAINameProvider
Private mobjClass As IOpenAINameProvider
Private mblnIsMessageRequired As Boolean
Private Function IOpenAINameProvider_GetClassName() As String
IOpenAINameProvider_GetClassName = "clsOpenAILogger"
End Function
Private Function IOpenAINameProvider_ToString() As String
IOpenAINameProvider_ToString = "IsMessageRequired=" & Me.IsMessageRequired
End Function
Public Property Let IsMessageRequired(ByVal value As Boolean)
mblnIsMessageRequired = value
End Property
Public Property Get IsMessageRequired() As Boolean
IsMessageRequired = mblnIsMessageRequired
End Property
Private Sub Class_Terminate()
Set mobjClass = Nothing
End Sub
Public Sub SetClass(ByVal obj As IOpenAINameProvider)
Set mobjClass = obj
End Sub
Public Sub PrintMessage(ParamArray vntMessage() As Variant)
' Purpose: Takes in an open ended list of string variables and appends then together as a message to
' output to the immediate window
If IsEmpty(vntMessage) Then
Exit Sub
Else
Dim i As Integer
Dim strAll As String
'concatenate message elements into into one string
For i = LBound(vntMessage) To UBound(vntMessage)
strAll = strAll & IIf(Len(strAll) > 0, " | ", "") & CStr(vntMessage(i))
Next i
'output string to immediate window
If Len(Trim(strAll)) > 0 Then
Call LogMessage(strAll)
End If
End If
End Sub
Private Sub LogMessage(ByVal strMessage As String)
'Purpose: The main logging routine for messages, which can be suppressed by calling routines
If Not mobjClass Is Nothing Then
If Me.IsMessageRequired Then
Debug.Print fncGetDateTimeStamp & vbTab & mobjClass.GetClassName & " : " & strMessage
End If
End If
End Sub
Public Sub PrintCriticalMessage(ByVal strMessage As String, Optional ByVal blnIsBorderRequired As Boolean = False, Optional ByVal strBorderCharacter As String = "*", Optional ByVal blnAddModuleName As Boolean = True, Optional ByVal strLabel As String = "WARNING")
'Purpose: This method always outputs to the immediate window regardless of whether logging is set to False
If Not mobjClass Is Nothing Then
Dim strMsg As String
Dim strName As String
strName = IIf(blnAddModuleName = True, mobjClass.GetClassName, "")
strMsg = fncGetDateTimeStamp & vbTab & strName & " " & strLabel & ": " & strMessage
If blnIsBorderRequired Then
Dim strBorder As String
strBorder = String(Len(strMsg), strBorderCharacter)
Debug.Print strBorder
Debug.Print strMsg
Debug.Print strBorder
Else
Debug.Print strMsg
End If
End If
End Sub
Public Sub PrintVBAError(ByVal objErr As ErrObject)
'Purpose: This method always outputs to the immediate window regardless of whether logging is set to False
If Not mobjClass Is Nothing Then
Dim strMsg As String
Dim strName As String
strName = mobjClass.GetClassName
strMsg = fncGetDateTimeStamp & vbTab & strName & " VBA ERROR: [" & Err.Number & "]" & vbTab & Err.Description
Debug.Print strMsg
End If
End Sub
Private Function fncGetDateTimeStamp() As String
'Purpose: All logged messages are outputted with a time stamp unless caling functions suppress this
fncGetDateTimeStamp = Format(Now, "yyyy-MM-dd hh:mm:ss")
End Function