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 =======================================================================================================================
11 Public Const cstLogMaxEntries =
99
14 REM TraceLog(
"INFO
",
"The OK button was pressed
")
16 REM Typical Usage for error logging
18 REM On Local Error GoTo Error_Sub
23 REM TraceError(
"ERROR
", Err,
"MySub
", Erl)
27 REM To display the current logged traces and/or to set parameters
30 REM -----------------------------------------------------------------------------------------------------------------------
31 Public Sub TraceConsole()
32 ' 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(
"DLGTRACE_TITLE
")
40 oTraceDialog.Model.HelpText = _GetLabel(
"DLGTRACE_HELP
")
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(
"numNbEntries
")
47 oNbEntries.Value = _A2B_.TraceLogCount
48 oNbEntries.HelpText = _GetLabel(
"DLGTRACE_LBLNBENTRIES_HELP
")
50 Set oControl = oTraceDialog.Model.getByName(
"lblNbEntries
")
51 oControl.Label = _GetLabel(
"DLGTRACE_LBLNBENTRIES_LABEL
")
52 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLNBENTRIES_HELP
")
54 Set oEntries = oTraceDialog.Model.getByName(
"numEntries
")
55 If _A2B_.TraceLogMaxEntries =
0 Then _A2B_.TraceLogMaxEntries = cstLogMaxEntries
56 oEntries.Value = _A2B_.TraceLogMaxEntries
57 oEntries.HelpText = _GetLabel(
"DLGTRACE_LBLENTRIES_HELP
")
59 Set oControl = oTraceDialog.Model.getByName(
"lblEntries
")
60 oControl.Label = _GetLabel(
"DLGTRACE_LBLENTRIES_LABEL
")
61 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLENTRIES_HELP
")
63 Set oDump = oTraceDialog.Model.getByName(
"cmdDump
")
65 oDump.Label = _GetLabel(
"DLGTRACE_CMDDUMP_LABEL
")
66 oDump.HelpText = _GetLabel(
"DLGTRACE_CMDDUMP_HELP
")
68 Set oTraceLog = oTraceDialog.Model.getByName(
"txtTraceLog
")
69 oTraceLog.HelpText = _GetLabel(
"DLGTRACE_TXTTRACELOG_HELP
")
70 If UBound(_A2B_.TraceLogs)
>=
0 Then
' Array yet initialized
71 oTraceLog.HardLineBreaks = True
73 If _A2B_.TraceLogCount
> 0 Then
74 If _A2B_.TraceLogCount
< _A2B_.TraceLogMaxEntries Then i = -
1 Else i = _A2B_.TraceLogLast
76 If i
< _A2B_.TraceLogMaxEntries -
1 Then i = i +
1 Else i =
0
77 If Len(_A2B_.TraceLogs(i))
> 11 Then
78 sText = sText
& Right(_A2B_.TraceLogs(i), Len(_A2B_.TraceLogs(i)) -
11)
& sLineBreak
' Skip date in display
80 Loop While i
<> _A2B_.TraceLogLast
81 oDump.Enabled =
1 ' Enable DumpToFile only if there is something to dump
83 If Len(sText)
> 0 Then sText = Left(sText, Len(sText) - Len(sLineBreak))
' Skip last linefeed
84 oTraceLog.Text = sText
86 oTraceLog.Text = _GetLabel(
"DLGTRACE_TXTTRACELOG_TEXT
")
89 Set oClear = oTraceDialog.Model.getByName(
"chkClear
")
90 oClear.State =
0 ' Unchecked
91 oClear.HelpText = _GetLabel(
"DLGTRACE_LBLCLEAR_HELP
")
93 Set oControl = oTraceDialog.Model.getByName(
"lblClear
")
94 oControl.Label = _GetLabel(
"DLGTRACE_LBLCLEAR_LABEL
")
95 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLCLEAR_HELP
")
97 Set oMinLevel = oTraceDialog.Model.getByName(
"cboMinLevel
")
98 If _A2B_.MinimalTraceLevel =
0 Then _A2B_.MinimalTraceLevel = _TraceLevel(TRACEERRORS)
99 oMinLevel.Text = _TraceLevel(_A2B_.MinimalTraceLevel)
100 oMinLevel.HelpText = _GetLabel(
"DLGTRACE_LBLMINLEVEL_HELP
")
102 Set oControl = oTraceDialog.Model.getByName(
"lblMinLevel
")
103 oControl.Label = _GetLabel(
"DLGTRACE_LBLMINLEVEL_LABEL
")
104 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLMINLEVEL_HELP
")
106 Set oControl = oTraceDialog.Model.getByName(
"cmdOK
")
107 oControl.Label = _GetLabel(
"DLGTRACE_CMDOK_LABEL
")
108 oControl.HelpText = _GetLabel(
"DLGTRACE_CMDOK_HELP
")
110 Set oControl = oTraceDialog.Model.getByName(
"cmdCancel
")
111 oControl.Label = _GetLabel(
"DLGTRACE_CMDCANCEL_LABEL
")
112 oControl.HelpText = _GetLabel(
"DLGTRACE_CMDCANCEL_HELP
")
114 iOKCancel = oTraceDialog.Execute()
116 Select Case iOKCancel
118 If oClear.State =
1 Then
119 _A2B_.TraceLogs() = Array()
' Erase logged traces
120 _A2B_.TraceLogCount =
0
122 If oMinLevel.Text
<> "" Then _A2B_.MinimalTraceLevel = _TraceLevel(oMinLevel.Text)
123 If oEntries.Value
<> 0 And oEntries.Value
<> _A2B_.TraceLogMaxEntries Then
124 _A2B_.TraceLogs() = Array()
125 _A2B_.TraceLogMaxEntries = oEntries.Value
132 If Not IsNull(oTraceDialog) Then oTraceDialog.Dispose()
136 .TraceLogs() = Array()
141 End Sub
' 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 ' Store error code and description in trace rolling buffer
152 ' Display error message if errorlevel
>= ERROR
153 ' Stop program execution if errorlevel = FATAL or ABORT
155 On Local Error Resume Next
156 If IsEmpty(_A2B_) Then Call Application._RootInit()
' 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(
"ERR#
")
& CStr(piErrorCode) _
161 & " (
" & sErrorDesc
& ")
" & _GetLabel(
"ERROCCUR
") _
162 & Iif(piErrorLine
> 0,
" " & _GetLabel(
"ERRLINE
")
& " " & CStr(piErrorLine),
"") _
163 & Iif(psErrorProc
<> "",
" " & _GetLabel(
"ERRIN
")
& " " & psErrorProc, Iif(_A2B_.CalledSub =
"",
"",
" " & _Getlabel(
"ERRIN
")
& " " & _A2B_.CalledSub))
165 .LastErrorCode = piErrorCode
166 .LastErrorLevel = psErrorLevel
167 .ErrorText = sErrorDesc
168 .ErrorLongText = sErrorText
169 .CalledSub =
""
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 )
178 TraceLog(psErrorLevel, sErrorText, bMsgBox)
180 ' 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()
189 End Sub
' TraceError V0.9
.5
191 REM -----------------------------------------------------------------------------------------------------------------------
192 Public Function TraceErrorCode() As Variant
193 ' Return the last encountered error code, level, description in an array
196 Dim vError As Variant
206 TraceErrorCode = vError
208 End Function
' TraceErrorCode V6.3
210 REM -----------------------------------------------------------------------------------------------------------------------
211 Public Sub TraceLevel(ByVal Optional psTraceLevel As String)
212 ' Set trace level to argument
214 If _ErrorHandler() Then On Local Error Goto Error_Sub
216 Case IsMissing(psTraceLevel) : psTraceLevel =
"ERROR
"
217 Case psTraceLevel =
"" : psTraceLevel =
"ERROR
"
218 Case Utils._InList(UCase(psTraceLevel), Array( _
219 TRACEDEBUG, TRACEINFO, TRACEWARNING, TRACEERRORS, TRACEFATAL, TRACEABORT _
221 Case Else : Goto Exit_Sub
223 _A2B_.MinimalTraceLevel = _TraceLevel(psTraceLevel)
229 .TraceLogs() = Array()
234 End Sub
' 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 ' 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
247 If .MinimalTraceLevel =
0 Then .MinimalTraceLevel = _TraceLevel(TRACEERRORS)
248 If _TraceLevel(psTraceLevel)
< .MinimalTraceLevel Then Exit Sub
250 If UBound(.TraceLogs) = -
1 Then
' Initialize TraceLog
251 If .TraceLogMaxEntries =
0 Then .TraceLogMaxEntries = cstLogMaxEntries
253 Redim vTraceLogs(
0 To .TraceLogMaxEntries -
1)
254 .TraceLogs = vTraceLogs
257 If .MinimalTraceLevel =
0 Then .MinimalTraceLevel = _TraceLevel(TRACEERRORS)
' Set default value
260 .TraceLogLast = .TraceLogLast +
1
261 If .TraceLogLast
> UBound(.TraceLogs) Then .TraceLogLast = LBound(.TraceLogs)
' Circular buffer
262 If Len(psTraceLevel)
> 7 Then sTraceLevel = Left(psTraceLevel,
7) Else sTraceLevel = psTraceLevel
& Spc(
8 - Len(psTraceLevel))
263 .TraceLogs(.TraceLogLast) = Format(Now(),
"YYYY-MM-DD hh:mm:ss
")
& " " & sTraceLevel
& psText
264 If .TraceLogCount
<= UBound(.TraceLogs) Then .TraceLogCount = .TraceLogCount +
1 ' # of active entries
267 If IsMissing(pbMsgBox) Then pbMsgBox = True
268 Dim iMsgBox As Integer
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
276 MsgBox psText, vbOKOnly + iMsgBox, psTraceLevel
283 .TraceLogs() = Array()
288 End Sub
' TraceLog V0.9
.5
291 REM -----------------------------------------------------------------------------------------------------------------------
292 REM --- PRIVATE FUNCTIONS ---
293 REM -----------------------------------------------------------------------------------------------------------------------
295 Private Sub _DumpToFile(oEvent As Object)
296 ' Execute the Dump To File command from the Trace dialog
297 ' Modified from Andrew Pitonyak
'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(
"txt
")
305 If sPath
<> "" Then
' Save button pressed
306 If UBound(_A2B_.TraceLogs)
>=
0 Then
' Array yet initialized
307 iFileNumber = FreeFile()
308 Open sPath For Append Access Write Lock Read As iFileNumber
309 If _A2B_.TraceLogCount
> 0 Then
310 If _A2B_.TraceLogCount
< _A2B_.TraceLogMaxEntries Then i = -
1 Else i = _A2B_.TraceLogLast
312 If i
< _A2B_.TraceLogMaxEntries -
1 Then i = i +
1 Else i =
0
313 Print #iFileNumber _A2B_.TraceLogs(i)
314 Loop While i
<> _A2B_.TraceLogLast
317 MsgBox _GetLabel(
"SAVECONSOLEENTRIES
"), vbOK + vbInformation, _GetLabel(
"SAVECONSOLE
")
324 TraceError(
"ERROR
", Err,
"DumpToFile
", Erl)
326 End Sub
' DumpToFile V0.8
.5
328 REM -----------------------------------------------------------------------------------------------------------------------
329 Public Function _ErrorHandler(Optional ByVal pbCheck As Boolean) As Boolean
330 ' Indicate if error handler is activated or not
331 ' When argument present set error handler
332 If IsEmpty(_A2B_) Then Call Application._RootInit()
' First use of Access2Base in current LibO/AOO session
333 If Not IsMissing(pbCheck) Then _A2B_.ErrorHandler = pbCheck
334 _ErrorHandler = _A2B_.ErrorHandler
338 REM -----------------------------------------------------------------------------------------------------------------------
339 Private Function _ErrorMessage(ByVal piErrorNumber As Integer, Optional ByVal pvArgs As Variant) As String
340 ' Return error message corresponding to ErrorNumber (standard or not)
341 ' and replaces %
0, %
1, ... , %
9 by psArgs(
0), psArgs(
1), ...
343 Dim sErrorMessage As String, i As Integer, sErrLabel
344 _ErrorMessage =
""
345 If piErrorNumber
> ERRINIT Then
346 sErrLabel =
"ERR
" & piErrorNumber
347 sErrorMessage = _Getlabel(sErrLabel)
348 If Not IsMissing(pvArgs) Then
349 If Not IsArray(pvArgs) Then
350 sErrorMessage = Join(Split(sErrorMessage,
"%
0"), Utils._CStr(pvArgs, False))
352 For i = LBound(pvArgs) To UBound(pvArgs)
353 sErrorMessage = Join(Split(sErrorMessage,
"%
" & i), Utils._CStr(pvArgs(i), False))
358 sErrorMessage = Error(piErrorNumber)
359 ' Most (or all?) error messages terminate with a
".
"
360 If Len(sErrorMessage)
> 1 And Right(sErrorMessage,
1) =
".
" Then sErrorMessage = Left(sErrorMessage, Len(sErrorMessage)-
1)
363 _ErrorMessage = sErrorMessage
366 End Function
' ErrorMessage V0.8
.9
368 REM -----------------------------------------------------------------------------------------------------------------------
369 Public Function _PromptFilePicker(ByVal psSuffix As String) As String
370 ' Prompt for output file name
371 ' Return
"" if Cancel
372 ' Modified from Andrew Pitonyak
'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(
"com.sun.star.ui.dialogs.FilePicker
")
380 oFileDialog.Initialize(Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION))
381 Set oUcb = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
383 oFileDialog.appendFilter(
"*.
" & psSuffix,
"*.
" & psSuffix)
384 oFileDialog.appendFilter(
"*.*
",
"*.*
")
385 oFileDialog.setCurrentFilter(
"*.
" & psSuffix)
386 Set oPath = createUnoService(
"com.sun.star.util.PathSettings
")
387 sInitPath = oPath.Work
' Probably My Documents
388 If oUcb.Exists(sInitPath) Then oFileDialog.SetDisplayDirectory(sInitPath)
390 iAccept = oFileDialog.Execute()
392 _PromptFilePicker =
""
393 If iAccept =
1 Then
' Save button pressed
394 _PromptFilePicker = oFileDialog.Files(
0)
398 If Not IsEmpty(oFileDialog) And Not IsNull(oFileDialog) Then oFileDialog.Dispose()
401 TraceError(
"ERROR
", Err,
"PromptFilePicker
", Erl)
403 End Function
' PromptFilePicker V0.8
.5
405 REM -----------------------------------------------------------------------------------------------------------------------
406 Public Sub _TraceArguments(Optional psCall As String)
407 ' Process the ERRMISSINGARGUMENTS error
408 ' 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)
414 End Sub
' TraceArguments
416 REM -----------------------------------------------------------------------------------------------------------------------
417 Private Function _TraceLevel(ByVal pvTraceLevel As Variant) As Variant
418 ' 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)
425 _TraceLevel =
4 ' 4 = Default
426 For i =
0 To UBound(vTraces)
427 If UCase(pvTraceLevel) = UCase(vTraces(i)) Then
432 Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbBigint, vbDecimal
433 If pvTraceLevel
< 1 Or pvTraceLevel
> UBound(vTraces) +
1 Then _TraceLevel = TRACEERRORS Else _TraceLevel = vTraces(pvTraceLevel -
1)
436 End Function
' TraceLevel