ESMF_LogOpen Subroutine

public subroutine ESMF_LogOpen(log, filename, keywordEnforcer, appendFlag, logkindFlag, noPrefix, rc)

Arguments

Type IntentOptional Attributes Name
type(ESMF_Log), intent(inout) :: log
character(len=*), intent(in) :: filename
type(ESMF_KeywordEnforcer), optional :: keywordEnforcer
logical, intent(in), optional :: appendFlag
type(ESMF_LogKind_Flag), intent(in), optional :: logkindFlag
logical, intent(in), optional :: noPrefix
integer, intent(out), optional :: rc

Source Code

    subroutine ESMF_LogOpen(log, filename, keywordEnforcer,  &
        appendflag, logkindflag, noPrefix, rc)
!
! !ARGUMENTS:
    type(ESMF_Log),          intent(inout)         :: log
    character(len=*),        intent(in)            :: filename
type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
    logical,                 intent(in),  optional :: appendFlag
    type(ESMF_LogKind_Flag), intent(in),  optional :: logkindFlag
    logical,                 intent(in),  optional :: noPrefix
    integer,                 intent(out), optional :: rc

!
! !DESCRIPTION:
!      This routine opens a file named {\tt filename} and associates
!      it with the {\tt ESMF\_Log}.  When {\tt logkindflag} is set to
!      {\tt ESMF\_LOGKIND\_MULTI} or {\tt ESMF\_LOGKIND\_MULTI\_ON\_ERROR}
!      the file name is prepended with PET number identification.  If the
!      incoming log is already open, an error is returned.
!
!      The arguments are:
!      \begin{description}
!      \item [log]
!            An {\tt ESMF\_Log} object.
!      \item [filename]
!            Name of log file to be opened.
!      \item [{[appendFlag]}]
!            If the log file exists, setting to {\tt .false.} will set the file position
!            to the beginning of the file.  Otherwise, new records will be appended to the
!            end of the file.  If not specified, defaults to {\tt .true.}.
!      \item [{[logkindFlag]}]
!            Set the logkindflag. See section \ref{const:logkindflag} for a list of
!            valid options.  When the {\tt ESMF\_LOGKIND\_MULTI\_ON\_ERROR} is selected,
!            the log opening is deferred until a {\tt ESMF\_LogWrite} with log message of
!            type {\tt ESMF\_LOGMSG\_ERROR} is written.
!            If not specified, defaults to {\tt ESMF\_LOGKIND\_MULTI}.
!      \item [{[noPrefix]}]
!            Set the noPrefix flag.  If set to {\tt .false.}, log messages are prefixed
!            with time stamps, message type, and PET number.  If set to {\tt .true.} the
!            messages will be written without prefixes.  If not specified, defaults to
!            {\tt .false.}.
!      \item [{[rc]}]
!            Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
!      \end{description}
!
!EOP
    interface
      subroutine f_ESMF_VMGlobalGet(localPet, petCount)
        integer, intent(out), optional  :: localPet
        integer, intent(out), optional  :: petCount
      end subroutine f_ESMF_VMGlobalGet
    end interface

    integer :: localrc, rc2
    integer :: iostat, memstat
    integer                                                :: i
    type(ESMF_LogEntry), dimension(:), pointer             :: localbuf
    character(len=ESMF_MAXPATHLEN)                         :: fname
    character(ESMF_MAXSTR)                                 :: petNumChar
    character(8)                                           :: position
    integer                                                :: petCount
    integer                                                :: digits
    character(len=10)                                      :: formatString

    type(ESMF_LogPrivate),pointer     :: alog

    ESMF_INIT_CHECK_SET_SHALLOW(ESMF_LogGetInit,ESMF_LogInit,log)

    ! Initialize return code; assume routine not implemented
    if (present(rc)) then
        rc=ESMF_FAILURE
    endif

    if (log%logTableIndex > 0) then
      alog => ESMF_LogTable(log%logTableIndex)
    else
      ESMF_LogTableCount = ESMF_LogTableCount + 1   ! counting number of files
      log%logTableIndex = ESMF_LogTableCount                ! Assign log
      alog => ESMF_LogTable(log%logTableIndex)
    endif

    ESMF_INIT_CHECK_SET_SHALLOW(ESMF_LogPrivateGetInit,ESMF_LogPrivateInit,alog)

    alog%appendFlag = .true.
    if (present (appendflag)) then
      alog%appendFlag = appendflag
    end if

    ! Test if it is open or closed
    if (alog%FileIsOpen == ESMF_TRUE) then
      write (ESMF_UtilIOStderr,*) ESMF_METHOD,  &
          ": This ESMF_Log is already open with file '", &
          trim(ESMF_LogTable(log%logTableIndex)%nameLogErrFile), "'"
      call ESMF_UtilIOUnitFlush(ESMF_UtilIOStderr, rc=rc)
      if (present(rc)) then
        rc=ESMF_RC_FILE_OPEN
      endif
      return
    endif

    alog%maxElements = 10
    alog%fIndex = 1

    call f_ESMF_VMGlobalGet(alog%petNumber, petCount)
    ! Convert PET to contiguous character label
    if (petCount>1) then
      digits = int (log10(real(petCount-1))+1)
    else
      digits = 1
    endif
    write(formatString, "('(i',i1,'.',i1,')')") digits, digits
    write(petNumChar, formatString) alog%petNumber
    alog%petNumLabel = "PET" // trim(adjustl(petNumChar))

    alog%stopprogram = .false.
    alog%flushImmediately = ESMF_FALSE
    alog%flushed = ESMF_FALSE
    alog%dirty = ESMF_FALSE
    alog%FileIsOpen=ESMF_FALSE
    alog%logmsgAbort => null ()
    nullify(alog%errorMask)
    alog%errorMaskCount=0
    if (present(logkindflag)) then
      alog%logkindflag=logkindflag
    else
      alog%logkindflag=ESMF_LOGKIND_MULTI
    endif
    alog%traceFlag = .false.
    alog%highResTimestampFlag = .false.
    alog%indentCount = 0
    alog%noPrefix = .false.

  if(alog%logkindflag /= ESMF_LOGKIND_NONE) then

    if (alog%logkindflag == ESMF_LOGKIND_SINGLE) then
        if (len_trim (filename) > ESMF_MAXPATHLEN-4) then
            write (ESMF_UtilIOStderr,*) ESMF_METHOD,  &
                ": Filename exceeded", ESMF_MAXPATHLEN, " characters."
            call ESMF_UtilIOUnitFlush(ESMF_UtilIOStderr, rc=rc)
            if (present(rc)) then
                rc = ESMF_RC_LONG_STR
            endif
            return
        endif
        alog%nameLogErrFile=trim(filename)
    else
        if (len_trim (filename) > ESMF_MAXPATHLEN-4) then
            write (ESMF_UtilIOStderr,*) ESMF_METHOD,  &
                ": Filename exceeded", ESMF_MAXPATHLEN, " characters."
            call ESMF_UtilIOUnitFlush(ESMF_UtilIOStderr, rc=rc)
            if (present(rc)) then
                rc = ESMF_RC_LONG_STR
            endif
            return
        endif
        fname = trim(alog%petNumLabel) // "." // trim(filename)
        alog%nameLogErrFile=fname
    endif

    ! Actually open the file
    if(alog%logkindflag /= ESMF_LOGKIND_MULTI_ON_ERROR) then
      call ESMF_LogOpenFile (alog, rc=localrc)
      if (present(rc)) then
        rc=localrc
      endif
      alog%deferredOpenFlag = .false.
    else
      alog%deferredOpenFlag = .true.
    end if

  endif

  !TODO: this is really strange because every time ESMF_LogOpen() is called
  !TODO: the _default_ Log on the C side is initialized, odd, isn't it? *gjt*
  
  !TODO: an attempt to at least only call the initialize for default log...
  
  if (log%logTableIndex == ESMF_LogDefault%logTableIndex) then
    ! this is the default log
    call c_ESMC_LogInitialize(filename,alog%petNumber,alog%logkindflag,rc2)
    !TODO: this is so messed up, why is rc2 not looked at, or passed back???
  endif

  if (present(rc)) rc=ESMF_SUCCESS
  
end subroutine ESMF_LogOpen