!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Routines to manage MRTCAL messages
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
module mrtcal_messaging
  use gpack_def
  use gbl_message
  !
  public :: seve,mseve,message_length
  public :: mrtcal_message_set_id
  public :: mrtcal_message
  public :: failed_calibrate
  private
  !
  ! Identifier used for message identification
  integer(kind=4) :: mrtcal_message_id = gpack_global_id  ! Default value for startup message
  !
  type :: mrtcal_debug_calib_message_t
    integer(kind=4) :: alloc   ! Support for MSET DEBUG CALIB ALLOCATION
    integer(kind=4) :: book    ! Support for MSET DEBUG CALIB BOOKKEEPING
    integer(kind=4) :: others  ! Support for MSET DEBUG CALIB OTHERS
  end type mrtcal_debug_calib_message_t
  !
  type :: mrtcal_debug_output_message_t
    integer(kind=4) :: acc   ! Support for MSET DEBUG OUTPUT ACCUMULATION
  end type mrtcal_debug_output_message_t
  !
  type :: mrtcal_debug_message_t
    type(mrtcal_debug_calib_message_t)  :: calib  ! Support for MSET DEBUG CALIB
    type(mrtcal_debug_output_message_t) :: output ! Support for MSET DEBUG OUTPUT
    integer(kind=4)                     :: sync   ! Support for MSET DEBUG SYNC
  end type mrtcal_debug_message_t
  !
  type(mrtcal_debug_message_t) :: mseve
  !
  interface failed_calibrate
    module procedure failed_calibrate_name
    module procedure failed_calibrate_num
  end interface failed_calibrate
  !
contains
  !
  subroutine mrtcal_message_set_id(id)
    !---------------------------------------------------------------------
    ! Alter library id into input id. Should be called by the library
    ! which wants to share its id with the current one.
    !---------------------------------------------------------------------
    integer(kind=4), intent(in) :: id
    ! Local
    character(len=message_length) :: mess
    !
    mrtcal_message_id = id
    !
    write (mess,'(A,I3)') 'Now use id #',mrtcal_message_id
    call mrtcal_message(seve%d,'mrtcal_message_set_id',mess)
  end subroutine mrtcal_message_set_id
  !
  subroutine mrtcal_message(mkind,procname,message)
    !---------------------------------------------------------------------
    ! Messaging facility for the current library. Calls the low-level
    ! (internal) messaging routine with its own identifier.
    !---------------------------------------------------------------------
    integer(kind=4),  intent(in) :: mkind     ! Message kind
    character(len=*), intent(in) :: procname  ! Name of calling procedure
    character(len=*), intent(in) :: message   ! Message string
    !
    call gmessage_write(mrtcal_message_id,mkind,procname,message)
  end subroutine mrtcal_message
  !
  function failed_calibrate_name(rname,sname,error)
    !---------------------------------------------------------------------
    ! Display the subscan name if input error is .true.
    ! Nothing done if error is .false.
    !---------------------------------------------------------------------
    logical :: failed_calibrate_name  ! Function value on return
    character(len=*), intent(in) :: rname  ! Calling routine name
    character(len=*), intent(in) :: sname  ! Subscan name
    logical,          intent(in) :: error  ! Error status
    !
    failed_calibrate_name = error
    if (.not.error)  return
    !
    call mrtcal_message(seve%e,rname,'Failed for subscan '''//trim(sname)//'''')
  end function failed_calibrate_name
  !
  function failed_calibrate_num(rname,snum,error)
    !---------------------------------------------------------------------
    ! Display the subscan number if input error is .true.
    ! Nothing done if error is .false.
    !---------------------------------------------------------------------
    logical :: failed_calibrate_num  ! Function value on return
    character(len=*), intent(in) :: rname  ! Calling routine name
    integer(kind=4),  intent(in) :: snum   ! Subscan number
    logical,          intent(in) :: error  ! Error status
    ! Local
    character(len=message_length) :: mess
    !
    failed_calibrate_num = error
    if (.not.error)  return
    !
    write(mess,'(A,I0)')  'Failed for subscan #',snum
    call mrtcal_message(seve%e,rname,mess)
  end function failed_calibrate_num
end module mrtcal_messaging
