-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.lss
427 lines (354 loc) · 13.9 KB
/
Logging.lss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
Option Public
Option Declare
Dim logsess As NotesSession
Dim logthisdb As NotesDatabase
Dim logdb As NotesDatabase
Dim loglookupdb As NotesDatabase
Dim loglookupview As NotesView
Dim logview As NotesView
Dim logdoc As NotesDocument
Dim logprofiledoc As NotesDocument
Dim loglookupdoc As NotesDocument
Dim logdeldoc As NotesDocument
Dim logcheckdoc As NotesDocument
Dim logrtitem As NotesRichTextItem
Dim logitem As NotesItem
Dim logsn As NotesName
Dim nnlog As NotesName
Dim logrs As NotesRichTextStyle
Dim logsendto As Variant
Dim logdelete As Variant
Dim logResult As Variant
Dim debuglogging As String
Dim loglocation As String
Dim logviewname As String
Dim logformname As String
Dim logdeletestr As String
Dim logdeleteinterval As String
Dim logstokeep As Long
Dim logwriteinterval As Integer
Dim loggingenabled As Integer
Dim logi As Integer
Dim logdelflag As Integer
Dim logdelcnt As Integer
Sub Initialize
Call initlogglobals
End Sub
Sub Terminate
If (Not loggingenabled) Or (logrtitem Is Nothing) Then
Exit Sub
End If
Call logrtitem.AddNewLine(2)
Call logrtitem.AppendText(Now & " : " & logdoc.callingobject(0) & " End.")
Call logrtitem.AddNewLine(1)
logdoc.endtime = Now
logdoc.Save True,True
End Sub
Sub logpurgeold
' INTERNAL LOGS VIEW MUST BE SORTED BY DESC DATE TO WORK CORRECTLY
' EXTERNAL LOGS NEED A CATEGORIZED VIEW
' Observed what looks like a race condition here when many people call the same logged process at the same instant
' I think that the theory that fits the facts is that the docToRemove handle can be to an object that someone else's run
' of the code has just deleted so an On Error Resume Next might be required for this Sub DJS 21/07/2014
On Error Resume Next ' DJS 23/07/2014 see above remarks
Dim docToRemove As NotesDocument, coll As NotesViewEntryCollection
Set logview = logdb.GetView(logviewname)
If logview Is Nothing Then
logaction(|Old logs not purged as view "|+logviewname+|" was not found.|)
Exit Sub
End If
Call deletespecifiedlogs ' GET RID OF FREQUENT LOGS SPECIFICALLY MENTIONED IN THE PROFILE OF THE CALLING APP
If loglocation = "Internal" Then
Set coll = logview.AllEntries
Else
' get a document collection of all log docs for the current calling object.
' this allows us to keep the same number of logs for different calling object, rather than keeping only n logs for the whole db
Dim key(1) As Variant
key(0) = logthisdb.Title
key(1) = logdoc.callingobject(0)
Set coll = logview.GetAllEntriesByKey(key, True)
End If
If coll.Count > logstokeep Then Set docToRemove = coll.GetLastEntry.document
While coll.Count > logstokeep And (Not docToRemove Is Nothing)
docToRemove.Remove True
logview.Refresh
' need to get a fresh collection bc last doc removed
If loglocation = "Internal" Then
Set coll = logview.AllEntries
Else
Set coll = logview.GetAllEntriesByKey(key, True)
End If
Set docToRemove = coll.GetLastEntry.document
Wend
logaction(|Old logs purged.|)
End Sub
Sub logerror(errnum As Integer, errmsg As String, errline As Integer, currentmodule As String, callingmodule As String, additionalinfo As String)
If Not loggingenabled Then
Exit Sub
End If
If logrtitem Is Nothing Then
Call initlog("Ad-Hoc Log")
End If
' build an error string
Dim errstr As String
errstr = "Error in " + currentmodule
If currentmodule <> callingmodule Then
errstr = errstr + " called by " & callingmodule
End If
errstr = errstr + ". Error #" & CStr(errnum) &" on line " & CStr(errline) & ". Error message: " & errmsg & "."
If additionalinfo <> "" Then
errstr = errstr + " Additional info: " & additionalinfo
End If
' set text to be red to highlight the error
Set logrs = logsess.CreateRichTextStyle
logrs.NotesColor = COLOR_RED
Call logrtitem.AppendStyle(logrs)
Call logrtitem.AppendText(Now & " : " & errstr)
Call logrtitem.AddNewLine(1)
' reset text to black font
Set logrs = logsess.CreateRichTextStyle
logrs.NotesColor = COLOR_BLACK
Call logrtitem.AppendStyle(logrs)
logdoc.errorfound = "Yes"
logdoc.Save True,True
End Sub
Sub semaphore(callingobject As String)
If (logprofiledoc.logmonitorstatus(0) = "Disabled") Then
Exit Sub
End If
If(loglocation = "Internal") Then
Set logdb = logsess.Currentdatabase
Set logsn = New NotesName(logthisdb.Server)
Else
Set loglookupdoc = loglookupview.GetDocumentByKey(UCase(loglocation), True)
If loglookupdoc Is Nothing Then
Print "Error the Lookup.nsf A1 view does not contain the case-sensitive key " & UCase(loglocation)
Exit Sub
End If
Set logdb = New NotesDatabase(loglookupdoc.target_server(0), loglookupdoc.path(0))
Set logsn = New NotesName(logdb.Server)
End If
' need to use local scope variables here in case globals for the log are still in use/need to be used
Dim docsemaphore As NotesDocument, delauthors As NotesItem, body As NotesRichTextItem, logkey(1) As String
logkey(0) = logthisdb.title
logkey(1) = callingobject ' THIS SHOULD MATCH THE AGENT NAME OR IDENTIFIER
Set logview = logdb.getview("S1")
Set docsemaphore = logview.getdocumentbykey(logkey,True)
If(docsemaphore Is Nothing) Then
Set docsemaphore = New NotesDocument(logdb)
docsemaphore.starttime = Now
docsemaphore.form = logformname
docsemaphore.semaphore = "Yes"
docsemaphore.delauthors = "*"
Set delauthors = docsemaphore.GetFirstItem("delauthors")
delauthors.IsAuthors = True ' The log must be able to potentially be deleted by anyone.
docsemaphore.server = logsn.common
docsemaphore.database = logthisdb.Title
docsemaphore.callingobject = callingobject
Set body = New NotesRichTextItem( docsemaphore, "Body" )
Call body.AppendText(Now & " " & callingobject & " Semaphore written")
Call body.AddNewLine(1)
docsemaphore.endtime = Now
docsemaphore.Save True,True
Else
docsemaphore.starttime = Now
docsemaphore.endtime = Now
docsemaphore.save True,True
End If
End Sub
Sub alerterrors(agentname As String,logstring As String)
If Not loggingenabled Then
Exit Sub
End If
Dim maildoc As New NotesDocument(logthisdb)
Dim mailrtitem As New NotesRichTextItem( maildoc, "Body" )
maildoc.form = "Memo"
maildoc.subject = "Server : " & logsn.Common & ". Database : " & logthisdb.Title & ". Process : " & agentname
Call mailrtitem.AppendText(logstring)
Call mailrtitem.AddNewline(1)
Call mailrtitem.AppendText("Link to database log ")
Call mailrtitem.AppendDocLink(logdoc,"Click to see log")
maildoc.sendto = logsendto
Call maildoc.send(False)
End Sub
Function logdoclink As NotesDocument
' Call this if you want to include a doclink to the log document in your application's doc DJS 23/09/2014
' calling code example
' Call rtitem.Appenddoclink(logdoclink, "Link to Log ", "Click to see the log")
Set logdoclink = logdoc
End Function
Sub deletespecifiedlogs
Dim deletedate As New NotesDateTime(Now)
If(logdeleteinterval = "" Or Not IsNumeric(logdeleteinterval)) Then
Exit Sub
End If
deletedate.adjustday(-Cint(logdeleteinterval)) ' ADJUST TO THE NUMBER OF DAYS REQUIRED TO KEEP
If(IsEmpty(logdelete) Or IsNull(logdelete)) Then
Exit Sub
End If
If(logdelete(0) = "") Then
Print "Logdelete is null"
Exit Sub
End If
For logi= LBound(logdelete) To UBound(logdelete)
logdeletestr = logdeletestr & logdelete(logi) & ","
Next
Set logcheckdoc = logview.getfirstdocument
logdelcnt = 0
While Not logcheckdoc Is Nothing
logdelflag = 0
For logi= LBound(logdelete) To UBound(logdelete)
If(logdelete(logi) = logcheckdoc.callingobject(0)) Then
Dim docdate As New NotesDateTime(logcheckdoc.starttime(0))
If(docdate.TimeDifference(deletedate ) < 0) Then
Set logdeldoc = logcheckdoc
logdelflag = 1
End If
End If
Next
Set logcheckdoc = logview.getnextdocument(logcheckdoc)
If(logdelflag = 1) Then
logdeldoc.remove True
logdelcnt = logdelcnt + 1
End If
Wend
If(logdelcnt > 0) Then
logaction("As specified in the calling db profile, " & CStr(logdelcnt) & " old logs prior to " & deletedate.LocalTime & " were purged for " & logdeletestr)
End If
End Sub
Sub initlog(callingobject As String)
If Not loggingenabled Then
' re-initialize globals and retest. this was added as there are some cases in which the log profile doc is created in script AFTER this library is initialised,
' so need to reinit the global vars
initlogglobals
If Not loggingenabled Then Exit Sub
End If
If(loglocation = "Internal") Then
Set logdb = logsess.Currentdatabase
Set logdoc = New NotesDocument(logdb)
Set logsn = New NotesName(logthisdb.Server)
Else
Set loglookupdoc = loglookupview.GetDocumentByKey(UCase(loglocation), True)
If loglookupdoc Is Nothing Then
Print "Error, the Lookup.nsf A1 view does not contain the case-sensitive key " & UCase(loglocation)
Exit Sub
End If
Set logdb = New NotesDatabase(loglookupdoc.target_server(0), loglookupdoc.path(0))
Set logdoc = New NotesDocument(logdb)
Set logsn = New NotesName(logdb.Server)
End If
logdoc.form = logformname
logdoc.delauthors = "*"
Set logitem = logdoc.GetFirstItem("delauthors")
logitem.IsAuthors = True ' The log must be able to potentially be deleted by anyone.
Set logrtitem = New NotesRichTextItem( logdoc, "Body" )
logdoc.server = logsn.common
logdoc.user = nnlog.Abbreviated ' Want to know who ran the code DJS 12/06/2012
logdoc.database = logthisdb.Title
Dim dblinkitem As New NotesRichTextItem(logdoc,"dblink")
Call dblinkitem.AppendDocLink(logthisdb,"Link To database " & logthisdb.Title)
logdoc.callingobject = callingobject
logdoc.starttime = Now
Set logrs = logsess.CreateRichTextStyle
logrs.FontSize = 8
Call logrtitem.AppendStyle(logrs)
Call logrtitem.AppendText(Now & " : " & callingobject & " Starts")
Call logrtitem.AddNewLine(1)
logdoc.Save True,True
' IF LOGGING INTERNAL TEST THAT THE CALLING USER ID HAS DELETE PERMISSION TO THE CURRENT DATABASE DJS 24/1/2013
If(loglocation <> "Internal") Then ' EXTERNAL IS FINE
logpurgeold
Else
Dim logdeldoc As New NotesDocument(logthisdb)
logdeldoc.server = logthisdb.Server
logdeldoc.path = logthisdb.FilePath
logResult=Evaluate("@UserAccess(server:path;[DELETEDOCUMENTS])",logdeldoc) ' THERE APPEARS TO BE NO LOTUSSCRIPT EQUIVALENT
If IsEmpty(logResult) Then ' IF THIS IS CALLED FROM A SCHEDULED AGENT EVALUATE WILL RETURN EMPTY DJS 06/02/2013
logpurgeold ' SERVER WILL HAVE DELETE ACCESS
Else
If(logResult(0) = "1") Then ' USER HAS DELETE ACCESS TO CURRENT DB
logpurgeold
End If
End If
End If
Call logrtitem.AddNewLine(1)
End Sub
Sub logclose(logstring As String)
If Not loggingenabled Then
Exit Sub
End If
Call logrtitem.AddNewLine(2)
Call logrtitem.AppendText(Now & " : " & logstring)
Call logrtitem.AddNewLine(1)
logdoc.endtime = Now
logdoc.Save True,True
End Sub
Sub logaction(logstring As String)
If Not loggingenabled Then
Exit Sub
End If
If logrtitem Is Nothing Then
Call initlog("Ad-Hoc Log")
End If
Static callcount As Long
Call logrtitem.AppendText(Now & " : " & logstring)
Call logrtitem.AddNewLine(1)
callcount = callcount + 1
If( callcount Mod logwriteinterval = 0) Then ' Only save the back-end doc at the interval specified for performance reasons
logdoc.Save True,True
End If
End Sub
Sub logactionwithlink(logstring As String, doc As NotesDocument)
If Not loggingenabled Then
Exit Sub
End If
If logrtitem Is Nothing Then
Call initlog("Ad-Hoc Log")
End If
Static callcount As Long
Call logrtitem.AppendText(Now & " : " & logstring & " ") ' add space before the doclink
Call logrtitem.AppendDocLink(doc, "Doc link for this action")
Call logrtitem.AddNewLine(1)
callcount = callcount + 1
If( callcount Mod logwriteinterval = 0) Then ' Only save the back-end doc at the interval specified for performance reasons
logdoc.Save True,True
End If
End Sub
Private Sub initlogglobals
' first check if initialization is needed by checking log profile
Set logsess = New NotesSession
Set logthisdb = logsess.currentdatabase
Set logprofiledoc = logthisdb.getprofiledocument("logprofile")
Set nnlog = New NotesName(logsess.UserName) ' Want to know who ran the code DJS 12/06/2012
' check if the profile doc has actually been created and populated with data
' The original code simply exited reporting logging not enabled if no log profile, it is more useful I decided
' if in this case a default log profile is created so the code can continue and is self-initialising DJS 14/07/2014
If Not logprofiledoc.HasItem("loggingstatus") Then
' Original code changed DJS 14/07/2014 loggingenabled = False
' Original code changed DJS 14/07/2014 Exit Sub
logprofiledoc.form = "logprofile" 'DJS 14/07/2014
logprofiledoc.loggingstatus = "Enabled" 'DJS 14/07/2014
logprofiledoc.loglocation = "CENTRALLOG" 'DJS 14/07/2014
logprofiledoc.logviewname = "logsbydb" 'DJS 14/07/2014
logprofiledoc.logstokeep = 100 'DJS 14/07/2014
logprofiledoc.logwriteinterval = "10" 'DJS 14/07/2014
logprofiledoc.logformname = "internallog" 'DJS 14/07/2014
Call logprofiledoc.Save(True, False) 'DJS 14/07/2014
End If
Set loglookupdb = New NotesDatabase( logthisdb.server, "lookup.nsf" ) ' Added test for local lookup.nsf DJS V015 23/07/2015
If (logprofiledoc.loggingstatus(0) = "Enabled" And loglookupdb.title <> "") Then ' Added test for local lookup.nsf DJS V015 23/07/2015
loggingenabled = True
Else
loggingenabled = False
Exit Sub
End If
loglocation = logprofiledoc.loglocation(0)
logviewname = logprofiledoc.logviewname(0)
logformname = logprofiledoc.logformname(0)
logwriteinterval = CInt(logprofiledoc.logwriteinterval(0))
logstokeep = logprofiledoc.logstokeep(0)
logsendto = logprofiledoc.sendto
logdeleteinterval = logprofiledoc.logdeleteinterval(0)
logdelete = logprofiledoc.logdelete
Set loglookupview = loglookupdb.GetView("A1")
End Sub