-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKTM Script Logging Framework.vb
1259 lines (1009 loc) · 46.9 KB
/
KTM Script Logging Framework.vb
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'#Reference {420B2830-E718-11CF-893D-00A0C9054228}#1.0#0#C:\Windows\SysWOW64\scrrun.dll#Microsoft Scripting Runtime#Scripting
'#Language "WWB-COM"
Option Explicit
'KTM Script Logging Framework
'2013-09-09 - Splitting batch logs by PID is now optional, default to off (set constant PROCESS_ID_IN_BATCHLOG_FILENAME)
'2013-08-08 - Design-time only issue in PB 6.0 fixed: When ParentFolder not known, doc/page not recorded.
'2013-05-16 - Split batch log by process ID. Log process ID on error.
'2013-01-16 - To simplify code, introduced dependency on Scripting.FileSystemObject.
' - Introduced dependency on Wscript.Shell to get correct path regardless of version or language of the OS.
' - Fix for negative error numbers not logged
'2012-12-16 - Delimiter was missing, causing Windows user and module to be combined
'CONFIGURABLE LOGGING CONSTANTS
'This message will be prepended to the msgbox shown to a user if there is a script error
Public Const USER_ERROR_MSG As String = "An error has occurred in the KTM project script. " & _
"If this message needs to be reported to a system administrator, please take a " & _
"screenshot with this message showing and explain what actions were taken immediately " & _
"before the error occurred."
Public Const LOG_FILENAME As String = "_KTM_Script_Log.log"
Public Const BATCH_LOG_FILENAME As String = "_KTM_Script_Batch.log"
Public Const DESIGN_LOG_FILENAME As String = "_KTM_Script_Design.log"
Public BATCH_LOG_FULLPATH As String
'for readability messages are logged on a newline after metadata, set this to true to log to a single line
Public Const LOG_SINGLE_LINE As Boolean = False
'NONCONFIGURABLE LOGGING CONSTANTS
Public Const LOCAL_LOG As Boolean = True
Public Const BATCH_LOG As Boolean = False
Public Const IGNORE_CURRENT_FUNCTION As Integer = 1
Public Const FORCE_ERROR As Boolean = True
Public Const ONLY_ON_ERROR As Boolean = False
Public Const SUPPRESS_MSGBOX As Boolean = True
'SMK 2013-05-16 Use to get process ID in InitializeBatch
'SMK 2013-09-09 Make per process log optional, default to off
Declare Function GetCurrentProcessId Lib "kernel32" Alias "GetCurrentProcessId" () As Long
Public PROCESS_ID As Long
Public Const PROCESS_ID_IN_BATCHLOG_FILENAME As Boolean = False
'GLOBAL LOGGING VARIABLES
'This will be changed to true if it looks like we are in a Thin Client module
Public THIN_CLIENT As Boolean
Public BATCH_IMAGE_LOGS As String
Public CAPTURE_LOCAL_LOGS As String
Public BATCH_CLASS As String
Public BATCH_NAME As String
Public BATCH_ID As Long
Public BATCH_ID_HEX As String
'to support KTM 5.5 features
Public BATCH_USERID As String
Public BATCH_USERNAME As String
Public BATCH_WINDOWSUSERNAME As String
Public BATCH_USERSTRING As String 'combination of the previous three
'======== START LOGGING CODE ========
'Initialize Capture/runtime info. Call from Application_InitializeBatch
Public Sub Logging_InitializeBatch(ByVal pXRootFolder As CscXFolder)
'SMK 2013-05-16 - Determine process ID
On Error Resume Next
PROCESS_ID=GetCurrentProcessId()
On Error GoTo CouldNotCreate
'assume the batch log folder does not exist
Dim LogFolderExists As Boolean
LogFolderExists = False
'these items are only set by Capture at runtime. if any are set,
' then we are at runtime and they are all set
If pXRootFolder.XValues.ItemExists("AC_BATCH_CLASS_NAME") Then
'Set batchname, batchid, batch class
BATCH_CLASS = pXRootFolder.XValues.ItemByName("AC_BATCH_CLASS_NAME").Value
BATCH_NAME = pXRootFolder.XValues.ItemByName("AC_BATCH_NAME").Value
BATCH_ID = CLng(pXRootFolder.XValues.ItemByName("AC_EXTERNAL_BATCHID").Value)
BATCH_ID_HEX = Hex(BATCH_ID)
'pad hex ID
BATCH_ID_HEX = Right("00000000", 8 - Len(BATCH_ID_HEX)) & BATCH_ID_HEX
'These items are only present in KTM 5.5+
If pXRootFolder.XValues.ItemExists("AC_BATCH_WINDOWSUSERNAME") Then
BATCH_WINDOWSUSERNAME=pXRootFolder.XValues.ItemByName("AC_BATCH_WINDOWSUSERNAME").Value
BATCH_USERID = pXRootFolder.XValues.ItemByName("AC_BATCH_USERID").Value
BATCH_USERNAME = pXRootFolder.XValues.ItemByName("AC_BATCH_USERNAME").Value
'if user profiles are off these will all be the same
If BATCH_WINDOWSUSERNAME = BATCH_USERID And BATCH_USERID = BATCH_USERNAME Then
'user profiles is off so only use the windows user
'SMK 2012-12-16 Delimiter was missing, causing Windows user and module to be combined
BATCH_USERSTRING = BATCH_WINDOWSUSERNAME & " -- "
Else
'user profiles is on, so use all
BATCH_USERSTRING = BATCH_WINDOWSUSERNAME & ", " & BATCH_USERNAME & _
" (" & BATCH_USERID & ") -- "
End If
End If
'set the batch logging path
BATCH_IMAGE_LOGS = pXRootFolder.XValues.ItemByName("AC_IMAGE_DIRECTORY").Value & _
"\" & BATCH_ID_HEX & "\Log\"
'SMK 2013-01-16 - To simplify code, introduced dependency on Scripting.FileSystemObject.
'To use an early bound object(FileSystemObject), add a reference to "Microsoft Scripting Runtime"
' C:\Windows\System32\scrrun.dll (C:\Windows\SysWOW64\scrrun.dll)
' Otherwise late bound object will be created via CreateObject("Scripting.FileSystemObject")
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
'Dim fso As FileSystemObject
On Error GoTo CouldNotCreate
If Not fso.FolderExists(BATCH_IMAGE_LOGS) Then
fso.CreateFolder(BATCH_IMAGE_LOGS)
End If
LogFolderExists=True
'if creating the folder causes an error then LogFolderExists is still false
CouldNotCreate:
Err.Clear()
On Error GoTo catch
'if the folder still doesn't exist after trying to create, just use image path
If Not LogFolderExists Then
'we prefer to log to the "Log" folder along with the interactive modules,
' but if there is a problem, use the image path itself
BATCH_IMAGE_LOGS = pXRootFolder.XValues.ItemByName("AC_IMAGE_DIRECTORY").Value & _
"\" & BATCH_ID_HEX & "\"
End If
End If
catch:
Set fso=Nothing
End Sub
'log initial information about the batch. Call from Batch_Open
Public Sub Logging_BatchOpen(ByVal pXRootFolder As CscXFolder)
On Error GoTo catch
'the project file is copied on publish and retains its original modified date
Dim ProjectLastSave As Date
ProjectLastSave = FileDateTime(Project.FileName)
'We can only get the batch class publish date if "Copy project during publish" is used
' otherwise we will just get the project path
Dim BatchClassPublishOrProjectPath As String
'if the "Copy project during publish" is checked, it will be located within PubTypes\Custom
If InStr(1, Project.FileName, "PubTypes\Custom") > 0 Then
'with "Copy project during publish" the folder containing the project is created
' (thus dated) while publishing
Dim ProjectFolder As String
ProjectFolder = Mid(Project.FileName, 1, InStrRev(Project.FileName, "\") - 1)
BatchClassPublishOrProjectPath = "published " & CStr(FileDateTime(ProjectFolder))
Else
'without "copy project during publish" the folder could have any date
' and the project could be anywhere, so just get the project path
BatchClassPublishOrProjectPath = Project.FileName
End If
'log basics like batch name, class, id, machine name, project save date
ScriptLog("Opening Batch """ & BATCH_NAME & """ (" & BATCH_ID & "/" & _
BATCH_ID_HEX & ") -- " & BATCH_USERSTRING & Environ("ComputerName") & vbNewLine & _
"Batch " & BATCH_ID & "/" & BATCH_ID_HEX & ": Batch Class """ & BATCH_CLASS & _
""" (" & BatchClassPublishOrProjectPath & ", project saved " & CStr(ProjectLastSave) _
& ")", LOCAL_LOG)
Exit Sub
'if there is an error, log it and try to keep going
catch:
ErrorLog(Err, "", Nothing, pXRootFolder, 0, ONLY_ON_ERROR, SUPPRESS_MSGBOX)
Resume Next
End Sub
'Find and return the Capture\Local\Logs directory
' caching the result to global variable CAPTURE_LOCAL_LOGS
Public Function Logging_CaptureLocalLogs() As String
'If CAPTURE_LOCAL_LOGS is already set, just return it
If CAPTURE_LOCAL_LOGS <> "" Then
Logging_CaptureLocalLogs = CAPTURE_LOCAL_LOGS
Exit Function
Else
On Error GoTo CouldNotCreate
'SMK 2013-01-16 - Introduced dependency on Wscript.Shell to read "Local" path from registry.
' Unlike previous method of manipulating environment path variables, this provides
' the correct path regardless of version or language of the OS.
'To use an early bound object (WshShell), add a reference to "Windows Script Host Object Model"
' C:\Windows\System32\wshom.ocx (C:\Windows\SysWOW64\wshom.ocx)
' Otherwise late bound object will be created via CreateObject("Wscript.Shell")
Dim wsh As Object
Set wsh = CreateObject("Wscript.Shell")
'Dim wsh As New WshShell
'The same registry location works on 32 or 64 bit OS (Windows redirect registry access from 32-bit apps to Wow6432Node as needed)
CAPTURE_LOCAL_LOGS=wsh.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Kofax Image Products\Ascent Capture\3.0\LocalPath") & "\Logs\"
'assume the batch log folder does not exist
Dim LogFolderExists As Boolean
LogFolderExists = False
'SMK 2013-01-16 - To simplify code, introduced dependency on Scripting.FileSystemObject.
'To use an early bound object(FileSystemObject), add a reference to "Microsoft Scripting Runtime"
' C:\Windows\System32\scrrun.dll (C:\Windows\SysWOW64\scrrun.dll)
' Otherwise late bound object will be created via CreateObject("Scripting.FileSystemObject")
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
'Dim fso As FileSystemObject
If Not fso.FolderExists(CAPTURE_LOCAL_LOGS) Then
fso.CreateFolder(CAPTURE_LOCAL_LOGS)
End If
LogFolderExists=True
'if creating the folder causes an error then FolderExists is still false
CouldNotCreate:
Err.Clear()
On Error GoTo catch
'if the folder still doesn't exist after trying to create, just use image path
If Not LogFolderExists Then
'if there is a problem getting the Capture local logs folder,
' log to the system temp folder
CAPTURE_LOCAL_LOGS = Environ("Temp") & "\"
End If
Logging_CaptureLocalLogs = CAPTURE_LOCAL_LOGS
End If
catch:
Set wsh=Nothing
Set fso=Nothing
End Function
'primary logging function
Public Sub ScriptLog(ByVal msg As String, Optional ByVal AddToLocalLog As Boolean = False, _
Optional ByVal pXDoc As CscXDocument = Nothing, _
Optional ByVal pXFolder As CscXFolder = Nothing, _
Optional ByVal PageNum As Integer = 0, _
Optional ByVal ExtraDepth As Integer = 0)
On Error GoTo catch
'for readability messages are logged on a newline after metadata,
' but this is a matter of preference
If Not LOG_SINGLE_LINE Then
msg = Replace(vbNewLine & msg, vbNewLine, vbNewLine & vbTab)
End If
'refer to the WinWrap documentation for "CallersLine Function" for
' an explaination of the Depth parameter
Dim Caller As String
Caller = CallersLine(ExtraDepth)
'In addition to the date/time, Timer makes it easy to see the number
' of seconds/hundredths of seconds between events
' output of Timer is also padded for readability in the log
Dim DateString As String
DateString = Now & " (" & Format(Timer, "00000.00") & ") -- "
'if we have an folder/xdoc/page (optional) we can log which document for context
Dim WhichDoc As String
WhichDoc = Logging_IdentifyFolderDocPage(pXFolder, pXDoc, PageNum)
If WhichDoc <> "" Then
WhichDoc = WhichDoc & " -- "
End If
'current module, class/function/line logged from
Dim ModuleAndFunction As String
ModuleAndFunction = Logging_ExecutionModeString() & " -- " & Logging_StackLine(Caller) & " "
' Always print a shorter message to the intermediate pane of the script window
' Previously this was only done in "Design" modes, but this meant it excluded testing runtime events
Debug.Print(Format(Timer, "00000.00") & " " & WhichDoc & ModuleAndFunction & msg)
'check if we are in design or runtime
If Project.ScriptExecutionMode = CscScriptModeServerDesign Or _
Project.ScriptExecutionMode = CscScriptModeValidationDesign Or _
Project.ScriptExecutionMode = CscScriptModeVerificationDesign Then
'if we are in project builder, there is no "image directory" so just write to local logs
' Because Application_InitializeBatch is only called manually in PB,
' use Logging_CaptureLocalLogs() directly to ensure the path is set
Open Logging_CaptureLocalLogs() & Format(Now(), "yyyymmdd") & DESIGN_LOG_FILENAME _
For Append As #1
Print #1, DateString & WhichDoc & ModuleAndFunction & msg
Close #1
Else
'In case logging is attempted without or before initialization, set temp path and unknown batch
'If Logging_InitializeBatch is called later it will correctly override these
If BATCH_IMAGE_LOGS = "" Then
'Keep in mind that a user has a different temp path per Windows session:
'https://stackoverflow.com/a/6521387/221018
BATCH_IMAGE_LOGS = Environ("Temp") & "\"
BATCH_ID_HEX = "Unknown-" & Format(Now(), "yyyymmdd")
End If
'Path with or without process ID
If PROCESS_ID_IN_BATCHLOG_FILENAME And PROCESS_ID > 0 Then
BATCH_LOG_FULLPATH = BATCH_IMAGE_LOGS & BATCH_ID_HEX & "_" & PROCESS_ID & BATCH_LOG_FILENAME
Else
BATCH_LOG_FULLPATH = BATCH_IMAGE_LOGS & BATCH_ID_HEX & BATCH_LOG_FILENAME
End If
'During runtime, always log to the batch log
Open BATCH_LOG_FULLPATH For Append As #1
'batches are processed from various machines, so each line in the batch log
' should specify the machine
Print #1, DateString & Environ("ComputerName") & " -- " & BATCH_USERSTRING & _
WhichDoc & ModuleAndFunction & msg
Close #1
'And add to local log if specified
If AddToLocalLog Then
Open Logging_CaptureLocalLogs() & Format(Now(), "yyyymmdd") & LOG_FILENAME _
For Append As #1
'the machine may be processing different batches/modules/users concurrently,
' so each line in the local log should have a batch id, userstring
Print #1, DateString & BATCH_ID_HEX & " -- " & BATCH_USERSTRING & _
WhichDoc & ModuleAndFunction & msg
Close #1
End If
End If
catch:
End Sub
'Primary error logging function. First parameter should always be "Err".
Public Sub ErrorLog(ByVal E As ErrObject, Optional ByVal ExtraInfo As String = "", _
Optional ByVal pXDoc As CscXDocument = Nothing, _
Optional ByVal pXFolder As CscXFolder = Nothing, _
Optional ByVal PageNum As Integer = 0, _
Optional ByVal ForceError As Boolean = False, _
Optional ByVal SuppressMsgBox As Boolean = False)
'checking if there is an error here means it does not need to be
' checked before the function is called
If E = 0 And ForceError = False Then
Exit Sub
End If
'ErrorMessage will be displayed To user In interactive modules
Dim ErrorMessage As String
ErrorMessage = "[Error] PID " & PROCESS_ID & " - " 'SMK 2013-05-16 include process ID in error
'FIX SMK 2013-01-16 include negative numbers
If E <> 0 Then
ErrorMessage = ErrorMessage & E.Number & " - " & E.Description
End If
If ExtraInfo <> "" Then
ErrorMessage = ErrorMessage & " " & ExtraInfo
End If
'when the error handler is set it clears the error, so we must finish with the e param first
E.Clear()
On Error GoTo catch
'get stack trace
Dim Stack As String
'1 extra depth to ignore this current function in the stack
Stack = Logging_StackTrace(IGNORE_CURRENT_FUNCTION)
'Add stack trace to the error message
ErrorMessage = ErrorMessage & vbNewLine & Stack
'log the error and stacktrace
ScriptLog(ErrorMessage, LOCAL_LOG, pXDoc, pXFolder, PageNum, IGNORE_CURRENT_FUNCTION)
'Display to user if not in Server or thin client
If Project.ScriptExecutionMode <> CscScriptModeServer And Not THIN_CLIENT And _
Not SuppressMsgBox Then
'if the message needs to be localized, other languages can be added
' as seen in the script help topic:
'Script Samples | Displaying Translated Error Messages For a Script Validation Method
Dim LocalizedMessage As String
Select Case Application.UILanguage
Case "en-US" 'American English
LocalizedMessage = USER_ERROR_MSG
Case Else
LocalizedMessage = USER_ERROR_MSG
End Select
'include info about where we are
Dim WhichDoc As String
WhichDoc = Logging_IdentifyFolderDocPage(pXFolder, pXDoc, PageNum)
'include batch info if it exists
If BATCH_ID_HEX <> "" Then
WhichDoc = BATCH_NAME & " (" & BATCH_ID_HEX & "), Batch Class: " & BATCH_CLASS _
& vbNewLine & WhichDoc
End If
MsgBox(LocalizedMessage & vbNewLine & vbNewLine & WhichDoc & vbNewLine & vbNewLine & _
ErrorMessage, vbCritical, "Script Error")
End If
catch:
End Sub
'returns a stacktrace from where ever it is called
Public Function Logging_StackTrace(Optional ByVal ExtraDepth As Integer = 0) As String
On Error GoTo catch
Dim i As Integer
i = ExtraDepth
Dim CurrentStackLine As String
CurrentStackLine = CallersLine(i)
'as long as CallersLine returns something, stacktrace continues
While CurrentStackLine <> ""
'get a nicer format for the stack line
CurrentStackLine = i & ": " & Logging_StackLine(CurrentStackLine) & _
Mid(CurrentStackLine, InStr(1, CurrentStackLine, "]") + 1)
'Add current line to the stack trace
Logging_StackTrace = Logging_StackTrace & CurrentStackLine & vbNewLine
'increment and try to get the next line (CallersLine returns blank if none)
i = i + 1
CurrentStackLine = CallersLine(i)
'protect against trying to log a large stack
If i > 10 Then
Logging_StackTrace = Logging_StackTrace & i & ": ...Stack continues beyond " & _
i - 1 & " frames..."
Exit While
End If
Wend
'on error exit
catch:
End Function
'Returns string from ScriptExecutionMode Enum to indicate which module is running
Public Function Logging_ExecutionModeString() As String
On Error GoTo catch
'There is not currently a way to tell if a script is executing in a rich or thin client
' This is important because MsgBox cannot be used if we are in a thin client
' If a thin client is enabled for the project and we are in that module,
' we must assume it is a thin client
THIN_CLIENT = False
Select Case Project.ScriptExecutionMode
Case CscScriptModeServer
Logging_ExecutionModeString = "Server " & Project.ScriptExecutionInstance
Case CscScriptModeServerDesign
Logging_ExecutionModeString = "ServerDesign " & Project.ScriptExecutionInstance
Case CscScriptModeUnknown
Logging_ExecutionModeString = "Unknown"
Case CscScriptModeValidation
Logging_ExecutionModeString = "Validation " & Project.ScriptExecutionInstance
If Project.WebBasedValidationEnabled Then
THIN_CLIENT = True
End If
Case CscScriptModeValidationDesign
Logging_ExecutionModeString = "ValidationDesign " & Project.ScriptExecutionInstance
Case CscScriptModeVerification
Logging_ExecutionModeString = "Verification"
If Project.WebBasedVerificationEnabled Then
THIN_CLIENT = True
End If
Case CscScriptModeVerificationDesign
Logging_ExecutionModeString = "VerificationDesign"
Case CscScriptModeDocumentReview
Logging_ExecutionModeString = "DocumentReview"
If Project.WebBasedDocumentReviewEnabled Then
THIN_CLIENT = True
End If
Case CscScriptModeCorrection
Logging_ExecutionModeString = "Correction"
If Project.WebBasedCorrectionEnabled Then
THIN_CLIENT = True
End If
Case Else
Logging_ExecutionModeString = "BeyondUnknown (" & Project.ScriptExecutionMode & ")"
End Select
If THIN_CLIENT Then
Logging_ExecutionModeString = Logging_ExecutionModeString & " (TC)"
End If
Exit Function
catch:
Logging_ExecutionModeString = "Unknown Module (Error " & Err.Number & ")"
End Function
'return [classname|subname#linenum]
' input is the return of WinWrap's CallersLine function: "[macroname|subname#linenum] linetext"
' refer to the WinWrap documentation for "CallersLine Function" regarding the Depth parameter
Public Function Logging_StackLine(ByVal Caller As String) As String
On Error GoTo catch
'the function name (subname) and linenum are followed by a ]
Dim EndPos As Integer
EndPos = InStr(Caller, "]")
'the function name will follow a |
Dim StartPos As Integer
StartPos = InStrRev(Caller, "|", EndPos) + 1
'get the function name
Dim FunctionAndLine As String
FunctionAndLine = Mid(Caller, StartPos, EndPos - StartPos)
'combine with class/folder
Logging_StackLine = "[" & Logging_SheetClass(Caller) & "|" & FunctionAndLine & "]"
Exit Function
catch:
FunctionAndLine = "Unknown Function (Error " & Err.Number & ")"
End Function
'return the name of the folder or class of the script at the given depth
' input is the return of WinWrap's CallersLine function: "[macroname|subname#linenum] linetext"
' refer to the WinWrap documentation for "CallersLine Function" regarding the Depth parameter
Public Function Logging_SheetClass(ByVal Caller As String) As String
On Error GoTo catch
'the sheet name (macroname) is followed by a |
Dim EndPos As Integer
EndPos = InStr(Caller, "|")
'the sheet name will follow a \ from Project Builder
'Project Script: [C:\ProjectFolder\ScriptProject|Document_BeforeProcessXDoc#827] 'Code
'Other Classes: [C:\1|ValidationForm_ButtonClicked# 18] 'Code
Dim StartPosPB As Integer
StartPosPB = InStrRev(Caller, "\", EndPos) + 1
'the sheet name will follow a * from runtime modules
'[*ScriptProject|Document_BeforeProcessXDoc#881] 'Code
Dim StartPosRuntime As Integer
StartPosRuntime = InStrRev(Caller, "*", EndPos) + 1
'Use whichever start position is found
Dim StartPos As Integer
If StartPosPB > StartPosRuntime Then
StartPos = StartPosPB
Else
StartPos = StartPosRuntime
End If
'get the sheet name
Dim Sheet As String
Sheet = Mid(Caller, StartPos, EndPos - StartPos)
'numeric sheet names should be classes or folders
If IsNumeric(Sheet) Then
Dim SheetNum As Long
SheetNum = CLng(Sheet)
'sheet numbers higher than zero are classes
If SheetNum > 0 Then
Dim TheClass As CscClass
Set TheClass = Project.ClassByID(SheetNum)
'make sure the class actually exists to prevent an error accessing the name
If Not TheClass Is Nothing Then
Logging_SheetClass = TheClass.Name
Else
Logging_SheetClass = "Unknown Class (" & SheetNum & ")"
End If
Else
'negative sheet numbers are folders (use absolute value for folder level)
SheetNum = Abs(SheetNum)
Dim TheFolder As CscFolderDef
Set TheFolder = Project.FolderByLevel(SheetNum)
'make sure the folder actually exists to prevent an error accessing the name
If Not TheFolder Is Nothing Then
Logging_SheetClass = TheFolder.Name
Else
Logging_SheetClass = "Unknown Folder (" & SheetNum & ")"
End If
End If
ElseIf Sheet = "ScriptProject" Then
'Project level script has the special designation "ScriptProject"
Logging_SheetClass = "Project"
Else
Logging_SheetClass = "Unknown Class (" & Sheet & ")"
End If
Exit Function
catch:
Logging_SheetClass = "Unknown Class (Error " & Err.Number & ")"
End Function
'Meant to be called from Batch_Close, this will log routing, rejection, and other details
Public Sub Logging_BatchClose(ByVal pXRootFolder As CASCADELib.CscXFolder, _
ByVal CloseMode As CASCADELib.CscBatchCloseMode)
On Error GoTo catch
Select Case CloseMode
'routing is evaluated after Final, Suspend, and Error modes
Case CscBatchCloseMode.CscBatchCloseError
ErrorLog(Err, "Closing batch in error:" & BATCH_ID & "/" & BATCH_ID_HEX & ", " & _
BATCH_NAME, Nothing, pXRootFolder, 0, FORCE_ERROR, SUPPRESS_MSGBOX)
Logging_Routing(pXRootFolder)
'find any rejected docs
Dim RejectedMsg As String
Logging_RejectedDocs(pXRootFolder, RejectedMsg, pXRootFolder.XValues)
'if there are rejected docs/pages
If RejectedMsg <> "" Then
RejectedMsg = "The following have been rejected: " & vbNewLine & RejectedMsg
ErrorLog(Err, RejectedMsg, Nothing, pXRootFolder, 0, FORCE_ERROR, SUPPRESS_MSGBOX)
'Potentially take extra action if there is a script error
' (set by Logging_RejectedDocs)
If pXRootFolder.XValues.ItemExists("LOGGING_SCRIPT_ERROR") Then
'script error action
End If
End If
Case CscBatchCloseMode.CscBatchCloseSuspend
ScriptLog("Suspending Batch:" & BATCH_ID & "/" & BATCH_ID_HEX & ", " & _
BATCH_NAME, LOCAL_LOG)
Logging_Routing(pXRootFolder)
Case CscBatchCloseMode.CscBatchCloseFinal
ScriptLog("Batch Close")
Logging_Routing(pXRootFolder)
Case CscBatchCloseMode.CscBatchCloseParent
'Application_InitializeBatch is not called between Child and Parent Batch_Close (SPR00093890)
' so initialize logging paths again otherwise Parent logging will go to the Child log
Logging_InitializeBatch(pXRootFolder)
'Log that we have "opened" the parent batch
Logging_BatchOpen(pXRootFolder)
ScriptLog("Routing complete, closing parent batch.")
Case CscBatchCloseMode.CscBatchCloseChild
'Note that if a child batch has been routed to a new batch class,
' this will Batch_Close will not fire for the child
'Log that we have "opened" the child batch
Logging_BatchOpen(pXRootFolder)
'See if we can find out which tag this was created with during routing
Dim i As Integer
Dim BatchTag As String
For i = 0 To pXRootFolder.XValues.Count - 1
If Mid(pXRootFolder.XValues.ItemByIndex(i).Key, 1, _
Len("KTM_DOCUMENTROUTING_QUEUE_")) = "KTM_DOCUMENTROUTING_QUEUE_" Then
BatchTag = Mid(pXRootFolder.XValues.ItemByIndex(i).Key, _
Len("KTM_DOCUMENTROUTING_QUEUE_") + 1)
ScriptLog("This batch has been created as a result of routing with " & _
"the tag: " & BatchTag)
End If
Next
If BatchTag = "" Then
ScriptLog("This batch has been created as a result of routing.")
End If
Case Else
ErrorLog(Err, "Unknown Batch Close Type!", Nothing, pXRootFolder, 0, _
FORCE_ERROR, SUPPRESS_MSGBOX)
End Select
Exit Sub
'if there is an error, log it and try to keep going
catch:
ErrorLog(Err, "", Nothing, pXRootFolder, 0, ONLY_ON_ERROR, SUPPRESS_MSGBOX)
Resume Next
End Sub
'Recursive function to check for rejected docs/pages, called from Logging_BatchClose
Public Sub Logging_RejectedDocs(ByVal XFolder As CscXFolder, ByRef msg As String, _
ByRef XValues As CscXValues)
On Error GoTo catch
Dim i As Integer
'recurse into folders
For i = 0 To XFolder.Folders.Count - 1
Logging_RejectedDocs(XFolder.Folders.ItemByIndex(i), msg, XValues)
Next
Dim RejectionNote As String
'check documents
Dim XDocInfo As CscXDocInfo
For i = 0 To XFolder.DocInfos.Count - 1
Set XDocInfo = XFolder.DocInfos.ItemByIndex(i)
'check if the doc is rejected
If XDocInfo.XValues.ItemExists("AC_REJECTED_DOCUMENT") Then
'identify doc
msg = msg & Logging_IdentifyFolderDocPage(Nothing, XDocInfo.XDocument)
'add rejection note if exists
If XDocInfo.XValues.ItemExists("AC_REJECTED_DOCUMENT_NOTE") Then
RejectionNote = XDocInfo.XValues.ItemByName("AC_REJECTED_DOCUMENT_NOTE").Value
msg = msg & ": " & RejectionNote & vbNewLine
'if the rejection note mentions (S/s)cript,
' note for later that there has been a script error
If InStr(1, RejectionNote, "cript") > 0 Then
XValues.Set("LOGGING_SCRIPT_ERROR", "True")
End If
Else
msg = msg & vbNewLine
End If
End If
'check pages
Dim PageIndex As Long
For PageIndex = 0 To XDocInfo.PageCount - 1
'check if the page is rejected
If XDocInfo.XValues.ItemExists("AC_REJECTED_PAGE" & CStr(PageIndex + 1)) Then
'identify page
msg = msg & Logging_IdentifyFolderDocPage(Nothing, XDocInfo.XDocument, PageIndex + 1)
'add rejection note if exists
If XDocInfo.XValues.ItemExists("AC_REJECTED_PAGE_NOTE" & CStr(PageIndex + 1)) Then
RejectionNote = XDocInfo.XValues.ItemByName("AC_REJECTED_PAGE_NOTE" & _
CStr(PageIndex + 1)).Value
msg = msg & ": " & RejectionNote & vbNewLine
Else
msg = msg & vbNewLine
End If
End If
Next
Next
'on error log and exit
catch:
ErrorLog(Err, "", Nothing, Nothing, 0, ONLY_ON_ERROR, SUPPRESS_MSGBOX)
End Sub
'Called from Logging_BatchClose to log documents that will be routed
Public Sub Logging_Routing(ByVal pXRootFolder As CscXFolder)
On Error GoTo catch
'This will hold the routing information we find:
' key=LOGGING_ROUTING_batchtag, value=folders and docs
Dim RoutingGroups As CscXValues
Set RoutingGroups = pXRootFolder.XValues
'Set a flag saying all documents have been routed
' Finding an unrouted document will set this to false
RoutingGroups.Set("LOGGING_ALLROUTED", "True")
'recursively check folders for routing, adding results to RoutingGroups
Logging_RoutingFolder(pXRootFolder, RoutingGroups)
'If all docs are routed this batch will get deleted, so log this to local log
If pXRootFolder.XValues.ItemByName("LOGGING_ALLROUTED").Value = "True" Then
ScriptLog("All documents in the batch appear to be routed. " & _
"Batch will be deleted.", LOCAL_LOG)
End If
'log if the original batch will be routed to a module
If RoutingGroups.ItemExists("KTM_DOCUMENTROUTING_QUEUE_THISBATCH") Then
ScriptLog("This original batch will be routed to " & _
pXRootFolder.XValues.ItemByName("KTM_DOCUMENTROUTING_QUEUE_THISBATCH").Value)
End If
'go through the document routing groups and log details
Dim msg As String
Dim BatchTag As String
Dim i As Integer
For i = 0 To RoutingGroups.Count - 1
'if the XValue key begins with "LOGGING_ROUTING_"
If Mid(RoutingGroups.ItemByIndex(i).Key, 1, _
Len("LOGGING_ROUTING_")) = "LOGGING_ROUTING_" Then
'the part after "LOGGING_ROUTING_"
BatchTag = Mid(RoutingGroups.ItemByIndex(i).Key, Len("LOGGING_ROUTING_") + 1)
msg = msg & "Routing group (" & BatchTag
'check if it is being routed to a specific queue
If pXRootFolder.XValues.ItemExists("KTM_DOCUMENTROUTING_QUEUE_" & BatchTag) Then
msg = msg & ", Queue=" & _
pXRootFolder.XValues.ItemByName("KTM_DOCUMENTROUTING_QUEUE_" & BatchTag).Value
End If
'check if it is being routed with a specific batch name KTM 5.5+
If pXRootFolder.XValues.ItemExists("KTM_DOCUMENTROUTING_BATCHNAME_" & BatchTag) Then
msg = msg & ", Batch Name=" & _
pXRootFolder.XValues.ItemByName("KTM_DOCUMENTROUTING_BATCHNAME_" & BatchTag).Value
End If
'check if it is being routed to a new batch class KTM 5.5+
If pXRootFolder.XValues.ItemExists("KTM_DOCUMENTROUTING_NEWBATCHCLASS_" & BatchTag) Then
msg = msg & ", Batch Class=" & _
pXRootFolder.XValues.ItemByName("KTM_DOCUMENTROUTING_NEWBATCHCLASS_" & _
BatchTag).Value & _
" (module will be ignored)"
End If
msg = msg & "): " & RoutingGroups.ItemByIndex(i).Value & vbNewLine
End If
Next
'if there were any routing groups, msg won't be empty
If msg <> "" Then
ScriptLog(msg)
End If
'on error log and exit
catch:
ErrorLog(Err, "", Nothing, Nothing, 0, ONLY_ON_ERROR, SUPPRESS_MSGBOX)
End Sub
'recursively check folders for routed documents (or first level routed folders),
' adding results to RoutingGroups
Public Sub Logging_RoutingFolder(ByVal XFolder As CscXFolder, ByRef RoutingGroups As CscXValues)
On Error GoTo catch
'only 1st level folders can be routed (but any documents can be routed)
Dim IsFirstLevelFolder As Boolean
IsFirstLevelFolder = False
If XFolder.IsRootFolder = False Then 'not the root
If XFolder.ParentFolder.IsRootFolder = True Then 'parent is the root
IsFirstLevelFolder = True
End If
End If
Dim BatchTag As String
'check if this folder is being routed
If IsFirstLevelFolder And XFolder.XValues.ItemExists("KTM_DOCUMENTROUTING") Then
BatchTag = XFolder.XValues.ItemByName("KTM_DOCUMENTROUTING").Value
Dim FolderName As String
FolderName = Logging_IdentifyFolderDocPage(XFolder)
'check if we've already added this group
If RoutingGroups.ItemExists("LOGGING_ROUTING_" & BatchTag) Then
'add this folder
RoutingGroups.Set("LOGGING_ROUTING_" & BatchTag, _
RoutingGroups.ItemByName("LOGGING_ROUTING_" & BatchTag).Value & "," & FolderName)
Else
'create it and add this document
RoutingGroups.Set("LOGGING_ROUTING_" & BatchTag, FolderName)
End If
'if the folder is being routed, it will route the contents,
' and if routing instructions were set on these contents, they will be ignored
Else
'if the folder is not being routed, check if its subfolders
Dim SubFolder As CscXFolder
Dim i As Integer
For i = 0 To XFolder.Folders.Count - 1
Set SubFolder = XFolder.Folders.ItemByIndex(i)
Logging_RoutingFolder(SubFolder, RoutingGroups)
Next
Dim oXDocInfo As CscXDocInfo
Dim DocName As String
'check for routed docs in this folder
For i = 0 To XFolder.DocInfos.Count - 1
Set oXDocInfo = XFolder.DocInfos.ItemByIndex(i)
DocName = Logging_IdentifyFolderDocPage(Nothing, oXDocInfo.XDocument)
'check if this document is being routed
If oXDocInfo.XValues.ItemExists("KTM_DOCUMENTROUTING") Then
BatchTag = oXDocInfo.XValues.ItemByName("KTM_DOCUMENTROUTING").Value
'check if we've already added this group
If RoutingGroups.ItemExists("LOGGING_ROUTING_" & BatchTag) Then
'add this document
RoutingGroups.Set("LOGGING_ROUTING_" & BatchTag, _
RoutingGroups.ItemByName("LOGGING_ROUTING_" & BatchTag).Value & "," & DocName)
Else
'create it and add this document
RoutingGroups.Set("LOGGING_ROUTING_" & BatchTag, DocName)
End If
Else
'If a document is not being routed, we know they are not all routed
RoutingGroups.Set("LOGGING_ALLROUTED", "False")
End If
Next
End If
'on error log and exit
catch:
ErrorLog(Err, "", Nothing, Nothing, 0, ONLY_ON_ERROR, SUPPRESS_MSGBOX)
End Sub
'Identifies structure and files from Folder/Doc/Page.
Public Function Logging_IdentifyFolderDocPage(Optional ByVal XFolder As CscXFolder = Nothing, _
Optional ByVal XDoc As CscXDocument = Nothing, _
Optional ByVal PageNum As Integer = 0) As String
'valid parameter combinations:
'only folder
'only doc, implies folder
'doc and page number (page object doesn't link to parent doc)
On Error GoTo catch
'if doc was provided, use that to set folder
If Not XDoc Is Nothing Then
Set XFolder = XDoc.ParentFolder
Else
'if doc was not provided, make sure we have a folder, or exit
If XFolder Is Nothing Then
Exit Function
Else
'also exit if we only have root because we are omitting root folder info
If XFolder.IsRootFolder Then
Exit Function
End If
End If
End If
'Structure will show info like F#\F#\D#\P#
Dim DocStructure As String
'Files will show info like (#.xdc\#.tif(#))
' (#) is the page number in that document
' All folders are in Folder.xfd, so no need to add that
Dim Files As String
'2013-08-08 - SMK - XDoc.ParentFolder is not always set in KTM 6.0 Project Builder
' In that case, just skip this section
If Not XFolder Is Nothing Then
'get the folder structure (other than root folder since it is always there)
Do While Not XFolder.IsRootFolder
Set XFolder = XFolder.ParentFolder
DocStructure = "F" & XFolder.IndexInFolder + 1 & "\" & DocStructure
Files = Mid(XFolder.FileName, InStrRev(XFolder.FileName, "\")) & Files
Loop
End If
'get the document
If Not XDoc Is Nothing Then
DocStructure = DocStructure & "D" & XDoc.IndexInFolder + 1
Files = Files & Mid(XDoc.FileName, InStrRev(XDoc.FileName, "\") + 1)
End If
'get the page
If PageNum > 0 And Not XDoc Is Nothing Then
DocStructure = DocStructure & "\P" & PageNum
Dim Page As CscXDocPage
Set Page = XDoc.Pages.ItemByIndex(PageNum - 1)
'make sure the page exists
If Not Page Is Nothing Then
Files = Files & Mid(Page.SourceFileName, InStrRev(Page.SourceFileName, "\"))
End If
End If
Logging_IdentifyFolderDocPage = DocStructure & " (" & Files & ")"