error_info_s.f90 Source File


Source Code

submodule (error_handling_m) error_info_s
    !! Contains functionality of the error info type
    implicit none
contains

    module subroutine print_error_info(this, channel, prefix)
        class(error_info_t), intent(in) :: this
        integer, intent(in) :: channel
        character(len=*), optional, intent(in) :: prefix

        integer :: mode
        character(len=:), allocatable :: c_print, prefix_local

        prefix_local = ""
        if(present(prefix)) prefix_local = prefix

        ! NOTE: It may be the case that i_info or r_info or both are not
        !       allocated, depending on the specific error info the user
        !       wants to give. The following construction using the variable
        !       mode avoids possible segfaults due to not allocated arrays.

        mode = 0
        if(.not. allocated(this%i_info)) mode = 1
        if(.not. allocated(this%r_info)) mode = mode + 2

        ! If the string message is empty, we replace it by a single space
        if(.not. allocated(this%c_info)) then
            c_print = prefix_local
        else
            c_print = prefix_local//trim(this%c_info)
        endif

        select case(mode)
            case(0)
                ! All allocated
                write(channel, *) c_print, this%i_info, this%r_info
            case(1)
                ! Integer array not allocated
                write(channel, *) c_print, this%r_info
            case(2)
                ! Real array not allocated
                write(channel, *) c_print, this%i_info
            case default
                ! Only string message
                write(channel, *) c_print
        end select
    end subroutine

end submodule