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, oDialogLib As Object, oTraceDialog As Object
37 Set oDialogLib = DialogLibraries
38 If oDialogLib.hasByName(
"Access2BaseDev
") Then
39 If Not oDialogLib.IsLibraryLoaded(
"Access2BaseDev
") Then oDialogLib.loadLibrary(
"Access2BaseDev
")
40 Set oTraceDialog = CreateUnoDialog(DialogLibraries.Access2BaseDev.dlgTrace)
42 If Not oDialogLib.IsLibraryLoaded(
"Access2Base
") Then oDialogLib.loadLibrary(
"Access2Base
")
43 Set oTraceDialog = CreateUnoDialog(DialogLibraries.Access2Base.dlgTrace)
45 oTraceDialog.Title = _GetLabel(
"DLGTRACE_TITLE
")
' 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(
"numNbEntries
")
52 oNbEntries.Value = _A2B_.TraceLogCount
53 oNbEntries.HelpText = _GetLabel(
"DLGTRACE_LBLNBENTRIES_HELP
")
55 Set oControl = oTraceDialog.Model.getByName(
"lblNbEntries
")
56 oControl.Label = _GetLabel(
"DLGTRACE_LBLNBENTRIES_LABEL
")
57 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLNBENTRIES_HELP
")
59 Set oEntries = oTraceDialog.Model.getByName(
"numEntries
")
60 If _A2B_.TraceLogMaxEntries =
0 Then _A2B_.TraceLogMaxEntries = cstLogMaxEntries
61 oEntries.Value = _A2B_.TraceLogMaxEntries
62 oEntries.HelpText = _GetLabel(
"DLGTRACE_LBLENTRIES_HELP
")
64 Set oControl = oTraceDialog.Model.getByName(
"lblEntries
")
65 oControl.Label = _GetLabel(
"DLGTRACE_LBLENTRIES_LABEL
")
66 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLENTRIES_HELP
")
68 Set oDump = oTraceDialog.Model.getByName(
"cmdDump
")
70 oDump.Label = _GetLabel(
"DLGTRACE_CMDDUMP_LABEL
")
71 oDump.HelpText = _GetLabel(
"DLGTRACE_CMDDUMP_HELP
")
73 Set oTraceLog = oTraceDialog.Model.getByName(
"txtTraceLog
")
74 oTraceLog.HelpText = _GetLabel(
"DLGTRACE_TXTTRACELOG_HELP
")
75 If UBound(_A2B_.TraceLogs)
>=
0 Then
' Array yet initialized
76 oTraceLog.HardLineBreaks = True
78 If _A2B_.TraceLogCount
> 0 Then
79 If _A2B_.TraceLogCount
< _A2B_.TraceLogMaxEntries Then i = -
1 Else i = _A2B_.TraceLogLast
81 If i
< _A2B_.TraceLogMaxEntries -
1 Then i = i +
1 Else i =
0
82 If Len(_A2B_.TraceLogs(i))
> 11 Then
83 sText = sText
& Right(_A2B_.TraceLogs(i), Len(_A2B_.TraceLogs(i)) -
11)
& sLineBreak
' Skip date in display
85 Loop While i
<> _A2B_.TraceLogLast
86 oDump.Enabled =
1 ' Enable DumpToFile only if there is something to dump
88 If Len(sText)
> 0 Then sText = Left(sText, Len(sText) -
1)
' Skip last linefeed
89 oTraceLog.Text = sText
91 oTraceLog.Text = _GetLabel(
"DLGTRACE_TXTTRACELOG_TEXT
")
94 Set oClear = oTraceDialog.Model.getByName(
"chkClear
")
95 oClear.State =
0 ' Unchecked
96 oClear.HelpText = _GetLabel(
"DLGTRACE_LBLCLEAR_HELP
")
98 Set oControl = oTraceDialog.Model.getByName(
"lblClear
")
99 oControl.Label = _GetLabel(
"DLGTRACE_LBLCLEAR_LABEL
")
100 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLCLEAR_HELP
")
102 Set oMinLevel = oTraceDialog.Model.getByName(
"cboMinLevel
")
103 If _A2B_.MinimalTraceLevel =
0 Then _A2B_.MinimalTraceLevel = _TraceLevel(TRACEERRORS)
104 oMinLevel.Text = _TraceLevel(_A2B_.MinimalTraceLevel)
105 oMinLevel.HelpText = _GetLabel(
"DLGTRACE_LBLMINLEVEL_HELP
")
107 Set oControl = oTraceDialog.Model.getByName(
"lblMinLevel
")
108 oControl.Label = _GetLabel(
"DLGTRACE_LBLMINLEVEL_LABEL
")
109 oControl.HelpText = _GetLabel(
"DLGTRACE_LBLMINLEVEL_HELP
")
111 Set oControl = oTraceDialog.Model.getByName(
"cmdOK
")
112 oControl.Label = _GetLabel(
"DLGTRACE_CMDOK_LABEL
")
113 oControl.HelpText = _GetLabel(
"DLGTRACE_CMDOK_HELP
")
115 Set oControl = oTraceDialog.Model.getByName(
"cmdCancel
")
116 oControl.Label = _GetLabel(
"DLGTRACE_CMDCANCEL_LABEL
")
117 oControl.HelpText = _GetLabel(
"DLGTRACE_CMDCANCEL_HELP
")
119 iOKCancel = oTraceDialog.Execute()
121 Select Case iOKCancel
123 If oClear.State =
1 Then
124 _A2B_.TraceLogs() = Array()
' Erase logged traces
125 _A2B_.TraceLogCount =
0
127 If oMinLevel.Text
<> "" Then _A2B_.MinimalTraceLevel = _TraceLevel(oMinLevel.Text)
128 If oEntries.Value
<> 0 And oEntries.Value
<> _A2B_.TraceLogMaxEntries Then
129 _A2B_.TraceLogs() = Array()
130 _A2B_.TraceLogMaxEntries = oEntries.Value
137 If Not IsNull(oTraceDialog) Then oTraceDialog.Dispose()
141 .TraceLogs() = Array()
146 End Sub
' 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 ' store error codes in trace buffer
158 On Local Error Resume Next
159 If IsEmpty(_A2B_) Then Call Application._RootInit()
' 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(
"ERR#
")
& CStr(piErrorCode) _
164 & " (
" & sErrorDesc
& ")
" & _GetLabel(
"ERROCCUR
") _
165 & Iif(piErrorLine
> 0,
" " & _GetLabel(
"ERRLINE
")
& " " & CStr(piErrorLine),
"") _
166 & Iif(psErrorProc
<> "",
" " & _GetLabel(
"ERRIN
")
& " " & psErrorProc, Iif(_A2B_.CalledSub =
"",
"",
" " & _Getlabel(
"ERRIN
")
& " " & _A2B_.CalledSub))
167 If IsMissing(pvMsgBox) Then pvMsgBox = ( psErrorLevel = TRACEERRORS Or psErrorLevel = TRACEFATAL Or psErrorLevel = TRACEABORT )
168 TraceLog(psErrorLevel, sErrorText, pvMsgBox)
170 ' Unexpected error detected in user program or in Access2Base
171 If psErrorLevel = TRACEFATAL Or psErrorLevel = TRACEABORT Then
172 _A2B_.CalledSub =
""
173 If psErrorLevel = TRACEFATAL Then
174 Set oDb = Application.CurrentDb()
175 If Not IsNull(oDb) Then oDb.CloseAllrecordsets()
180 End Sub
' TraceError V0.9,
5
182 REM -----------------------------------------------------------------------------------------------------------------------
183 Public Sub TraceLevel(ByVal Optional psTraceLevel As String)
184 ' Set trace level to argument
186 If _ErrorHandler() Then On Local Error Goto Error_Sub
188 Case IsMissing(psTraceLevel) : psTraceLevel =
"ERROR
"
189 Case psTraceLevel =
"" : psTraceLevel =
"ERROR
"
190 Case Utils._InList(UCase(psTraceLevel), Array( _
191 TRACEDEBUG, TRACEINFO, TRACEWARNING, TRACEERRORS, TRACEFATAL, TRACEABORT _
193 Case Else : Goto Exit_Sub
195 _A2B_.MinimalTraceLevel = _TraceLevel(psTraceLevel)
201 .TraceLogs() = Array()
206 End Sub
' 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 ' 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
219 If .MinimalTraceLevel =
0 Then .MinimalTraceLevel = _TraceLevel(TRACEERRORS)
220 If _TraceLevel(psTraceLevel)
< .MinimalTraceLevel Then Exit Sub
222 If UBound(.TraceLogs) = -
1 Then
' Initialize TraceLog
223 If .TraceLogMaxEntries =
0 Then .TraceLogMaxEntries = cstLogMaxEntries
225 Redim vTraceLogs(
0 To .TraceLogMaxEntries -
1)
226 .TraceLogs = vTraceLogs
229 If .MinimalTraceLevel =
0 Then .MinimalTraceLevel = _TraceLevel(TRACEERRORS)
' Set default value
232 .TraceLogLast = .TraceLogLast +
1
233 If .TraceLogLast
> UBound(.TraceLogs) Then .TraceLogLast = LBound(.TraceLogs)
' Circular buffer
234 If Len(psTraceLevel)
> 7 Then sTraceLevel = Left(psTraceLevel,
7) Else sTraceLevel = psTraceLevel
& Spc(
8 - Len(psTraceLevel))
235 .TraceLogs(.TraceLogLast) = Format(Now(),
"YYYY-MM-DD hh:mm:ss
")
& " " & sTraceLevel
& psText
236 If .TraceLogCount
<= UBound(.TraceLogs) Then .TraceLogCount = .TraceLogCount +
1 ' # of active entries
239 If IsMissing(pbMsgBox) Then pbMsgBox = True
240 Dim iMsgBox As Integer
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
248 MsgBox psText, vbOKOnly + iMsgBox, psTraceLevel
255 .TraceLogs() = Array()
260 End Sub
' TraceLog V0.9
.5
263 REM -----------------------------------------------------------------------------------------------------------------------
264 REM --- PRIVATE FUNCTIONS ---
265 REM -----------------------------------------------------------------------------------------------------------------------
267 Private Sub _DumpToFile(oEvent As Object)
268 ' Execute the Dump To File command from the Trace dialog
269 ' Modified from Andrew Pitonyak
'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(
"txt
")
277 If sPath
<> "" Then
' Save button pressed
278 If UBound(_A2B_.TraceLogs)
>=
0 Then
' Array yet initialized
279 iFileNumber = FreeFile()
280 Open sPath For Append Access Write Lock Read As iFileNumber
281 If _A2B_.TraceLogCount
> 0 Then
282 If _A2B_.TraceLogCount
< _A2B_.TraceLogMaxEntries Then i = -
1 Else i = _A2B_.TraceLogLast
284 If i
< _A2B_.TraceLogMaxEntries -
1 Then i = i +
1 Else i =
0
285 Print #iFileNumber _A2B_.TraceLogs(i)
286 Loop While i
<> _A2B_.TraceLogLast
289 MsgBox _GetLabel(
"SAVECONSOLEENTRIES
"), vbOK + vbInformation, _GetLabel(
"SAVECONSOLE
")
296 TraceError(
"ERROR
", Err,
"DumpToFile
", Erl)
298 End Sub
' DumpToFile V0.8
.5
300 REM -----------------------------------------------------------------------------------------------------------------------
301 Public Function _ErrorHandler(Optional ByVal pbCheck As Boolean) As Boolean
302 ' Indicate if error handler is activated or not
303 ' When argument present set error handler
304 If IsEmpty(_A2B_) Then Call Application._RootInit()
' First use of Access2Base in current LibO/AOO session
305 If Not IsMissing(pbCheck) Then _A2B_.ErrorHandler = pbCheck
306 _ErrorHandler = _A2B_.ErrorHandler
310 REM -----------------------------------------------------------------------------------------------------------------------
311 Private Function _ErrorMessage(ByVal piErrorNumber As Integer, Optional ByVal pvArgs As Variant) As String
312 ' Return error message corresponding to ErrorNumber (standard or not)
313 ' and replaces %
0, %
1, ... , %
9 by psArgs(
0), psArgs(
1), ...
315 Dim sErrorMessage As String, i As Integer, sErrLabel
316 _ErrorMessage =
""
317 If piErrorNumber
> ERRINIT Then
318 sErrLabel =
"ERR
" & piErrorNumber
319 sErrorMessage = _Getlabel(sErrLabel)
320 If Not IsMissing(pvArgs) Then
321 If Not IsArray(pvArgs) Then
322 sErrorMessage = Join(Split(sErrorMessage,
"%
0"), Utils._CStr(pvArgs, False))
324 For i = LBound(pvArgs) To UBound(pvArgs)
325 sErrorMessage = Join(Split(sErrorMessage,
"%
" & i), Utils._CStr(pvArgs(i), False))
330 sErrorMessage = Error(piErrorNumber)
331 ' Most (or all?) error messages terminate with a
".
"
332 If Len(sErrorMessage)
> 1 And Right(sErrorMessage,
1) =
".
" Then sErrorMessage = Left(sErrorMessage, Len(sErrorMessage)-
1)
335 _ErrorMessage = sErrorMessage
338 End Function
' ErrorMessage V0.8
.9
340 REM -----------------------------------------------------------------------------------------------------------------------
341 Public Function _PromptFilePicker(ByVal psSuffix As String) As String
342 ' Prompt for output file name
343 ' Return
"" if Cancel
344 ' Modified from Andrew Pitonyak
'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(
"com.sun.star.ui.dialogs.FilePicker
")
352 oFileDialog.Initialize(Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION))
353 Set oUcb = createUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
355 oFileDialog.appendFilter(
"*.
" & psSuffix,
"*.
" & psSuffix)
356 oFileDialog.appendFilter(
"*.*
",
"*.*
")
357 oFileDialog.setCurrentFilter(
"*.
" & psSuffix)
358 Set oPath = createUnoService(
"com.sun.star.util.PathSettings
")
359 sInitPath = oPath.Work
' Probably My Documents
360 If oUcb.Exists(sInitPath) Then oFileDialog.SetDisplayDirectory(sInitPath)
362 iAccept = oFileDialog.Execute()
364 _PromptFilePicker =
""
365 If iAccept =
1 Then
' Save button pressed
366 _PromptFilePicker = oFileDialog.Files(
0)
370 If Not IsEmpty(oFileDialog) And Not IsNull(oFileDialog) Then oFileDialog.Dispose()
373 TraceError(
"ERROR
", Err,
"PromptFilePicker
", Erl)
375 End Function
' PromptFilePicker V0.8
.5
377 REM -----------------------------------------------------------------------------------------------------------------------
378 Public Sub _TraceArguments(Optional psCall As String)
379 ' Process the ERRMISSINGARGUMENTS error
380 ' 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)
386 End Sub
' TraceArguments
388 REM -----------------------------------------------------------------------------------------------------------------------
389 Private Function _TraceLevel(ByVal pvTraceLevel As Variant) As Variant
390 ' 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)
397 _TraceLevel =
4 ' 4 = Default
398 For i =
0 To UBound(vTraces)
399 If UCase(pvTraceLevel) = UCase(vTraces(i)) Then
404 Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbBigint, vbDecimal
405 If pvTraceLevel
< 1 Or pvTraceLevel
> UBound(vTraces) +
1 Then _TraceLevel = TRACEERRORS Else _TraceLevel = vTraces(pvTraceLevel -
1)
408 End Function
' TraceLevel