Okay, here it is.
Bear in mind that this code is my personal solution to the lack of easy-to-use debugging tools from Lotus.
Code:
'log_code:
Option Public
Dim loglines As Long
Sub log_init(s As NotesSession,db As notesdatabase,logdb As notesdatabase,logflag As Integer,logdoc As NotesDocument,doclog As NotesRichTextItem,procname As String,logdomain As String)
On Error Goto errcode
If logflag Then
Call addlogheader(doclog,procname)
Else
Set logdb = s.GetDatabase(db.server,[i]logdbpath[/i],False)
If logdb.IsOpen Then logflag = True Else logflag = False
If logflag Then
Set logdoc = logdb.createdocument
logdoc.form ="Log"
logdoc.status="started"
logdoc.domain=logdomain
logdoc.process=procname
logdoc.user=s.username
Set doclog = New notesrichtextitem(logdoc,"body")
End If
End If
Goto endcode
errcode:
Print "Log creation impossible - " & Error$
Resume endcode
endcode:
End Sub
Sub addlogline(doclog As NotesRichTextItem,msgdata As String)
Static i As Integer
msgdata = Time$ & " " & msgdata
Call doclog.AppendText(msgdata)
i=i+1
loglines = loglines + 1
If i>69 Then
i = 0
Call doclog.AddNewline(1,True)
Else
Call doclog.addnewline(1,False)
End If
End Sub
Sub addloglinevalue(doclog As NotesRichTextItem,msgdata As String,itemvalue As Variant)
Dim stritem As String
If Datatype(itemvalue) = 8 Then
stritem = itemvalue
Else
stritem = Str(itemvalue)
End If
msgdata = msgdata & stritem
Call addlogline(doclog,msgdata)
End Sub
Sub wraplog(logdoc As notesdocument,doclog As NotesRichTextItem)
logdoc.status="Finished"
Call addlogline(doclog,"End of process")
Call logdoc.ComputeWithForm(False,False)
Call logdoc.save(True,False,True)
End Sub
Sub addlogheader(doclog As NotesRichTextItem,procname As String)
Call addlogline(doclog,"******************************************************************")
Call addlogline(doclog,procname)
Call addlogline(doclog,"******************************************************************")
End Sub
Function checkloglinecount As Long
checkloglinecount = loglines
End Function
So, with this very small library, what do you get ?
First, I must explain how to use this library.
The principle of use is as follows : you have a piece of code that you wish to trace. Use the Include statement to include this library as reference, then declare some variables in the Declarations portion of your library.
These are the variables you need to declare :
'log code requirements
Dim logdb As notesdatabase
Dim logdoc As NotesDocument
Dim logmsg As String
Dim logflag As Integer
Dim doclog As notesrichtextitem
With these global variables, you are ready to start tracing your code.
At the start of your code, you need to "open" the log. You do this by writing this line :
Code:
Call log_init(session,db,logdb,logflag,logdoc,doclog,"Code title",LogDomain)
This opening declaration allows you to recover three things :
- a boolean (LogFlag) that tells you that the trace is enabled,
- a notesdocument object (LogDoc) that will hold the trace information, and
- a notesrichtextitem (DocLog), in which you write the trace
Now you need to start including trace data.
It is up to you to decide how far you want to go, but whenever an important branch occurs, I include a line like this :
Code:
If logflag Then Call addlogline(doclog,"trace message here")
Also, when I want to trace a variable's value at some point, I use this :
Code:
If logflag then Call addloglinevalue(doclog,"Variable name ",variablevalue)
Once you have appropriately inserted your trace code, you must use
wraplog to finish your trace.
At this point, a line like this is needed :
Code:
Call wraplog(logdoc,doclog)
If, for some reason, you want a line count, you can use
checkloglinecount, a function that returns a long value with the number of lines written in the log. Do it before wrapping up, though.
You will notice that
log_init subroutine looks for a log database. It is up to you to set one up before doing a trace, or rewrite that portion to just open a log document in the current database.
Also, there is a useless little function to include a header in the trace, in case of need.
There, that should just about wrap up the explanation. It is not an easy solution, but implementing this trace function has saved me months of headaches and questions.
I hope it will help you.
Pascal.