subroutine ESMF_LogFlush(log, keywordEnforcer, rc)
!
!
! !ARGUMENTS:
type(ESMF_Log), intent(inout), optional :: log
type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
integer, intent(out), optional :: rc
!
! !STATUS:
! \begin{itemize}
! \item\apiStatusCompatibleVersion{5.2.0r}
! \end{itemize}
!
! !DESCRIPTION:
! This subroutine flushes the file buffer associated with {\tt log}.
!
! The arguments are:
! \begin{description}
!
! \item [{[log]}]
! An optional {\tt ESMF\_Log} object that can be used instead
! of the default Log.
! \item [{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
!EOP
integer :: j
type(ESMF_LogPrivate),pointer :: alog
integer :: localrc, localrc2
integer :: memstat
logical :: spaceflag
! Initialize return code; assume routine not implemented
localrc = ESMF_RC_NOT_IMPL
if (present(rc)) then
rc=localrc
endif
ESMF_INIT_CHECK_SET_SHALLOW(ESMF_LogGetInit,ESMF_LogInit,log)
nullify(alog) ! ensure that the association status is well defined
if (present(log)) then
if (log%logTableIndex > 0) then
alog => ESMF_LogTable(log%logTableIndex)
else
localrc = ESMF_RC_OBJ_NOT_CREATED
endif
else
if (ESMF_LogDefault%logTableIndex > 0) then
alog => ESMF_LogTable(ESMF_LogDefault%logTableIndex)
end if
endif
if (associated(alog)) then
ESMF_INIT_CHECK_SET_SHALLOW(ESMF_LogPrivateGetInit,ESMF_LogPrivateInit,alog)
if (alog%FileIsOpen /= ESMF_TRUE) then
if (present (log)) then
write (ESMF_UtilIOStderr,*) ESMF_METHOD, &
": ESMF_Log not open -- cannot ESMF_LogFlush()."
call ESMF_UtilIOUnitFlush(ESMF_UtilIOStderr, rc=rc)
if (present (rc)) &
rc=ESMF_RC_FILE_OPEN
else
if (present (rc)) &
rc=ESMF_SUCCESS
end if
return
endif
if ((alog%FileIsOpen == ESMF_TRUE) .AND. &
(alog%flushed == ESMF_FALSE) .AND. &
(alog%dirty == ESMF_TRUE)) then
do j=1, alog%fIndex-1
if (.not. alog%LOG_ENTRY(j)%noprefix) then
write (alog%unitNumber, '(2a,3i2.2,a,i3.3,5a)', advance='no') &
alog%LOG_ENTRY(j)%d, " ", &
alog%LOG_ENTRY(j)%h, &
alog%LOG_ENTRY(j)%m, &
alog%LOG_ENTRY(j)%s, ".", &
alog%LOG_ENTRY(j)%ms, " ", &
alog%LOG_ENTRY(j)%lt, " ", &
trim(alog%petNumLabel), " "
if (alog%LOG_ENTRY(j)%highResTimestampFlag) then
write (alog%unitNumber, '(f18.6,1x)', advance='no') alog%LOG_ENTRY(j)%highResTimestamp
end if
end if ! noprefix
spaceflag = .false.
if (alog%LOG_ENTRY(j)%fileflag) then
write (alog%unitNumber, '(a)', advance='no') &
trim(alog%LOG_ENTRY(j)%file)
spaceflag = .true.
end if
if (alog%LOG_ENTRY(j)%lineflag) then
write (alog%unitNumber, '(a,i0)', advance='no') &
':', alog%LOG_ENTRY(j)%line
spaceflag = .true.
end if
if (alog%LOG_ENTRY(j)%methodflag) then
write (alog%unitNumber, '(1x,a)', advance='no') &
trim(alog%LOG_ENTRY(j)%method)
spaceflag = .true.
end if
if (spaceflag) then
write (alog%unitNumber, '(a)', advance='no') ' '
end if
write (alog%unitNumber, '(a)', advance='no') &
trim (ESMF_UtilArray2String (alog%LOG_ENTRY(j)%msg))
deallocate (alog%LOG_ENTRY(j)%msg, stat=memstat)
if (memstat /= 0) then
write (ESMF_UtilIOStderr,*) ESMF_METHOD, &
": Deallocation error."
call ESMF_UtilIOUnitFlush(ESMF_UtilIOStderr, rc=rc)
localrc = ESMF_RC_MEM_DEALLOCATE
if (present (rc)) then
rc = localrc
end if
return
end if
write (alog%unitNumber, *)
end do
end if
localrc = ESMF_SUCCESS
alog%fIndex = 1
call ESMF_UtilIOUnitFlush (alog%unitNumber, rc=localrc2)
if (localrc2 /= ESMF_SUCCESS) then
write (ESMF_UtilIOStderr,*) 'unit flush failed, rc =', localrc2
call ESMF_UtilIOUnitFlush(ESMF_UtilIOStderr, rc=rc)
localrc = localrc2
end if
alog%flushed = ESMF_TRUE
alog%dirty = ESMF_FALSE
endif
if (present (rc)) then
rc = localrc
end if
end subroutine ESMF_LogFlush