home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Power Pack / Visual_Basic4_Power_Pack.bin / vb4files / mcvbehtp / vbehtp40.bas < prev    next >
Encoding:
BASIC Source File  |  1996-11-20  |  16.2 KB  |  488 lines

  1. Attribute VB_Name = "vbErrorHandler_bas"
  2. Option Explicit
  3.  
  4. Global Const VB_LNG_FRENCH = 1
  5. Global Const VB_LNG_DUTCH = 2
  6. Global Const VB_LNG_GERMAN = 3
  7. Global Const VB_LNG_ENGLISH = 4
  8. Global Const VB_LNG_ITALIAN = 5
  9. Global Const VB_LNG_SPANISH = 6
  10. Global Const VB_LNG_CATALAN = 7
  11. Global Const VB_LNG_POLISH = 8
  12.  
  13. Const MB_MESSAGE_LEFT = 0
  14.  
  15. #If Win16 Then
  16.  
  17. Declare Sub cPushID Lib "mcvb4016.dll" (IDArray As Integer, ByVal nID As Integer)
  18. Declare Sub cPopID Lib "mcvb4016.dll" (IDArray As Integer, ByVal nID As Integer)
  19. Declare Sub cPopLastID Lib "mcvb4016.dll" (IDArray As Integer)
  20. Declare Function cGetID Lib "mcvb4016.dll" (IDArray As Integer, ByVal nPosition As Integer) As Integer
  21. Declare Sub cClearID Lib "mcvb4016.dll" (IDArray As Integer)
  22. Declare Sub cChangeChars Lib "mcvb4016.dll" (Txt As String, CharSet As String, NewCharSet As String)
  23. Declare Function cGetIni Lib "mcvb4016.dll" (ByVal AppName As String, ByVal szItem As String, ByVal szDefault As String, ByVal InitFile As String) As String
  24. Declare Function cInsertBlocks Lib "mcvb4016.dll" (Txt As String, Insert As String) As String
  25. Declare Function cLngMsgBox Lib "mcvb4016.dll" (ByVal nLanguage As Integer, ByVal Message As String, ByVal Button As Long, ByVal Title As String) As Integer
  26. Declare Function cKillFileAll Lib "mcvb4016.dll" (ByVal lpFilename As String) As Integer
  27. Declare Function cTimerClose Lib "mcvb4016.dll" (ByVal TimerHandle As Integer) As Integer
  28. Declare Function cTimerOpen Lib "mcvb4016.dll" () As Integer
  29. Declare Function cTimerRead Lib "mcvb4016.dll" (ByVal TimerHandle As Integer) As Long
  30. Declare Function cTimerStart Lib "mcvb4016.dll" (ByVal TimerHandle As Integer) As Integer
  31.  
  32. #Else
  33.  
  34. Declare Sub cPushID Lib "mcvb4032.dll" (IDArray() As Integer, ByVal nID As Integer)
  35. Declare Sub cPopID Lib "mcvb4032.dll" (IDArray() As Integer, ByVal nID As Integer)
  36. Declare Sub cPopLastID Lib "mcvb4032.dll" (IDArray() As Integer)
  37. Declare Function cGetID Lib "mcvb4032.dll" (IDArray() As Integer, ByVal nPosition As Integer) As Integer
  38. Declare Sub cClearID Lib "mcvb4032.dll" (IDArray() As Integer)
  39. Declare Sub cChangeChars Lib "mcvb4032.dll" (Txt As String, CharSet As String, NewCharSet As String)
  40. Declare Function cGetIni Lib "mcvb4032.dll" (ByVal AppName As String, ByVal szItem As String, ByVal szDefault As String, ByVal InitFile As String) As String
  41. Declare Function cInsertBlocks Lib "mcvb4032.dll" (Txt As String, Insert As String) As String
  42. Declare Function cLngMsgBox Lib "mcvb4032.dll" (ByVal nLanguage As Integer, ByVal Message As String, ByVal Button As Long, ByVal Title As String) As Integer
  43. Declare Function cKillFileAll Lib "mcvb4032.dll" (ByVal lpFilename As String) As Integer
  44. Declare Function cTimerClose Lib "mcvb4032.dll" (ByVal TimerHandle As Integer) As Integer
  45. Declare Function cTimerOpen Lib "mcvb4032.dll" () As Integer
  46. Declare Function cTimerRead Lib "mcvb4032.dll" (ByVal TimerHandle As Integer) As Long
  47. Declare Function cTimerStart Lib "mcvb4032.dll" (ByVal TimerHandle As Integer) As Integer
  48.  
  49. #End If
  50.  
  51. 'Don't change any variables and their value below
  52.  
  53. Const ID_ITEMS = 16
  54.  
  55. Type tagERRORHANDLERtype
  56.    ModuleName                       As String * 256
  57.    RoutineHandle                    As String * 4
  58.    RoutineName                      As String * 76
  59.    CrLf                             As String * 2
  60. End Type
  61.  
  62. Type tagTRACERtype
  63.    StartStop                        As String * 1
  64.    RoutineHandle                    As Integer
  65. End Type
  66.  
  67. Type tagPROFILERtype
  68.    ModuleName                       As String * 256
  69.    RoutineHandle                    As String * 4
  70.    RoutineName                      As String * 76
  71.    TimeCounter                      As Long
  72.    TotalCall                        As Long
  73.    TotalTime                        As Long
  74.    MinimumTime                      As Long
  75.    MaximumTime                      As Long
  76.    Dummy                            As String * 10
  77.    CrLf                             As String * 2
  78. End Type
  79.  
  80. Dim TotalRoutines                   As Integer
  81. Dim ActualTrace                     As Long
  82. Dim OldStartRoutine                 As Integer
  83. Dim OldStopRoutine                  As Integer
  84.  
  85. Dim FileTR                          As String
  86. Dim FilePF                          As String
  87.  
  88. Dim chanFileTR                      As Integer
  89. Dim chanFilePF                      As Integer
  90.  
  91. Dim FileLNG                         As String
  92.  
  93. Dim FileHND                         As String
  94.  
  95. Dim FileLOG                         As String
  96.  
  97. Dim IDArray(0 To ID_ITEMS)          As Integer
  98.  
  99. Dim Language                        As Integer
  100. Dim AutoLog                         As Integer
  101. Dim WaitingTimeForReaction          As Integer
  102. Dim DefaultButton                   As Integer
  103. Dim DisplayOnline                   As Integer
  104. Dim TraceProfile                    As Integer
  105.  
  106. Dim TotalSameHandle                 As Long
  107. Dim LastHandle                      As Integer
  108. Dim ChanHandle                      As Integer
  109. Dim OldChanHandle                   As Integer
  110.  
  111. Dim tagERRORHANDLER                 As tagERRORHANDLERtype
  112. Dim tagTRACER                       As tagTRACERtype
  113. Dim tagPROFILER                     As tagPROFILERtype
  114.  
  115.  
  116. Sub mcClearID()
  117.    #If Win16 Then
  118.       Call cClearID(IDArray(0))
  119.    #Else
  120.       Call cClearID(IDArray)
  121.    #End If
  122. End Sub
  123.  
  124. Function mcGetID(nPos As Integer)
  125.    #If Win16 Then
  126.       mcGetID = cGetID(IDArray(0), nPos)
  127.    #Else
  128.       mcGetID = cGetID(IDArray, nPos)
  129.    #End If
  130. End Function
  131.  
  132. Function mcGetLanguageID(LanguageID As Integer) As String
  133.  
  134.    Dim RetLanguage      As String
  135.  
  136.    Select Case LanguageID
  137.       Case VB_LNG_FRENCH
  138.          RetLanguage = "VFR"
  139.       Case VB_LNG_DUTCH
  140.          RetLanguage = "VNL"
  141.       Case VB_LNG_GERMAN
  142.          RetLanguage = "VDE"
  143.       Case VB_LNG_ENGLISH
  144.          RetLanguage = "VUK"
  145.       Case VB_LNG_ITALIAN
  146.          RetLanguage = "VIT"
  147.       Case VB_LNG_SPANISH
  148.          RetLanguage = "VSP"
  149.       Case VB_LNG_CATALAN
  150.          RetLanguage = "VCA"
  151.       Case VB_LNG_POLISH
  152.          RetLanguage = "VPO"
  153.       Case Else
  154.          RetLanguage = "VUK"
  155.    End Select
  156.    
  157.    If (LanguageID > 0) Then
  158.       Language = LanguageID
  159.    Else
  160.       Language = VB_LNG_ENGLISH
  161.    End If
  162.  
  163.    mcGetLanguageID = RetLanguage
  164.  
  165. End Function
  166.  
  167. Function mcIDErrorHandler(nErr As Integer) As Integer
  168.  
  169.    ' check if this a correct Error passed
  170.    If (nErr = 0) Then
  171.       'if no, resume next
  172.       mcIDErrorHandler = True
  173.       Exit Function
  174.    End If
  175.  
  176.    Dim RoutineCount     As Integer
  177.    Dim RoutineNumber    As Integer
  178.    Dim RoutineStack     As String
  179.    Dim TotalRoutines    As Integer
  180.    Dim BlankLines       As Integer
  181.    Dim Chan             As Integer
  182.    Dim StopExit         As Integer
  183.    Dim TimeOut          As Long
  184.    Dim ButtonsConfig    As Integer
  185.    Dim ErrorTitle       As String
  186.  
  187.    '  some initializations
  188.    RoutineStack = ""
  189.    TotalRoutines = 0
  190.    BlankLines = 0
  191.    StopExit = False
  192.    ButtonsConfig = 0
  193.    ErrorTitle = ""
  194.    RoutineStack = RoutineStack + mcReadText("0", "")
  195.    
  196.    ' find the next valid unused file number.
  197.    Chan = FreeFile
  198.  
  199.    ' open the file with the definition of each routines (file must be in the WINDOWS directory)
  200.    Close #Chan
  201.    Open FileHND For Random Shared As #Chan Len = Len(tagERRORHANDLER)
  202.  
  203.    ' get the stack of the routines
  204.    For RoutineCount = 0 To ID_ITEMS
  205.       ' get the number of the routine
  206.       RoutineNumber = mcGetID(RoutineCount)
  207.       ' if there a valid routine number
  208.       If (RoutineNumber > 0) Then
  209.          ' yes, read the definition of the routine
  210.          Get #Chan, RoutineNumber, tagERRORHANDLER
  211.          ' form the stack of the routines founden to display
  212.          RoutineStack = RoutineStack + Left$(tagERRORHANDLER.ModuleName + Space$(12), 14) + Chr$(9) + tagERRORHANDLER.RoutineHandle + Chr$(9) + Trim$(tagERRORHANDLER.RoutineName) + Chr$(13)
  213.          ' count the routines to display
  214.          TotalRoutines = TotalRoutines + 1
  215.       Else
  216.          ' no, exit from reading the stack
  217.          Exit For
  218.       End If
  219.    Next RoutineCount
  220.  
  221.    ' close the open file
  222.    Close #Chan
  223.  
  224.    ' check if the default button must be activated
  225.    If (DefaultButton = True) Then
  226.       ' yes, RETRY and CANCEL with RETRY is the default
  227.       ButtonsConfig = 5 Or 0
  228.    Else
  229.       ' no, RETRY and CANCEL with CANCEL is the default
  230.       ButtonsConfig = 5 Or 256
  231.       ' yes, add text for RETRY after timeout or action
  232.       RoutineStack = RoutineStack & Chr$(13) & Chr$(13) & "program will be stopped"
  233.    End If
  234.  
  235.    ' set the error title
  236.    ErrorTitle = mcReadText("1", nErr & "~" & Error$(nErr))
  237.  
  238.    ' check if one routine has been founded
  239.    If (Len(RoutineStack) > 0) Then
  240.       ' check the time out
  241.       TimeOut = WaitingTimeForReaction * (163840 Or 524288)
  242.       ' display remaining blank lines
  243.       BlankLines = (8 - TotalRoutines) - (TimeOut = 0)
  244.       For RoutineCount = 0 To BlankLines
  245.          RoutineStack = RoutineStack + Chr$(13)
  246.       Next RoutineCount
  247.       ' add some text for management
  248.       RoutineStack = RoutineStack & mcReadText("2", "")
  249.       ' check if a timeout must be used
  250.       If (TimeOut <> 0) Then
  251.          ' yes, add text depending of the default button
  252.          RoutineStack = RoutineStack & mcReadText("3", "") & " "
  253.          ' if default is RETRY then display 'continue' else 'stop'
  254.          If (DefaultButton = True) Then
  255.             RoutineStack = RoutineStack & mcReadText("4", "")
  256.          Else
  257.             RoutineStack = RoutineStack & mcReadText("5", "")
  258.          End If
  259.       End If
  260.       ' display the error message box
  261.       StopExit = (cLngMsgBox(Language, RoutineStack, MB_MESSAGE_LEFT Or TimeOut Or ButtonsConfig Or 16, ErrorTitle) = 2)
  262.       ' yield process
  263.       DoEvents
  264.    End If
  265.  
  266.    ' check if an auto logging must be performed
  267.    If (AutoLog = True) Then
  268.  
  269.       ' open the logging file in append mode
  270.       Close #Chan
  271.       Open FileLOG For Append Shared As #Chan
  272.  
  273.       ' save the error and his description
  274.       Print #Chan, ErrorTitle; " "; mcReadText("6", Date$ & "~" & Time$)
  275.       Print #Chan, ""
  276.       ' save the full stack name of each routines founden
  277.       Print #Chan, RoutineStack
  278.       Print #Chan, ""
  279.       ' check if the CANCEL button pushed or TimeOut
  280.       If (StopExit = True) Then
  281.          ' yes stop by operator, save text for CANCEL
  282.          Print #Chan, mcReadText("7", "")
  283.       Else
  284.          ' no, retry by operator, save text for RETRY
  285.          Print #Chan, mcReadText("8", "")
  286.       End If
  287.       ' save separator
  288.       Print #Chan, String$(78, "-")
  289.  
  290.       ' close the file
  291.       Close #Chan
  292.  
  293.    End If
  294.  
  295.    ' if stop the program the END the application
  296.    If (StopExit = True) Then End
  297.  
  298.    ' no stop, resumes to next line in the main application
  299.    mcIDErrorHandler = True
  300.  
  301. End Function
  302.  
  303. Sub mcOnlineDisplay(ID As Integer)
  304.  
  305.    Dim ActualLine    As String
  306.  
  307.    If (ChanHandle = -1) Then
  308.  
  309.       ' close the old chan if more than 1 mcInitID is called
  310.       If (OldChanHandle <> -1) Then Close #OldChanHandle
  311.  
  312.       ' find the next valid unused file number.
  313.       ChanHandle = FreeFile
  314.  
  315.       ' open the file with the definition of each routines (file must be in the WINDOWS directory)
  316.       Close #ChanHandle
  317.       Open FileHND For Random Shared As #ChanHandle Len = Len(tagERRORHANDLER)
  318.  
  319.       ' save the handle
  320.       OldChanHandle = ChanHandle
  321.  
  322.    End If
  323.  
  324.    ' read the handle
  325.    Get #ChanHandle, ID, tagERRORHANDLER
  326.  
  327.    If (LastHandle = ID) Then
  328.       TotalSameHandle = TotalSameHandle + 1
  329.    Else
  330.       If (frmDisplayOnline.lstOnline.ListIndex > -1) Then
  331.          ActualLine = frmDisplayOnline.lstOnline.List(frmDisplayOnline.lstOnline.ListIndex)
  332.          frmDisplayOnline.lstOnline.List(frmDisplayOnline.lstOnline.ListIndex) = TotalSameHandle & Mid$(ActualLine, InStr(ActualLine, Chr$(9)))
  333.       End If
  334.       TotalSameHandle = 1
  335.    End If
  336.  
  337.    frmDisplayOnline.lblCounter = TotalSameHandle
  338.    frmDisplayOnline.lblHandle = ID
  339.  
  340.    If (LastHandle <> ID) Then
  341.       frmDisplayOnline.lstOnline.AddItem TotalSameHandle & Chr$(9) & Trim$(tagERRORHANDLER.RoutineHandle) & Chr$(9) & Trim$(tagERRORHANDLER.ModuleName) & Chr$(9) & Trim$(tagERRORHANDLER.RoutineName)
  342.       If (frmDisplayOnline.lstOnline.ListCount > 25) Then frmDisplayOnline.lstOnline.RemoveItem 0
  343.       frmDisplayOnline.lstOnline.ListIndex = frmDisplayOnline.lstOnline.NewIndex
  344.    End If
  345.  
  346.    LastHandle = ID
  347.  
  348.    DoEvents
  349.  
  350. End Sub
  351.  
  352. Sub mcPopID(ID As Integer)
  353.    #If Win16 Then
  354.       Call cPopID(IDArray(0), ID)
  355.    #Else
  356.       Call cPopID(IDArray, ID)
  357.    #End If
  358. End Sub
  359.  
  360. Sub mcPopLastID(WhichManagement As Integer, RoutineNumber As Integer)
  361.    If ((Abs(WhichManagement) = 2) Or (Abs(WhichManagement) = 3)) Then Call mcStopTracer(RoutineNumber)
  362.    #If Win16 Then
  363.       Call cPopLastID(IDArray(0))
  364.    #Else
  365.       Call cPopLastID(IDArray)
  366.    #End If
  367. End Sub
  368.  
  369. Sub mcPushID(ID As Integer)
  370.    #If Win16 Then
  371.       Call cPushID(IDArray(0), ID)
  372.    #Else
  373.       Call cPushID(IDArray, ID)
  374.    #End If
  375.    If (DisplayOnline = True) Then Call mcOnlineDisplay(ID)
  376. End Sub
  377.  
  378. Function mcReadText(TextOrder As String, InsertText As String) As String
  379.  
  380.    Dim Tmp              As String
  381.    Dim BasisText        As String
  382.  
  383.    ' read the text in the language file
  384.    BasisText = cGetIni("mcvbehtp", TextOrder, "?", FileLNG)
  385.    
  386.    ' insert some text if any
  387.    Tmp = cInsertBlocks(BasisText, InsertText)
  388.  
  389.    ' change all º by a CR and all ú by TAB
  390.    Call cChangeChars(Tmp, "ºú", Chr$(13) + Chr$(9))
  391.  
  392.    mcReadText = Tmp
  393.  
  394. End Function
  395.  
  396. Sub mcStartTracer(RoutineNumber As Integer)
  397.  
  398.    Dim TimerCounter  As Integer
  399.    Dim Status        As Integer
  400.  
  401.    ' check if the routine number is not outside the limits
  402.    If ((RoutineNumber < 1) Or (RoutineNumber > TotalRoutines)) Then Exit Sub
  403.  
  404.    ' check if this is the same routine
  405.    If (OldStartRoutine <> RoutineNumber) Then
  406.       ' increment the trace number
  407.       ActualTrace = ActualTrace + 1
  408.       ' prepare the trace information
  409.       tagTRACER.StartStop = ">"
  410.       tagTRACER.RoutineHandle = RoutineNumber
  411.       ' save the trace information
  412.       Put #chanFileTR, ActualTrace, tagTRACER
  413.    End If
  414.  
  415.    ' save the old routine
  416.    OldStartRoutine = RoutineNumber
  417.  
  418.    ' read the record associated with the routine number
  419.    Get #chanFilePF, RoutineNumber, tagPROFILER
  420.  
  421.    ' open a timer
  422.    TimerCounter = cTimerOpen()
  423.    ' save the handle of the new timer
  424.    tagPROFILER.TimeCounter = TimerCounter
  425.    ' increment the number of calls
  426.    tagPROFILER.TotalCall = tagPROFILER.TotalCall + 1
  427.  
  428.    ' save the record associated with the routine number
  429.    Put #chanFilePF, RoutineNumber, tagPROFILER
  430.  
  431.    ' start the timer
  432.    Status = cTimerStart(TimerCounter)
  433.  
  434. End Sub
  435.  
  436. Sub mcStopTracer(RoutineNumber As Integer)
  437.  
  438.    Dim TimerCounter  As Integer
  439.    Dim TimeElapsed   As Long
  440.    Dim Status        As Integer
  441.  
  442.    ' check if the routine number is not outside the limits
  443.    If ((RoutineNumber < 1) Or (RoutineNumber > TotalRoutines)) Then Exit Sub
  444.  
  445.    ' check if this is the same routine
  446.    If (OldStopRoutine <> RoutineNumber) Then
  447.       ' increment the trace number
  448.       ActualTrace = ActualTrace + 1
  449.       ' prepare the trace information
  450.       tagTRACER.StartStop = "<"
  451.       tagTRACER.RoutineHandle = RoutineNumber
  452.       ' save the trace information
  453.       Put #chanFileTR, ActualTrace, tagTRACER
  454.    End If
  455.  
  456.    ' save the old routine
  457.    OldStopRoutine = RoutineNumber
  458.  
  459.    ' read the record associated with the routine number
  460.    Get #chanFilePF, RoutineNumber, tagPROFILER
  461.  
  462.    ' check if the timer is valid
  463.    If (tagPROFILER.TimeCounter > 0) Then
  464.       ' computes the elapsed time
  465.       TimeElapsed = cTimerRead(tagPROFILER.TimeCounter)
  466.       ' add the elapsed time
  467.       tagPROFILER.TotalTime = tagPROFILER.TotalTime + TimeElapsed
  468.       ' check for the minimum time
  469.       If (TimeElapsed < tagPROFILER.MinimumTime) Then tagPROFILER.MinimumTime = TimeElapsed
  470.       ' check for the minimum time
  471.       If (TimeElapsed > tagPROFILER.MaximumTime) Then tagPROFILER.MaximumTime = TimeElapsed
  472.    End If
  473.  
  474.    ' save the record associated with the routine number
  475.    Put #chanFilePF, RoutineNumber, tagPROFILER
  476.  
  477.    ' close the associated timer
  478.    Status = cTimerClose(tagPROFILER.TimeCounter)
  479.  
  480. End Sub
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.