ESMF_LogOpenFile Subroutine

private subroutine ESMF_LogOpenFile(alog, rc)

Arguments

Type IntentOptional Attributes Name
type(ESMF_LogPrivate), intent(inout) :: alog
integer, intent(out) :: rc

Source Code

  subroutine ESMF_LogOpenFile (alog, rc)
!
! !ARGUMENTS:
!
    type(ESMF_LogPrivate), intent(inout) :: alog
    integer,               intent(out)   :: rc
!
! !DESCRIPTION:
!     This subroutine opens the log file and allocates the log buffer.
!
!     The arguments are:
!     \begin{description}
!
!     \item [alog]
!       Internal Log object.
!     \item [rc]
!       Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
!     \end{description}
!
!EOPI

    type(ESMF_LogEntry), pointer :: localbuf(:)
    character(8) :: position
    integer :: i
    integer :: memstat, iostat
    integer :: localrc

    ! Initialize return code; assume routine not implemented
    rc=ESMF_RC_NOT_IMPL

    ! find an available unit number
    call ESMF_UtilIOUnitGet (alog%unitNumber, rc=localrc)
    if (localrc /= ESMF_SUCCESS) then
      rc=ESMF_RC_CANNOT_GET
    endif

    position = merge ("append", "rewind", alog%appendFlag)

    ! open the file, with retries
    do i=1, ESMF_LOG_MAXTRYOPEN
#if !defined (ESMF_OS_MinGW)
        OPEN(UNIT=alog%unitNumber,File=alog%nameLogErrFile,&
             POSITION=position, ACTION="WRITE", STATUS="UNKNOWN", IOSTAT=iostat)
#else
#if defined (__INTEL_COMPILER)
        OPEN(UNIT=alog%unitNumber,File=alog%nameLogErrFile,&
             POSITION=position, ACTION="WRITE", STATUS="UNKNOWN", &
             SHARE="DENYNONE", IOSTAT=iostat)
#else
        OPEN(UNIT=alog%unitNumber,File=alog%nameLogErrFile,&
             POSITION=position, ACTION="WRITE", STATUS="UNKNOWN", IOSTAT=iostat)
#endif
#endif
        if (iostat == 0) then
            alog%FileIsOpen = ESMF_TRUE
            exit
        endif
    enddo

    ! if unable to open file then error out
    if (alog%FileIsOpen /= ESMF_TRUE) then
      write (ESMF_UtilIOStderr,*) ESMF_METHOD,  &
          ': error opening file: ', trim (alog%nameLogErrFile),  &
          ', iostat =', iostat
      call ESMF_UtilIOUnitFlush(ESMF_UtilIOStderr, rc=rc)
      rc=ESMF_RC_FILE_UNEXPECTED
      return
    endif

    ! BEWARE:  absoft 8.0 compiler bug - if you try to allocate directly
    ! you get an error.  if you allocate a local buffer and then point the
    ! derived type buffer at it, it works.  go figure.

    allocate(localbuf(alog%maxElements), stat=memstat)
    if (memstat /= 0) then
      write (ESMF_UtilIOStderr,*) ESMF_METHOD,  &
          ': Allocation of buffer failed.'
      call ESMF_UtilIOUnitFlush(ESMF_UtilIOStderr, rc=rc)
      rc = ESMF_RC_MEM_ALLOCATE
      return
    endif
    alog%LOG_ENTRY => localbuf

    rc = ESMF_SUCCESS

  end subroutine ESMF_LogOpenFile