tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / wizards / source / access2base / Trace.xba
blob041bea53268f2a3d48e23f6e2bcfef7b403a4a2e
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">
4 REM =======================================================================================================================
5 REM === The Access2Base library is a part of the LibreOffice project. ===
6 REM === Full documentation is available on http://www.access2base.com ===
7 REM =======================================================================================================================
9 Option Explicit
11 Public Const cstLogMaxEntries = 99
13 REM Typical Usage
14 REM TraceLog(&quot;INFO&quot;, &quot;The OK button was pressed&quot;)
15 REM
16 REM Typical Usage for error logging
17 REM Sub MySub()
18 REM On Local Error GoTo Error_Sub
19 REM ...
20 REM Exit_Sub:
21 REM Exit Sub
22 REM Error_Sub:
23 REM TraceError(&quot;ERROR&quot;, Err, &quot;MySub&quot;, Erl)
24 REM GoTo Exit_Sub
25 REM End Sub
26 REM
27 REM To display the current logged traces and/or to set parameters
28 REM TraceConsole()
30 REM -----------------------------------------------------------------------------------------------------------------------
31 Public Sub TraceConsole()
32 &apos; Display the Trace dialog with current trace log values and parameter choices
33 If _ErrorHandler() Then On Local Error Goto Error_Sub
35 Dim sLineBreak As String, oTraceDialog As Object
36 sLineBreak = vbNewLine
38 Set oTraceDialog = CreateUnoDialog(Utils._GetDialogLib().dlgTrace)
39 oTraceDialog.Title = _GetLabel(&quot;DLGTRACE_TITLE&quot;)
40 oTraceDialog.Model.HelpText = _GetLabel(&quot;DLGTRACE_HELP&quot;)
42 Dim oEntries As Object, oTraceLog As Object, oClear As Object, oMinLevel As Object, oNbEntries As Object, oDump As Object
43 Dim oControl As Object
44 Dim i As Integer, sText As String, iOKCancel As Integer
46 Set oNbEntries = oTraceDialog.Model.getByName(&quot;numNbEntries&quot;)
47 oNbEntries.Value = _A2B_.TraceLogCount
48 oNbEntries.HelpText = _GetLabel(&quot;DLGTRACE_LBLNBENTRIES_HELP&quot;)
50 Set oControl = oTraceDialog.Model.getByName(&quot;lblNbEntries&quot;)
51 oControl.Label = _GetLabel(&quot;DLGTRACE_LBLNBENTRIES_LABEL&quot;)
52 oControl.HelpText = _GetLabel(&quot;DLGTRACE_LBLNBENTRIES_HELP&quot;)
54 Set oEntries = oTraceDialog.Model.getByName(&quot;numEntries&quot;)
55 If _A2B_.TraceLogMaxEntries = 0 Then _A2B_.TraceLogMaxEntries = cstLogMaxEntries
56 oEntries.Value = _A2B_.TraceLogMaxEntries
57 oEntries.HelpText = _GetLabel(&quot;DLGTRACE_LBLENTRIES_HELP&quot;)
59 Set oControl = oTraceDialog.Model.getByName(&quot;lblEntries&quot;)
60 oControl.Label = _GetLabel(&quot;DLGTRACE_LBLENTRIES_LABEL&quot;)
61 oControl.HelpText = _GetLabel(&quot;DLGTRACE_LBLENTRIES_HELP&quot;)
63 Set oDump = oTraceDialog.Model.getByName(&quot;cmdDump&quot;)
64 oDump.Enabled = 0
65 oDump.Label = _GetLabel(&quot;DLGTRACE_CMDDUMP_LABEL&quot;)
66 oDump.HelpText = _GetLabel(&quot;DLGTRACE_CMDDUMP_HELP&quot;)
68 Set oTraceLog = oTraceDialog.Model.getByName(&quot;txtTraceLog&quot;)
69 oTraceLog.HelpText = _GetLabel(&quot;DLGTRACE_TXTTRACELOG_HELP&quot;)
70 If UBound(_A2B_.TraceLogs) &gt;= 0 Then &apos; Array yet initialized
71 oTraceLog.HardLineBreaks = True
72 sText = &quot;&quot;
73 If _A2B_.TraceLogCount &gt; 0 Then
74 If _A2B_.TraceLogCount &lt; _A2B_.TraceLogMaxEntries Then i = -1 Else i = _A2B_.TraceLogLast
76 If i &lt; _A2B_.TraceLogMaxEntries - 1 Then i = i + 1 Else i = 0
77 If Len(_A2B_.TraceLogs(i)) &gt; 11 Then
78 sText = sText &amp; Right(_A2B_.TraceLogs(i), Len(_A2B_.TraceLogs(i)) - 11) &amp; sLineBreak &apos; Skip date in display
79 End If
80 Loop While i &lt;&gt; _A2B_.TraceLogLast
81 oDump.Enabled = 1 &apos; Enable DumpToFile only if there is something to dump
82 End If
83 If Len(sText) &gt; 0 Then sText = Left(sText, Len(sText) - Len(sLineBreak)) &apos; Skip last linefeed
84 oTraceLog.Text = sText
85 Else
86 oTraceLog.Text = _GetLabel(&quot;DLGTRACE_TXTTRACELOG_TEXT&quot;)
87 End If
89 Set oClear = oTraceDialog.Model.getByName(&quot;chkClear&quot;)
90 oClear.State = 0 &apos; Unchecked
91 oClear.HelpText = _GetLabel(&quot;DLGTRACE_LBLCLEAR_HELP&quot;)
93 Set oControl = oTraceDialog.Model.getByName(&quot;lblClear&quot;)
94 oControl.Label = _GetLabel(&quot;DLGTRACE_LBLCLEAR_LABEL&quot;)
95 oControl.HelpText = _GetLabel(&quot;DLGTRACE_LBLCLEAR_HELP&quot;)
97 Set oMinLevel = oTraceDialog.Model.getByName(&quot;cboMinLevel&quot;)
98 If _A2B_.MinimalTraceLevel = 0 Then _A2B_.MinimalTraceLevel = _TraceLevel(TRACEERRORS)
99 oMinLevel.Text = _TraceLevel(_A2B_.MinimalTraceLevel)
100 oMinLevel.HelpText = _GetLabel(&quot;DLGTRACE_LBLMINLEVEL_HELP&quot;)
102 Set oControl = oTraceDialog.Model.getByName(&quot;lblMinLevel&quot;)
103 oControl.Label = _GetLabel(&quot;DLGTRACE_LBLMINLEVEL_LABEL&quot;)
104 oControl.HelpText = _GetLabel(&quot;DLGTRACE_LBLMINLEVEL_HELP&quot;)
106 Set oControl = oTraceDialog.Model.getByName(&quot;cmdOK&quot;)
107 oControl.Label = _GetLabel(&quot;DLGTRACE_CMDOK_LABEL&quot;)
108 oControl.HelpText = _GetLabel(&quot;DLGTRACE_CMDOK_HELP&quot;)
110 Set oControl = oTraceDialog.Model.getByName(&quot;cmdCancel&quot;)
111 oControl.Label = _GetLabel(&quot;DLGTRACE_CMDCANCEL_LABEL&quot;)
112 oControl.HelpText = _GetLabel(&quot;DLGTRACE_CMDCANCEL_HELP&quot;)
114 iOKCancel = oTraceDialog.Execute()
116 Select Case iOKCancel
117 Case 1 &apos; OK
118 If oClear.State = 1 Then
119 _A2B_.TraceLogs() = Array() &apos; Erase logged traces
120 _A2B_.TraceLogCount = 0
121 End If
122 If oMinLevel.Text &lt;&gt; &quot;&quot; Then _A2B_.MinimalTraceLevel = _TraceLevel(oMinLevel.Text)
123 If oEntries.Value &lt;&gt; 0 And oEntries.Value &lt;&gt; _A2B_.TraceLogMaxEntries Then
124 _A2B_.TraceLogs() = Array()
125 _A2B_.TraceLogMaxEntries = oEntries.Value
126 End If
127 Case 0 &apos; Cancel
128 Case Else
129 End Select
131 Exit_Sub:
132 If Not IsNull(oTraceDialog) Then oTraceDialog.Dispose()
133 Exit Sub
134 Error_Sub:
135 With _A2B_
136 .TraceLogs() = Array()
137 .TraceLogCount = 0
138 .TraceLogLast = 0
139 End With
140 GoTo Exit_Sub
141 End Sub &apos; TraceConsole V1.1.0
143 REM -----------------------------------------------------------------------------------------------------------------------
144 Public Sub TraceError(ByVal psErrorLevel As String _
145 , ByVal piErrorCode As Integer _
146 , ByVal psErrorProc As String _
147 , ByVal piErrorLine As Integer _
148 , ByVal Optional pvMsgBox As Variant _
149 , ByVal Optional pvArgs As Variant _
151 &apos; Store error code and description in trace rolling buffer
152 &apos; Display error message if errorlevel &gt;= ERROR
153 &apos; Stop program execution if errorlevel = FATAL or ABORT
155 On Local Error Resume Next
156 If IsEmpty(_A2B_) Then Call Application._RootInit() &apos; First use of Access2Base in current LibO/AOO session
158 Dim sErrorText As String, sErrorDesc As String, oDb As Object, bMsgBox As Boolean
159 sErrorDesc = _ErrorMessage(piErrorCode, pvArgs)
160 sErrorText = _GetLabel(&quot;ERR#&quot;) &amp; CStr(piErrorCode) _
161 &amp; &quot; (&quot; &amp; sErrorDesc &amp; &quot;) &quot; &amp; _GetLabel(&quot;ERROCCUR&quot;) _
162 &amp; Iif(piErrorLine &gt; 0, &quot; &quot; &amp; _GetLabel(&quot;ERRLINE&quot;) &amp; &quot; &quot; &amp; CStr(piErrorLine), &quot;&quot;) _
163 &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))
164 With _A2B_
165 .LastErrorCode = piErrorCode
166 .LastErrorLevel = psErrorLevel
167 .ErrorText = sErrorDesc
168 .ErrorLongText = sErrorText
169 .CalledSub = &quot;&quot;
170 End With
171 If VarType(pvMsgBox) = vbError Then
172 bMsgBox = ( psErrorLevel = TRACEERRORS Or psErrorLevel = TRACEFATAL Or psErrorLevel = TRACEABORT )
173 ElseIf IsMissing(pvMsgBox) Then
174 bMsgBox = ( psErrorLevel = TRACEERRORS Or psErrorLevel = TRACEFATAL Or psErrorLevel = TRACEABORT )
175 Else
176 bMsgBox = pvMsgBox
177 End If
178 TraceLog(psErrorLevel, sErrorText, bMsgBox)
180 &apos; Unexpected error detected in user program or in Access2Base
181 If psErrorLevel = TRACEFATAL Or psErrorLevel = TRACEABORT Then
182 If psErrorLevel = TRACEFATAL Then
183 Set oDb = _A2B_.CurrentDb()
184 If Not IsNull(oDb) Then oDb.CloseAllrecordsets()
185 End If
186 Stop
187 End If
189 End Sub &apos; TraceError V0.9.5
191 REM -----------------------------------------------------------------------------------------------------------------------
192 Public Function TraceErrorCode() As Variant
193 &apos; Return the last encountered error code, level, description in an array
194 &apos; UNPUBLISHED
196 Dim vError As Variant
198 With _A2B_
199 vError = Array( _
200 .LastErrorCode _
201 , .LastErrorLevel _
202 , .ErrorText _
203 , .ErrorLongText _
205 End With
206 TraceErrorCode = vError
208 End Function &apos; TraceErrorCode V6.3
210 REM -----------------------------------------------------------------------------------------------------------------------
211 Public Sub TraceLevel(ByVal Optional psTraceLevel As String)
212 &apos; Set trace level to argument
214 If _ErrorHandler() Then On Local Error Goto Error_Sub
215 Select Case True
216 Case IsMissing(psTraceLevel) : psTraceLevel = &quot;ERROR&quot;
217 Case psTraceLevel = &quot;&quot; : psTraceLevel = &quot;ERROR&quot;
218 Case Utils._InList(UCase(psTraceLevel), Array( _
219 TRACEDEBUG, TRACEINFO, TRACEWARNING, TRACEERRORS, TRACEFATAL, TRACEABORT _
221 Case Else : Goto Exit_Sub
222 End Select
223 _A2B_.MinimalTraceLevel = _TraceLevel(psTraceLevel)
225 Exit_Sub:
226 Exit Sub
227 Error_Sub:
228 With _A2B_
229 .TraceLogs() = Array()
230 .TraceLogCount = 0
231 .TraceLogLast = 0
232 End With
233 GoTo Exit_Sub
234 End Sub &apos; TraceLevel V0.9.5
236 REM -----------------------------------------------------------------------------------------------------------------------
237 Public Sub TraceLog(ByVal psTraceLevel As String _
238 , ByVal psText As String _
239 , ByVal Optional pbMsgBox As Boolean _
241 &apos; Store Text in trace log (circular buffer)
243 If _ErrorHandler() Then On Local Error Goto Error_Sub
244 Dim vTraceLogs() As String, sTraceLevel As String
246 With _A2B_
247 If .MinimalTraceLevel = 0 Then .MinimalTraceLevel = _TraceLevel(TRACEERRORS)
248 If _TraceLevel(psTraceLevel) &lt; .MinimalTraceLevel Then Exit Sub
250 If UBound(.TraceLogs) = -1 Then &apos; Initialize TraceLog
251 If .TraceLogMaxEntries = 0 Then .TraceLogMaxEntries = cstLogMaxEntries
253 Redim vTraceLogs(0 To .TraceLogMaxEntries - 1)
254 .TraceLogs = vTraceLogs
255 .TraceLogCount = 0
256 .TraceLogLast = -1
257 If .MinimalTraceLevel = 0 Then .MinimalTraceLevel = _TraceLevel(TRACEERRORS) &apos; Set default value
258 End If
260 .TraceLogLast = .TraceLogLast + 1
261 If .TraceLogLast &gt; UBound(.TraceLogs) Then .TraceLogLast = LBound(.TraceLogs) &apos; Circular buffer
262 If Len(psTraceLevel) &gt; 7 Then sTraceLevel = Left(psTraceLevel, 7) Else sTraceLevel = psTraceLevel &amp; Spc(8 - Len(psTraceLevel))
263 .TraceLogs(.TraceLogLast) = Format(Now(), &quot;YYYY-MM-DD hh:mm:ss&quot;) &amp; &quot; &quot; &amp; sTraceLevel &amp; psText
264 If .TraceLogCount &lt;= UBound(.TraceLogs) Then .TraceLogCount = .TraceLogCount + 1 &apos; # of active entries
265 End With
267 If IsMissing(pbMsgBox) Then pbMsgBox = True
268 Dim iMsgBox As Integer
269 If pbMsgBox Then
270 Select Case psTraceLevel
271 Case TRACEINFO: iMsgBox = vbInformation
272 Case TRACEERRORS, TRACEWARNING: iMsgBox = vbExclamation
273 Case TRACEFATAL, TRACEABORT: iMsgBox = vbCritical
274 Case Else: iMsgBox = vbInformation
275 End Select
276 MsgBox psText, vbOKOnly + iMsgBox, psTraceLevel
277 End If
279 Exit_Sub:
280 Exit Sub
281 Error_Sub:
282 With _A2B_
283 .TraceLogs() = Array()
284 .TraceLogCount = 0
285 .TraceLogLast = 0
286 End With
287 GoTo Exit_Sub
288 End Sub &apos; TraceLog V0.9.5
291 REM -----------------------------------------------------------------------------------------------------------------------
292 REM --- PRIVATE FUNCTIONS ---
293 REM -----------------------------------------------------------------------------------------------------------------------
295 Private Sub _DumpToFile(oEvent As Object)
296 &apos; Execute the Dump To File command from the Trace dialog
297 &apos; Modified from Andrew Pitonyak&apos;s Base Macro Programming §10.4
300 If _ErrorHandler() Then On Local Error GoTo Error_Sub
302 Dim sPath as String, iFileNumber As Integer, i As Integer
304 sPath = _PromptFilePicker(&quot;txt&quot;)
305 If sPath &lt;&gt; &quot;&quot; Then &apos; Save button pressed
306 If UBound(_A2B_.TraceLogs) &gt;= 0 Then &apos; Array yet initialized
307 iFileNumber = FreeFile()
308 Open sPath For Append Access Write Lock Read As iFileNumber
309 If _A2B_.TraceLogCount &gt; 0 Then
310 If _A2B_.TraceLogCount &lt; _A2B_.TraceLogMaxEntries Then i = -1 Else i = _A2B_.TraceLogLast
312 If i &lt; _A2B_.TraceLogMaxEntries - 1 Then i = i + 1 Else i = 0
313 Print #iFileNumber _A2B_.TraceLogs(i)
314 Loop While i &lt;&gt; _A2B_.TraceLogLast
315 End If
316 Close iFileNumber
317 MsgBox _GetLabel(&quot;SAVECONSOLEENTRIES&quot;), vbOK + vbInformation, _GetLabel(&quot;SAVECONSOLE&quot;)
318 End If
319 End If
321 Exit_Sub:
322 Exit Sub
323 Error_Sub:
324 TraceError(&quot;ERROR&quot;, Err, &quot;DumpToFile&quot;, Erl)
325 GoTo Exit_Sub
326 End Sub &apos; DumpToFile V0.8.5
328 REM -----------------------------------------------------------------------------------------------------------------------
329 Public Function _ErrorHandler(Optional ByVal pbCheck As Boolean) As Boolean
330 &apos; Indicate if error handler is activated or not
331 &apos; When argument present set error handler
332 If IsEmpty(_A2B_) Then Call Application._RootInit() &apos; First use of Access2Base in current LibO/AOO session
333 If Not IsMissing(pbCheck) Then _A2B_.ErrorHandler = pbCheck
334 _ErrorHandler = _A2B_.ErrorHandler
335 Exit Function
336 End Function
338 REM -----------------------------------------------------------------------------------------------------------------------
339 Private Function _ErrorMessage(ByVal piErrorNumber As Integer, Optional ByVal pvArgs As Variant) As String
340 &apos; Return error message corresponding to ErrorNumber (standard or not)
341 &apos; and replaces %0, %1, ... , %9 by psArgs(0), psArgs(1), ...
343 Dim sErrorMessage As String, i As Integer, sErrLabel
344 _ErrorMessage = &quot;&quot;
345 If piErrorNumber &gt; ERRINIT Then
346 sErrLabel = &quot;ERR&quot; &amp; piErrorNumber
347 sErrorMessage = _Getlabel(sErrLabel)
348 If Not IsMissing(pvArgs) Then
349 If Not IsArray(pvArgs) Then
350 sErrorMessage = Join(Split(sErrorMessage, &quot;%0&quot;), Utils._CStr(pvArgs, False))
351 Else
352 For i = LBound(pvArgs) To UBound(pvArgs)
353 sErrorMessage = Join(Split(sErrorMessage, &quot;%&quot; &amp; i), Utils._CStr(pvArgs(i), False))
354 Next i
355 End If
356 End If
357 Else
358 sErrorMessage = Error(piErrorNumber)
359 &apos; Most (or all?) error messages terminate with a &quot;.&quot;
360 If Len(sErrorMessage) &gt; 1 And Right(sErrorMessage, 1) = &quot;.&quot; Then sErrorMessage = Left(sErrorMessage, Len(sErrorMessage)-1)
361 End If
363 _ErrorMessage = sErrorMessage
364 Exit Function
366 End Function &apos; ErrorMessage V0.8.9
368 REM -----------------------------------------------------------------------------------------------------------------------
369 Public Function _PromptFilePicker(ByVal psSuffix As String) As String
370 &apos; Prompt for output file name
371 &apos; Return &quot;&quot; if Cancel
372 &apos; Modified from Andrew Pitonyak&apos;s Base Macro Programming §10.4
374 If _ErrorHandler() Then On Local Error GoTo Error_Function
376 Dim oFileDialog as Object, oUcb as object, oPath As Object
377 Dim iAccept as Integer, sInitPath as String
379 Set oFileDialog = CreateUnoService(&quot;com.sun.star.ui.dialogs.FilePicker&quot;)
380 oFileDialog.Initialize(Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION))
381 Set oUcb = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
383 oFileDialog.appendFilter(&quot;*.&quot; &amp; psSuffix, &quot;*.&quot; &amp; psSuffix)
384 oFileDialog.appendFilter(&quot;*.*&quot;, &quot;*.*&quot;)
385 oFileDialog.setCurrentFilter(&quot;*.&quot; &amp; psSuffix)
386 Set oPath = createUnoService(&quot;com.sun.star.util.PathSettings&quot;)
387 sInitPath = oPath.Work &apos; Probably My Documents
388 If oUcb.Exists(sInitPath) Then oFileDialog.SetDisplayDirectory(sInitPath)
390 iAccept = oFileDialog.Execute()
392 _PromptFilePicker = &quot;&quot;
393 If iAccept = 1 Then &apos; Save button pressed
394 _PromptFilePicker = oFileDialog.Files(0)
395 End If
397 Exit_Function:
398 If Not IsEmpty(oFileDialog) And Not IsNull(oFileDialog) Then oFileDialog.Dispose()
399 Exit Function
400 Error_Function:
401 TraceError(&quot;ERROR&quot;, Err, &quot;PromptFilePicker&quot;, Erl)
402 GoTo Exit_Function
403 End Function &apos; PromptFilePicker V0.8.5
405 REM -----------------------------------------------------------------------------------------------------------------------
406 Public Sub _TraceArguments(Optional psCall As String)
407 &apos; Process the ERRMISSINGARGUMENTS error
408 &apos; psCall is present if error detected before call to _SetCalledSub
410 If Not IsMissing(psCall) Then Utils._SetCalledSub(psCall)
411 TraceError(TRACEFATAL, ERRMISSINGARGUMENTS, Utils._CalledSub(), 0)
412 Exit Sub
414 End Sub &apos; TraceArguments
416 REM -----------------------------------------------------------------------------------------------------------------------
417 Private Function _TraceLevel(ByVal pvTraceLevel As Variant) As Variant
418 &apos; Convert string trace level to numeric value or the opposite
420 Dim vTraces As Variant, i As Integer
421 vTraces = Array(TRACEDEBUG, TRACEINFO, TRACEWARNING, TRACEERRORS, TRACEFATAL, TRACEABORT, TRACEANY)
423 Select Case VarType(pvTraceLevel)
424 Case vbString
425 _TraceLevel = 4 &apos; 4 = Default
426 For i = 0 To UBound(vTraces)
427 If UCase(pvTraceLevel) = UCase(vTraces(i)) Then
428 _TraceLevel = i + 1
429 Exit For
430 End If
431 Next i
432 Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbBigint, vbDecimal
433 If pvTraceLevel &lt; 1 Or pvTraceLevel &gt; UBound(vTraces) + 1 Then _TraceLevel = TRACEERRORS Else _TraceLevel = vTraces(pvTraceLevel - 1)
434 End Select
436 End Function &apos; TraceLevel
438 </script:module>