subroutine ESMF_LogInitialize(filename, logappendflag, logkindflag, rc)
!
! !ARGUMENTS:
      character(len=*),        intent(in)           :: filename
      logical,                 intent(in), optional :: logappendflag
      type(ESMF_LogKind_Flag), intent(in), optional :: logkindflag
      integer,                 intent(out),optional :: rc
! !DESCRIPTION:
!      This routine initializes the global default {\tt ESMF\_Log}.
!      The default {\tt ESMF\_Log} is assigned the {\tt filename} and
!      is associated with an open Fortran unit number.
!
!      The arguments are:
!      \begin{description}
!
!      \item [filename]
!            Name of file.  Maximum length 58 characters to allow for
!            the PET number to be added and keep the total file name
!            length under 64 characters.
!     \item [{[logappendflag]}]
!           If the log file already exists, a value of {\tt .false.}
!           will set the file position to the beginning of the file.  A value
!           of [\tt .true.} sets the position to the end of the file.
!           If not specified, defaults to {\tt .true.}.
!      \item [{[logkindflag]}]
!            Specifies {\tt ESMF\_LOGKIND\_SINGLE}, {\tt ESMF\_LOGKIND\_MULTI} or
!            {\tt ESMF\_LOGKIND\_NONE}.
!      \item [{[rc]}]
!            Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
!      \end{description}
!
!EOPI
    integer                        :: localrc
    integer                        :: msgAbortCnt
    type(ESMF_LogMsg_Flag)         :: msgAbortLst(6)
    character(ESMF_MAXSTR)         :: envRtAbort
    integer                        :: idxRtAbort
    ! Initialize return code; assume routine not implemented
    if (present(rc)) rc = ESMF_RC_NOT_IMPL
    call ESMF_LogOpen(ESMF_LogDefault, filename,  &
      appendflag=logappendflag, logkindflag=logkindflag, rc=localrc)
    if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
      ESMF_CONTEXT, rcToReturn=rc)) return
    call c_ESMC_VMGetEnv("ESMF_RUNTIME_ABORT_LOGMSG_TYPES", envRtAbort, localrc)
    if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
      ESMF_CONTEXT, rcToReturn=rc)) return
    msgAbortCnt = 0
    idxRtAbort = index (envRtAbort, "ESMF_LOGMSG_DEBUG")
    if (idxRtAbort > 0) then
      msgAbortCnt = msgAbortCnt + 1
      msgAbortLst(msgAbortCnt) = ESMF_LOGMSG_DEBUG
    endif
    idxRtAbort = index (envRtAbort, "ESMF_LOGMSG_ERROR")
    if (idxRtAbort > 0) then
      msgAbortCnt = msgAbortCnt + 1
      msgAbortLst(msgAbortCnt) = ESMF_LOGMSG_ERROR
    endif
    idxRtAbort = index (envRtAbort, "ESMF_LOGMSG_INFO")
    if (idxRtAbort > 0) then
      msgAbortCnt = msgAbortCnt + 1
      msgAbortLst(msgAbortCnt) = ESMF_LOGMSG_INFO
    endif
    idxRtAbort = index (envRtAbort, "ESMF_LOGMSG_JSON")
    if (idxRtAbort > 0) then
      msgAbortCnt = msgAbortCnt + 1
      msgAbortLst(msgAbortCnt) = ESMF_LOGMSG_JSON
    endif
    idxRtAbort = index (envRtAbort, "ESMF_LOGMSG_TRACE")
    if (idxRtAbort > 0) then
      msgAbortCnt = msgAbortCnt + 1
      msgAbortLst(msgAbortCnt) = ESMF_LOGMSG_TRACE
    endif
    idxRtAbort = index (envRtAbort, "ESMF_LOGMSG_WARNING")
    if (idxRtAbort > 0) then
      msgAbortCnt = msgAbortCnt + 1
      msgAbortLst(msgAbortCnt) = ESMF_LOGMSG_WARNING
    endif
    if (msgAbortCnt > 6) then
      call ESMF_LogSetError(ESMF_RC_BUFFER_SHORT, &
        msg="msgAbortLst overflow - ESMF_RUNTIME_ABORT_LOGMSG_TYPES", &
        ESMF_CONTEXT, rcToReturn=rc)
      return
    elseif (msgAbortCnt > 0) then
      call ESMF_LogSet(ESMF_LogDefault, &
        logmsgAbort=msgAbortLst(1:msgAbortCnt), rc=localrc)
      if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
        ESMF_CONTEXT, rcToReturn=rc)) return
    endif
    if (present(rc)) rc=ESMF_SUCCESS
end subroutine ESMF_LogInitialize