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 =======================================================================================================================
10 Public Const cstLogMaxEntries =
20
13 REM TraceLog(
"INFO
",
"The OK button was pressed
")
15 REM Typical Usage for error logging
17 REM On Local Error GoTo Error_Sub
22 REM TraceError(
"ERROR
", Err,
"MySub
", Erl)
26 REM To display the current logged traces and/or to set parameters
29 REM -----------------------------------------------------------------------------------------------------------------------
30 Public Sub TraceConsole()
31 ' 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, oTraceDialog As Object
35 sLineBreak = vbNewLine
37 Set oTraceDialog = CreateUnoDialog(Utils._GetDialogLib().dlgTrace)
38 oTraceDialog.Title = _GetLabel(
"DLGTRACE_TITLE
")
' HelpText ???
40 Dim oEntries As Object, oTraceLog As Object, oClear As Object, oMinLevel As Object, oNbEntries As Object, oDump As Object
41 Dim oControl As Object
42 Dim i As Integer, sText As String, iOKCancel As Integer
44 Set oNbEntries = oTraceDialog.Model.getByName(
"numNbEntries
")
45 oNbEntries.Value = _A2B_.TraceLogCount
46 oNbEntries.HelpText = _GetLabel(
"DLGTRACE_LBLNBENTRIES_HELP
")
48 Set oControl = oTraceDialog.Model.getByName(
"lblNbEntries
")
49 oControl.Label = _GetLabel(
"DLGTRACE_LBLNBENTRIES_LABEL
")
50 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLNBENTRIES_HELP
")
52 Set oEntries = oTraceDialog.Model.getByName(
"numEntries
")
53 If _A2B_.TraceLogMaxEntries =
0 Then _A2B_.TraceLogMaxEntries = cstLogMaxEntries
54 oEntries.Value = _A2B_.TraceLogMaxEntries
55 oEntries.HelpText = _GetLabel(
"DLGTRACE_LBLENTRIES_HELP
")
57 Set oControl = oTraceDialog.Model.getByName(
"lblEntries
")
58 oControl.Label = _GetLabel(
"DLGTRACE_LBLENTRIES_LABEL
")
59 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLENTRIES_HELP
")
61 Set oDump = oTraceDialog.Model.getByName(
"cmdDump
")
63 oDump.Label = _GetLabel(
"DLGTRACE_CMDDUMP_LABEL
")
64 oDump.HelpText = _GetLabel(
"DLGTRACE_CMDDUMP_HELP
")
66 Set oTraceLog = oTraceDialog.Model.getByName(
"txtTraceLog
")
67 oTraceLog.HelpText = _GetLabel(
"DLGTRACE_TXTTRACELOG_HELP
")
68 If UBound(_A2B_.TraceLogs)
>=
0 Then
' Array yet initialized
69 oTraceLog.HardLineBreaks = True
71 If _A2B_.TraceLogCount
> 0 Then
72 If _A2B_.TraceLogCount
< _A2B_.TraceLogMaxEntries Then i = -
1 Else i = _A2B_.TraceLogLast
74 If i
< _A2B_.TraceLogMaxEntries -
1 Then i = i +
1 Else i =
0
75 If Len(_A2B_.TraceLogs(i))
> 11 Then
76 sText = sText
& Right(_A2B_.TraceLogs(i), Len(_A2B_.TraceLogs(i)) -
11)
& sLineBreak
' Skip date in display
78 Loop While i
<> _A2B_.TraceLogLast
79 oDump.Enabled =
1 ' Enable DumpToFile only if there is something to dump
81 If Len(sText)
> 0 Then sText = Left(sText, Len(sText) - Len(sLineBreak))
' Skip last linefeed
82 oTraceLog.Text = sText
84 oTraceLog.Text = _GetLabel(
"DLGTRACE_TXTTRACELOG_TEXT
")
87 Set oClear = oTraceDialog.Model.getByName(
"chkClear
")
88 oClear.State =
0 ' Unchecked
89 oClear.HelpText = _GetLabel(
"DLGTRACE_LBLCLEAR_HELP
")
91 Set oControl = oTraceDialog.Model.getByName(
"lblClear
")
92 oControl.Label = _GetLabel(
"DLGTRACE_LBLCLEAR_LABEL
")
93 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLCLEAR_HELP
")
95 Set oMinLevel = oTraceDialog.Model.getByName(
"cboMinLevel
")
96 If _A2B_.MinimalTraceLevel =
0 Then _A2B_.MinimalTraceLevel = _TraceLevel(TRACEERRORS)
97 oMinLevel.Text = _TraceLevel(_A2B_.MinimalTraceLevel)
98 oMinLevel.HelpText = _GetLabel(
"DLGTRACE_LBLMINLEVEL_HELP
")
100 Set oControl = oTraceDialog.Model.getByName(
"lblMinLevel
")
101 oControl.Label = _GetLabel(
"DLGTRACE_LBLMINLEVEL_LABEL
")
102 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLMINLEVEL_HELP
")
104 Set oControl = oTraceDialog.Model.getByName(
"cmdOK
")
105 oControl.Label = _GetLabel(
"DLGTRACE_CMDOK_LABEL
")
106 oControl.HelpText = _GetLabel(
"DLGTRACE_CMDOK_HELP
")
108 Set oControl = oTraceDialog.Model.getByName(
"cmdCancel
")
109 oControl.Label = _GetLabel(
"DLGTRACE_CMDCANCEL_LABEL
")
110 oControl.HelpText = _GetLabel(
"DLGTRACE_CMDCANCEL_HELP
")
112 iOKCancel = oTraceDialog.Execute()
114 Select Case iOKCancel
116 If oClear.State =
1 Then
117 _A2B_.TraceLogs() = Array()
' Erase logged traces
118 _A2B_.TraceLogCount =
0
120 If oMinLevel.Text
<> "" Then _A2B_.MinimalTraceLevel = _TraceLevel(oMinLevel.Text)
121 If oEntries.Value
<> 0 And oEntries.Value
<> _A2B_.TraceLogMaxEntries Then
122 _A2B_.TraceLogs() = Array()
123 _A2B_.TraceLogMaxEntries = oEntries.Value
130 If Not IsNull(oTraceDialog) Then oTraceDialog.Dispose()
134 .TraceLogs() = Array()
139 End Sub
' TraceConsole V1.1
.0
141 REM -----------------------------------------------------------------------------------------------------------------------
142 Public Sub TraceError(ByVal psErrorLevel As String _
143 , ByVal piErrorCode As Integer _
144 , ByVal psErrorProc As String _
145 , ByVal piErrorLine As Integer _
146 , ByVal Optional pvMsgBox As Variant _
147 , ByVal Optional pvArgs As Variant _
149 ' store error codes in trace buffer
151 On Local Error Resume Next
152 If IsEmpty(_A2B_) Then Call Application._RootInit()
' First use of Access2Base in current LibO/AOO session
154 Dim sErrorText As String, sErrorDesc As String, oDb As Object
155 sErrorDesc = _ErrorMessage(piErrorCode, pvArgs)
156 sErrorText = _GetLabel(
"ERR#
")
& CStr(piErrorCode) _
157 & " (
" & sErrorDesc
& ")
" & _GetLabel(
"ERROCCUR
") _
158 & Iif(piErrorLine
> 0,
" " & _GetLabel(
"ERRLINE
")
& " " & CStr(piErrorLine),
"") _
159 & Iif(psErrorProc
<> "",
" " & _GetLabel(
"ERRIN
")
& " " & psErrorProc, Iif(_A2B_.CalledSub =
"",
"",
" " & _Getlabel(
"ERRIN
")
& " " & _A2B_.CalledSub))
160 If IsMissing(pvMsgBox) Then pvMsgBox = ( psErrorLevel = TRACEERRORS Or psErrorLevel = TRACEFATAL Or psErrorLevel = TRACEABORT )
161 TraceLog(psErrorLevel, sErrorText, pvMsgBox)
163 ' Unexpected error detected in user program or in Access2Base
164 If psErrorLevel = TRACEFATAL Or psErrorLevel = TRACEABORT Then
165 _A2B_.CalledSub =
""
166 If psErrorLevel = TRACEFATAL Then
167 Set oDb = Application.CurrentDb()
168 If Not IsNull(oDb) Then oDb.CloseAllrecordsets()
173 End Sub
' TraceError V0.9,
5
175 REM -----------------------------------------------------------------------------------------------------------------------
176 Public Sub TraceLevel(ByVal Optional psTraceLevel As String)
177 ' Set trace level to argument
179 If _ErrorHandler() Then On Local Error Goto Error_Sub
181 Case IsMissing(psTraceLevel) : psTraceLevel =
"ERROR
"
182 Case psTraceLevel =
"" : psTraceLevel =
"ERROR
"
183 Case Utils._InList(UCase(psTraceLevel), Array( _
184 TRACEDEBUG, TRACEINFO, TRACEWARNING, TRACEERRORS, TRACEFATAL, TRACEABORT _
186 Case Else : Goto Exit_Sub
188 _A2B_.MinimalTraceLevel = _TraceLevel(psTraceLevel)
194 .TraceLogs() = Array()
199 End Sub
' TraceLevel V0.9
.5
201 REM -----------------------------------------------------------------------------------------------------------------------
202 Public Sub TraceLog(Byval psTraceLevel As String _
203 , ByVal psText As String _
204 , ByVal Optional pbMsgBox As Boolean _
206 ' Store Text in trace log (circular buffer)
208 If _ErrorHandler() Then On Local Error Goto Error_Sub
209 Dim vTraceLogs() As String, sTraceLevel As String
212 If .MinimalTraceLevel =
0 Then .MinimalTraceLevel = _TraceLevel(TRACEERRORS)
213 If _TraceLevel(psTraceLevel)
< .MinimalTraceLevel Then Exit Sub
215 If UBound(.TraceLogs) = -
1 Then
' Initialize TraceLog
216 If .TraceLogMaxEntries =
0 Then .TraceLogMaxEntries = cstLogMaxEntries
218 Redim vTraceLogs(
0 To .TraceLogMaxEntries -
1)
219 .TraceLogs = vTraceLogs
222 If .MinimalTraceLevel =
0 Then .MinimalTraceLevel = _TraceLevel(TRACEERRORS)
' Set default value
225 .TraceLogLast = .TraceLogLast +
1
226 If .TraceLogLast
> UBound(.TraceLogs) Then .TraceLogLast = LBound(.TraceLogs)
' Circular buffer
227 If Len(psTraceLevel)
> 7 Then sTraceLevel = Left(psTraceLevel,
7) Else sTraceLevel = psTraceLevel
& Spc(
8 - Len(psTraceLevel))
228 .TraceLogs(.TraceLogLast) = Format(Now(),
"YYYY-MM-DD hh:mm:ss
")
& " " & sTraceLevel
& psText
229 If .TraceLogCount
<= UBound(.TraceLogs) Then .TraceLogCount = .TraceLogCount +
1 ' # of active entries
232 If IsMissing(pbMsgBox) Then pbMsgBox = True
233 Dim iMsgBox As Integer
235 Select Case psTraceLevel
236 Case TRACEINFO: iMsgBox = vbInformation
237 Case TRACEERRORS, TRACEWARNING: iMsgBox = vbExclamation
238 Case TRACEFATAL, TRACEABORT: iMsgBox = vbCritical
239 Case Else: iMsgBox = vbInformation
241 MsgBox psText, vbOKOnly + iMsgBox, psTraceLevel
248 .TraceLogs() = Array()
253 End Sub
' TraceLog V0.9
.5
256 REM -----------------------------------------------------------------------------------------------------------------------
257 REM --- PRIVATE FUNCTIONS ---
258 REM -----------------------------------------------------------------------------------------------------------------------
260 Private Sub _DumpToFile(oEvent As Object)
261 ' Execute the Dump To File command from the Trace dialog
262 ' Modified from Andrew Pitonyak
's Base Macro Programming §
10.4
265 If _ErrorHandler() Then On Local Error GoTo Error_Sub
267 Dim sPath as String, iFileNumber As Integer, i As Integer
269 sPath = _PromptFilePicker(
"txt
")
270 If sPath
<> "" Then
' Save button pressed
271 If UBound(_A2B_.TraceLogs)
>=
0 Then
' Array yet initialized
272 iFileNumber = FreeFile()
273 Open sPath For Append Access Write Lock Read As iFileNumber
274 If _A2B_.TraceLogCount
> 0 Then
275 If _A2B_.TraceLogCount
< _A2B_.TraceLogMaxEntries Then i = -
1 Else i = _A2B_.TraceLogLast
277 If i
< _A2B_.TraceLogMaxEntries -
1 Then i = i +
1 Else i =
0
278 Print #iFileNumber _A2B_.TraceLogs(i)
279 Loop While i
<> _A2B_.TraceLogLast
282 MsgBox _GetLabel(
"SAVECONSOLEENTRIES
"), vbOK + vbInformation, _GetLabel(
"SAVECONSOLE
")
289 TraceError(
"ERROR
", Err,
"DumpToFile
", Erl)
291 End Sub
' DumpToFile V0.8
.5
293 REM -----------------------------------------------------------------------------------------------------------------------
294 Public Function _ErrorHandler(Optional ByVal pbCheck As Boolean) As Boolean
295 ' Indicate if error handler is activated or not
296 ' When argument present set error handler
297 If IsEmpty(_A2B_) Then Call Application._RootInit()
' First use of Access2Base in current LibO/AOO session
298 If Not IsMissing(pbCheck) Then _A2B_.ErrorHandler = pbCheck
299 _ErrorHandler = _A2B_.ErrorHandler
303 REM -----------------------------------------------------------------------------------------------------------------------
304 Private Function _ErrorMessage(ByVal piErrorNumber As Integer, Optional ByVal pvArgs As Variant) As String
305 ' Return error message corresponding to ErrorNumber (standard or not)
306 ' and replaces %
0, %
1, ... , %
9 by psArgs(
0), psArgs(
1), ...
308 Dim sErrorMessage As String, i As Integer, sErrLabel
309 _ErrorMessage =
""
310 If piErrorNumber
> ERRINIT Then
311 sErrLabel =
"ERR
" & piErrorNumber
312 sErrorMessage = _Getlabel(sErrLabel)
313 If Not IsMissing(pvArgs) Then
314 If Not IsArray(pvArgs) Then
315 sErrorMessage = Join(Split(sErrorMessage,
"%
0"), Utils._CStr(pvArgs, False))
317 For i = LBound(pvArgs) To UBound(pvArgs)
318 sErrorMessage = Join(Split(sErrorMessage,
"%
" & i), Utils._CStr(pvArgs(i), False))
323 sErrorMessage = Error(piErrorNumber)
324 ' Most (or all?) error messages terminate with a
".
"
325 If Len(sErrorMessage)
> 1 And Right(sErrorMessage,
1) =
".
" Then sErrorMessage = Left(sErrorMessage, Len(sErrorMessage)-
1)
328 _ErrorMessage = sErrorMessage
331 End Function
' ErrorMessage V0.8
.9
333 REM -----------------------------------------------------------------------------------------------------------------------
334 Public Function _PromptFilePicker(ByVal psSuffix As String) As String
335 ' Prompt for output file name
336 ' Return
"" if Cancel
337 ' Modified from Andrew Pitonyak
's Base Macro Programming §
10.4
339 If _ErrorHandler() Then On Local Error GoTo Error_Function
341 Dim oFileDialog as Object, oUcb as object, oPath As Object
342 Dim iAccept as Integer, sInitPath as String
344 Set oFileDialog = CreateUnoService(
"com.sun.star.ui.dialogs.FilePicker
")
345 oFileDialog.Initialize(Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION))
346 Set oUcb = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
348 oFileDialog.appendFilter(
"*.
" & psSuffix,
"*.
" & psSuffix)
349 oFileDialog.appendFilter(
"*.*
",
"*.*
")
350 oFileDialog.setCurrentFilter(
"*.
" & psSuffix)
351 Set oPath = createUnoService(
"com.sun.star.util.PathSettings
")
352 sInitPath = oPath.Work
' Probably My Documents
353 If oUcb.Exists(sInitPath) Then oFileDialog.SetDisplayDirectory(sInitPath)
355 iAccept = oFileDialog.Execute()
357 _PromptFilePicker =
""
358 If iAccept =
1 Then
' Save button pressed
359 _PromptFilePicker = oFileDialog.Files(
0)
363 If Not IsEmpty(oFileDialog) And Not IsNull(oFileDialog) Then oFileDialog.Dispose()
366 TraceError(
"ERROR
", Err,
"PromptFilePicker
", Erl)
368 End Function
' PromptFilePicker V0.8
.5
370 REM -----------------------------------------------------------------------------------------------------------------------
371 Public Sub _TraceArguments(Optional psCall As String)
372 ' Process the ERRMISSINGARGUMENTS error
373 ' psCall is present if error detected before call to _SetCalledSub
375 If Not IsMissing(psCall) Then Utils._SetCalledSub(psCall)
376 TraceError(TRACEFATAL, ERRMISSINGARGUMENTS, Utils._CalledSub(),
0)
379 End Sub
' TraceArguments
381 REM -----------------------------------------------------------------------------------------------------------------------
382 Private Function _TraceLevel(ByVal pvTraceLevel As Variant) As Variant
383 ' Convert string trace level to numeric value or the opposite
385 Dim vTraces As Variant, i As Integer
386 vTraces = Array(TRACEDEBUG, TRACEINFO, TRACEWARNING, TRACEERRORS, TRACEFATAL, TRACEABORT, TRACEANY)
388 Select Case VarType(pvTraceLevel)
390 _TraceLevel =
4 ' 4 = Default
391 For i =
0 To UBound(vTraces)
392 If UCase(pvTraceLevel) = UCase(vTraces(i)) Then
397 Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbBigint, vbDecimal
398 If pvTraceLevel
< 1 Or pvTraceLevel
> UBound(vTraces) +
1 Then _TraceLevel = TRACEERRORS Else _TraceLevel = vTraces(pvTraceLevel -
1)
401 End Function
' TraceLevel