bump product version to 5.0.4.1
[LibreOffice.git] / wizards / source / access2base / Trace.xba
blob3c2943a7be96010f688411ccfd9f6816cc1e03a8
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Trace" script:language="StarBasic">REM =======================================================================================================================
4 REM === The Access2Base library is a part of the LibreOffice project. ===
5 REM === Full documentation is available on http://www.access2base.com ===
6 REM =======================================================================================================================
8 Option Explicit
10 Public Const cstLogMaxEntries = 20
12 REM Typical Usage
13 REM TraceLog(&quot;INFO&quot;, &quot;The OK button was pressed&quot;)
14 REM
15 REM Typical Usage for error logging
16 REM Sub MySub()
17 REM On Local Error GoTo Error_Sub
18 REM ...
19 REM Exit_Sub:
20 REM Exit Sub
21 REM Error_Sub:
22 REM TraceError(&quot;ERROR&quot;, Err, &quot;MySub&quot;, Erl)
23 REM GoTo Exit_Sub
24 REM End Sub
25 REM
26 REM To display the current logged traces and/or to set parameters
27 REM TraceConsole()
29 REM -----------------------------------------------------------------------------------------------------------------------
30 Public Sub TraceConsole()
31 &apos; Display the Trace dialog with current trace log values and parameter choices
32 If _ErrorHandler() Then On Local Error Goto Error_Sub
34 Dim sLineBreak As String, oDialogLib As Object, oTraceDialog As Object
35 sLineBreak = Chr(10)
37 Set oDialogLib = DialogLibraries
38 If oDialogLib.hasByName(&quot;Access2BaseDev&quot;) Then
39 If Not oDialogLib.IsLibraryLoaded(&quot;Access2BaseDev&quot;) Then oDialogLib.loadLibrary(&quot;Access2BaseDev&quot;)
40 Set oTraceDialog = CreateUnoDialog(DialogLibraries.Access2BaseDev.dlgTrace)
41 Else
42 If Not oDialogLib.IsLibraryLoaded(&quot;Access2Base&quot;) Then oDialogLib.loadLibrary(&quot;Access2Base&quot;)
43 Set oTraceDialog = CreateUnoDialog(DialogLibraries.Access2Base.dlgTrace)
44 EndIf
45 oTraceDialog.Title = _GetLabel(&quot;DLGTRACE_TITLE&quot;) &apos; HelpText ???
47 Dim oEntries As Object, oTraceLog As Object, oClear As Object, oMinLevel As Object, oNbEntries As Object, oDump As Object
48 Dim oControl As Object
49 Dim i As Integer, sText As String, iOKCancel As Integer
51 Set oNbEntries = oTraceDialog.Model.getByName(&quot;numNbEntries&quot;)
52 oNbEntries.Value = _A2B_.TraceLogCount
53 oNbEntries.HelpText = _GetLabel(&quot;DLGTRACE_LBLNBENTRIES_HELP&quot;)
55 Set oControl = oTraceDialog.Model.getByName(&quot;lblNbEntries&quot;)
56 oControl.Label = _GetLabel(&quot;DLGTRACE_LBLNBENTRIES_LABEL&quot;)
57 oControl.HelpText = _GetLabel(&quot;DLGTRACE_LBLNBENTRIES_HELP&quot;)
59 Set oEntries = oTraceDialog.Model.getByName(&quot;numEntries&quot;)
60 If _A2B_.TraceLogMaxEntries = 0 Then _A2B_.TraceLogMaxEntries = cstLogMaxEntries
61 oEntries.Value = _A2B_.TraceLogMaxEntries
62 oEntries.HelpText = _GetLabel(&quot;DLGTRACE_LBLENTRIES_HELP&quot;)
64 Set oControl = oTraceDialog.Model.getByName(&quot;lblEntries&quot;)
65 oControl.Label = _GetLabel(&quot;DLGTRACE_LBLENTRIES_LABEL&quot;)
66 oControl.HelpText = _GetLabel(&quot;DLGTRACE_LBLENTRIES_HELP&quot;)
68 Set oDump = oTraceDialog.Model.getByName(&quot;cmdDump&quot;)
69 oDump.Enabled = 0
70 oDump.Label = _GetLabel(&quot;DLGTRACE_CMDDUMP_LABEL&quot;)
71 oDump.HelpText = _GetLabel(&quot;DLGTRACE_CMDDUMP_HELP&quot;)
73 Set oTraceLog = oTraceDialog.Model.getByName(&quot;txtTraceLog&quot;)
74 oTraceLog.HelpText = _GetLabel(&quot;DLGTRACE_TXTTRACELOG_HELP&quot;)
75 If UBound(_A2B_.TraceLogs) &gt;= 0 Then &apos; Array yet initialized
76 oTraceLog.HardLineBreaks = True
77 sText = &quot;&quot;
78 If _A2B_.TraceLogCount &gt; 0 Then
79 If _A2B_.TraceLogCount &lt; _A2B_.TraceLogMaxEntries Then i = -1 Else i = _A2B_.TraceLogLast
81 If i &lt; _A2B_.TraceLogMaxEntries - 1 Then i = i + 1 Else i = 0
82 If Len(_A2B_.TraceLogs(i)) &gt; 11 Then
83 sText = sText &amp; Right(_A2B_.TraceLogs(i), Len(_A2B_.TraceLogs(i)) - 11) &amp; sLineBreak &apos; Skip date in display
84 End If
85 Loop While i &lt;&gt; _A2B_.TraceLogLast
86 oDump.Enabled = 1 &apos; Enable DumpToFile only if there is something to dump
87 End If
88 If Len(sText) &gt; 0 Then sText = Left(sText, Len(sText) - 1) &apos; Skip last linefeed
89 oTraceLog.Text = sText
90 Else
91 oTraceLog.Text = _GetLabel(&quot;DLGTRACE_TXTTRACELOG_TEXT&quot;)
92 End If
94 Set oClear = oTraceDialog.Model.getByName(&quot;chkClear&quot;)
95 oClear.State = 0 &apos; Unchecked
96 oClear.HelpText = _GetLabel(&quot;DLGTRACE_LBLCLEAR_HELP&quot;)
98 Set oControl = oTraceDialog.Model.getByName(&quot;lblClear&quot;)
99 oControl.Label = _GetLabel(&quot;DLGTRACE_LBLCLEAR_LABEL&quot;)
100 oControl.HelpText = _GetLabel(&quot;DLGTRACE_LBLCLEAR_HELP&quot;)
102 Set oMinLevel = oTraceDialog.Model.getByName(&quot;cboMinLevel&quot;)
103 If _A2B_.MinimalTraceLevel = 0 Then _A2B_.MinimalTraceLevel = _TraceLevel(TRACEERRORS)
104 oMinLevel.Text = _TraceLevel(_A2B_.MinimalTraceLevel)
105 oMinLevel.HelpText = _GetLabel(&quot;DLGTRACE_LBLMINLEVEL_HELP&quot;)
107 Set oControl = oTraceDialog.Model.getByName(&quot;lblMinLevel&quot;)
108 oControl.Label = _GetLabel(&quot;DLGTRACE_LBLMINLEVEL_LABEL&quot;)
109 oControl.HelpText = _GetLabel(&quot;DLGTRACE_LBLMINLEVEL_HELP&quot;)
111 Set oControl = oTraceDialog.Model.getByName(&quot;cmdOK&quot;)
112 oControl.Label = _GetLabel(&quot;DLGTRACE_CMDOK_LABEL&quot;)
113 oControl.HelpText = _GetLabel(&quot;DLGTRACE_CMDOK_HELP&quot;)
115 Set oControl = oTraceDialog.Model.getByName(&quot;cmdCancel&quot;)
116 oControl.Label = _GetLabel(&quot;DLGTRACE_CMDCANCEL_LABEL&quot;)
117 oControl.HelpText = _GetLabel(&quot;DLGTRACE_CMDCANCEL_HELP&quot;)
119 iOKCancel = oTraceDialog.Execute()
121 Select Case iOKCancel
122 Case 1 &apos; OK
123 If oClear.State = 1 Then
124 _A2B_.TraceLogs() = Array() &apos; Erase logged traces
125 _A2B_.TraceLogCount = 0
126 End If
127 If oMinLevel.Text &lt;&gt; &quot;&quot; Then _A2B_.MinimalTraceLevel = _TraceLevel(oMinLevel.Text)
128 If oEntries.Value &lt;&gt; 0 And oEntries.Value &lt;&gt; _A2B_.TraceLogMaxEntries Then
129 _A2B_.TraceLogs() = Array()
130 _A2B_.TraceLogMaxEntries = oEntries.Value
131 End If
132 Case 0 &apos; Cancel
133 Case Else
134 End Select
136 Exit_Sub:
137 If Not IsNull(oTraceDialog) Then oTraceDialog.Dispose()
138 Exit Sub
139 Error_Sub:
140 With _A2B_
141 .TraceLogs() = Array()
142 .TraceLogCount = 0
143 .TraceLogLast = 0
144 End With
145 GoTo Exit_Sub
146 End Sub &apos; TraceConsole V1.1.0
148 REM -----------------------------------------------------------------------------------------------------------------------
149 Public Sub TraceError(ByVal psErrorLevel As String _
150 , ByVal piErrorCode As Integer _
151 , ByVal psErrorProc As String _
152 , ByVal piErrorLine As Integer _
153 , ByVal Optional pvMsgBox As Variant _
154 , ByVal Optional pvArgs As Variant _
156 &apos; store error codes in trace buffer
158 On Local Error Resume Next
159 If IsEmpty(_A2B_) Then Call Application._RootInit() &apos; First use of Access2Base in current LibO/AOO session
161 Dim sErrorText As String, sErrorDesc As String, oDb As Object
162 sErrorDesc = _ErrorMessage(piErrorCode, pvArgs)
163 sErrorText = _GetLabel(&quot;ERR#&quot;) &amp; CStr(piErrorCode) _
164 &amp; &quot; (&quot; &amp; sErrorDesc &amp; &quot;) &quot; &amp; _GetLabel(&quot;ERROCCUR&quot;) _
165 &amp; Iif(piErrorLine &gt; 0, &quot; &quot; &amp; _GetLabel(&quot;ERRLINE&quot;) &amp; &quot; &quot; &amp; CStr(piErrorLine), &quot;&quot;) _
166 &amp; Iif(psErrorProc &lt;&gt; &quot;&quot;, &quot; &quot; &amp; _GetLabel(&quot;ERRIN&quot;) &amp; &quot; &quot; &amp; psErrorProc, Iif(_A2B_.CalledSub = &quot;&quot;, &quot;&quot;, &quot; &quot; &amp; _Getlabel(&quot;ERRIN&quot;) &amp; &quot; &quot; &amp; _A2B_.CalledSub))
167 If IsMissing(pvMsgBox) Then pvMsgBox = ( psErrorLevel = TRACEERRORS Or psErrorLevel = TRACEFATAL Or psErrorLevel = TRACEABORT )
168 TraceLog(psErrorLevel, sErrorText, pvMsgBox)
170 &apos; Unexpected error detected in user program or in Access2Base
171 If psErrorLevel = TRACEFATAL Or psErrorLevel = TRACEABORT Then
172 _A2B_.CalledSub = &quot;&quot;
173 If psErrorLevel = TRACEFATAL Then
174 Set oDb = Application.CurrentDb()
175 If Not IsNull(oDb) Then oDb.CloseAllrecordsets()
176 End If
177 Stop
178 End If
180 End Sub &apos; TraceError V0.9,5
182 REM -----------------------------------------------------------------------------------------------------------------------
183 Public Sub TraceLevel(ByVal Optional psTraceLevel As String)
184 &apos; Set trace level to argument
186 If _ErrorHandler() Then On Local Error Goto Error_Sub
187 Select Case True
188 Case IsMissing(psTraceLevel) : psTraceLevel = &quot;ERROR&quot;
189 Case psTraceLevel = &quot;&quot; : psTraceLevel = &quot;ERROR&quot;
190 Case Utils._InList(UCase(psTraceLevel), Array( _
191 TRACEDEBUG, TRACEINFO, TRACEWARNING, TRACEERRORS, TRACEFATAL, TRACEABORT _
193 Case Else : Goto Exit_Sub
194 End Select
195 _A2B_.MinimalTraceLevel = _TraceLevel(psTraceLevel)
197 Exit_Sub:
198 Exit Sub
199 Error_Sub:
200 With _A2B_
201 .TraceLogs() = Array()
202 .TraceLogCount = 0
203 .TraceLogLast = 0
204 End With
205 GoTo Exit_Sub
206 End Sub &apos; TraceLevel V0.9.5
208 REM -----------------------------------------------------------------------------------------------------------------------
209 Public Sub TraceLog(Byval psTraceLevel As String _
210 , ByVal psText As String _
211 , ByVal Optional pbMsgBox As Boolean _
213 &apos; Store Text in trace log (circular buffer)
215 If _ErrorHandler() Then On Local Error Goto Error_Sub
216 Dim vTraceLogs() As String, sTraceLevel As String
218 With _A2B_
219 If .MinimalTraceLevel = 0 Then .MinimalTraceLevel = _TraceLevel(TRACEERRORS)
220 If _TraceLevel(psTraceLevel) &lt; .MinimalTraceLevel Then Exit Sub
222 If UBound(.TraceLogs) = -1 Then &apos; Initialize TraceLog
223 If .TraceLogMaxEntries = 0 Then .TraceLogMaxEntries = cstLogMaxEntries
225 Redim vTraceLogs(0 To .TraceLogMaxEntries - 1)
226 .TraceLogs = vTraceLogs
227 .TraceLogCount = 0
228 .TraceLogLast = -1
229 If .MinimalTraceLevel = 0 Then .MinimalTraceLevel = _TraceLevel(TRACEERRORS) &apos; Set default value
230 End If
232 .TraceLogLast = .TraceLogLast + 1
233 If .TraceLogLast &gt; UBound(.TraceLogs) Then .TraceLogLast = LBound(.TraceLogs) &apos; Circular buffer
234 If Len(psTraceLevel) &gt; 7 Then sTraceLevel = Left(psTraceLevel, 7) Else sTraceLevel = psTraceLevel &amp; Spc(8 - Len(psTraceLevel))
235 .TraceLogs(.TraceLogLast) = Format(Now(), &quot;YYYY-MM-DD hh:mm:ss&quot;) &amp; &quot; &quot; &amp; sTraceLevel &amp; psText
236 If .TraceLogCount &lt;= UBound(.TraceLogs) Then .TraceLogCount = .TraceLogCount + 1 &apos; # of active entries
237 End With
239 If IsMissing(pbMsgBox) Then pbMsgBox = True
240 Dim iMsgBox As Integer
241 If pbMsgBox Then
242 Select Case psTraceLevel
243 Case TRACEINFO: iMsgBox = vbInformation
244 Case TRACEERRORS, TRACEWARNING: iMsgBox = vbExclamation
245 Case TRACEFATAL, TRACEABORT: iMsgBox = vbCritical
246 Case Else: iMsgBox = vbInformation
247 End Select
248 MsgBox psText, vbOKOnly + iMsgBox, psTraceLevel
249 End If
251 Exit_Sub:
252 Exit Sub
253 Error_Sub:
254 With _A2B_
255 .TraceLogs() = Array()
256 .TraceLogCount = 0
257 .TraceLogLast = 0
258 End With
259 GoTo Exit_Sub
260 End Sub &apos; TraceLog V0.9.5
263 REM -----------------------------------------------------------------------------------------------------------------------
264 REM --- PRIVATE FUNCTIONS ---
265 REM -----------------------------------------------------------------------------------------------------------------------
267 Private Sub _DumpToFile(oEvent As Object)
268 &apos; Execute the Dump To File command from the Trace dialog
269 &apos; Modified from Andrew Pitonyak&apos;s Base Macro Programming §10.4
272 If _ErrorHandler() Then On Local Error GoTo Error_Sub
274 Dim sPath as String, iFileNumber As Integer, i As Integer
276 sPath = _PromptFilePicker(&quot;txt&quot;)
277 If sPath &lt;&gt; &quot;&quot; Then &apos; Save button pressed
278 If UBound(_A2B_.TraceLogs) &gt;= 0 Then &apos; Array yet initialized
279 iFileNumber = FreeFile()
280 Open sPath For Append Access Write Lock Read As iFileNumber
281 If _A2B_.TraceLogCount &gt; 0 Then
282 If _A2B_.TraceLogCount &lt; _A2B_.TraceLogMaxEntries Then i = -1 Else i = _A2B_.TraceLogLast
284 If i &lt; _A2B_.TraceLogMaxEntries - 1 Then i = i + 1 Else i = 0
285 Print #iFileNumber _A2B_.TraceLogs(i)
286 Loop While i &lt;&gt; _A2B_.TraceLogLast
287 End If
288 Close iFileNumber
289 MsgBox _GetLabel(&quot;SAVECONSOLEENTRIES&quot;), vbOK + vbInformation, _GetLabel(&quot;SAVECONSOLE&quot;)
290 End If
291 End If
293 Exit_Sub:
294 Exit Sub
295 Error_Sub:
296 TraceError(&quot;ERROR&quot;, Err, &quot;DumpToFile&quot;, Erl)
297 GoTo Exit_Sub
298 End Sub &apos; DumpToFile V0.8.5
300 REM -----------------------------------------------------------------------------------------------------------------------
301 Public Function _ErrorHandler(Optional ByVal pbCheck As Boolean) As Boolean
302 &apos; Indicate if error handler is activated or not
303 &apos; When argument present set error handler
304 If IsEmpty(_A2B_) Then Call Application._RootInit() &apos; First use of Access2Base in current LibO/AOO session
305 If Not IsMissing(pbCheck) Then _A2B_.ErrorHandler = pbCheck
306 _ErrorHandler = _A2B_.ErrorHandler
307 Exit Function
308 End Function
310 REM -----------------------------------------------------------------------------------------------------------------------
311 Private Function _ErrorMessage(ByVal piErrorNumber As Integer, Optional ByVal pvArgs As Variant) As String
312 &apos; Return error message corresponding to ErrorNumber (standard or not)
313 &apos; and replaces %0, %1, ... , %9 by psArgs(0), psArgs(1), ...
315 Dim sErrorMessage As String, i As Integer, sErrLabel
316 _ErrorMessage = &quot;&quot;
317 If piErrorNumber &gt; ERRINIT Then
318 sErrLabel = &quot;ERR&quot; &amp; piErrorNumber
319 sErrorMessage = _Getlabel(sErrLabel)
320 If Not IsMissing(pvArgs) Then
321 If Not IsArray(pvArgs) Then
322 sErrorMessage = Join(Split(sErrorMessage, &quot;%0&quot;), Utils._CStr(pvArgs, False))
323 Else
324 For i = LBound(pvArgs) To UBound(pvArgs)
325 sErrorMessage = Join(Split(sErrorMessage, &quot;%&quot; &amp; i), Utils._CStr(pvArgs(i), False))
326 Next i
327 End If
328 End If
329 Else
330 sErrorMessage = Error(piErrorNumber)
331 &apos; Most (or all?) error messages terminate with a &quot;.&quot;
332 If Len(sErrorMessage) &gt; 1 And Right(sErrorMessage, 1) = &quot;.&quot; Then sErrorMessage = Left(sErrorMessage, Len(sErrorMessage)-1)
333 End If
335 _ErrorMessage = sErrorMessage
336 Exit Function
338 End Function &apos; ErrorMessage V0.8.9
340 REM -----------------------------------------------------------------------------------------------------------------------
341 Public Function _PromptFilePicker(ByVal psSuffix As String) As String
342 &apos; Prompt for output file name
343 &apos; Return &quot;&quot; if Cancel
344 &apos; Modified from Andrew Pitonyak&apos;s Base Macro Programming §10.4
346 If _ErrorHandler() Then On Local Error GoTo Error_Function
348 Dim oFileDialog as Object, oUcb as object, oPath As Object
349 Dim iAccept as Integer, sInitPath as String
351 Set oFileDialog = CreateUnoService(&quot;com.sun.star.ui.dialogs.FilePicker&quot;)
352 oFileDialog.Initialize(Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION))
353 Set oUcb = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
355 oFileDialog.appendFilter(&quot;*.&quot; &amp; psSuffix, &quot;*.&quot; &amp; psSuffix)
356 oFileDialog.appendFilter(&quot;*.*&quot;, &quot;*.*&quot;)
357 oFileDialog.setCurrentFilter(&quot;*.&quot; &amp; psSuffix)
358 Set oPath = createUnoService(&quot;com.sun.star.util.PathSettings&quot;)
359 sInitPath = oPath.Work &apos; Probably My Documents
360 If oUcb.Exists(sInitPath) Then oFileDialog.SetDisplayDirectory(sInitPath)
362 iAccept = oFileDialog.Execute()
364 _PromptFilePicker = &quot;&quot;
365 If iAccept = 1 Then &apos; Save button pressed
366 _PromptFilePicker = oFileDialog.Files(0)
367 End If
369 Exit_Function:
370 If Not IsEmpty(oFileDialog) And Not IsNull(oFileDialog) Then oFileDialog.Dispose()
371 Exit Function
372 Error_Function:
373 TraceError(&quot;ERROR&quot;, Err, &quot;PromptFilePicker&quot;, Erl)
374 GoTo Exit_Function
375 End Function &apos; PromptFilePicker V0.8.5
377 REM -----------------------------------------------------------------------------------------------------------------------
378 Public Sub _TraceArguments(Optional psCall As String)
379 &apos; Process the ERRMISSINGARGUMENTS error
380 &apos; psCall is present if error detected before call to _SetCalledSub
382 If Not IsMissing(psCall) Then Utils._SetCalledSub(psCall)
383 TraceError(TRACEFATAL, ERRMISSINGARGUMENTS, Utils._CalledSub(), 0)
384 Exit Sub
386 End Sub &apos; TraceArguments
388 REM -----------------------------------------------------------------------------------------------------------------------
389 Private Function _TraceLevel(ByVal pvTraceLevel As Variant) As Variant
390 &apos; Convert string trace level to numeric value or the opposite
392 Dim vTraces As Variant, i As Integer
393 vTraces = Array(TRACEDEBUG, TRACEINFO, TRACEWARNING, TRACEERRORS, TRACEFATAL, TRACEABORT, TRACEANY)
395 Select Case VarType(pvTraceLevel)
396 Case vbString
397 _TraceLevel = 4 &apos; 4 = Default
398 For i = 0 To UBound(vTraces)
399 If UCase(pvTraceLevel) = UCase(vTraces(i)) Then
400 _TraceLevel = i + 1
401 Exit For
402 End If
403 Next i
404 Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbBigint, vbDecimal
405 If pvTraceLevel &lt; 1 Or pvTraceLevel &gt; UBound(vTraces) + 1 Then _TraceLevel = TRACEERRORS Else _TraceLevel = vTraces(pvTraceLevel - 1)
406 End Select
408 End Function &apos; TraceLevel
410 </script:module>