SUBROUTINE trackerrors(filevar, event, prerc, flags, recordkey, record, userrc)

*

*** Initialization

    INCLUDE JBC.h

    COMMON /trackerrors/ openflag, errorlog, jbcmessages

* This dynamic array contains the IDs of records in the jbcmessages file that we want to log

    error_ids = ''

    error_ids<-1> = 'DIVIDE_ZERO'

    error_ids<-1> = 'NON_NUMERIC'

    error_ids<-1> = 'ZERO_USED'



*

*** Open the ERRORLOG and jbcmessages files, once per session, to named common.

    IF UNASSIGNED(openflag) OR NOT(openflag) THEN

        openflag = @TRUE      ;* Assume successful OPENs

        OPEN 'ERRORLOG' TO errorlog ELSE openflag = @FALSE

        OPEN 'C:\custom\jbcmessages' TO jbcmessages ELSE openflag = @FALSE

        IF openflag = @FALSE THEN

            CRT 'Unable to open  ERRORLOG or jbcmessages.'

            ABORT

        END

    END

*

*** Log only the specific errors in the error_ids array

    IF event = TRIGGER_TYPE_POSTREAD THEN

        LOCATE recordkey IN error_ids SETTING position THEN

            READU rec FROM errorlog, recordkey ELSE rec = ''

            process = SYSTEM(1029)<1,1,4>

            rec<1,-1> = process

            rec<2,-1> = DATE()

            rec<3,-1> = TIME()

            rec<4,-1> = SYSTEM(18)      ;* port number

            rec<5,-1> = SYSTEM(19)      ;* user id

            rec<6,-1> = @PID

            WRITE rec ON errorlog, recordkey

        END

    END

    RETURN