Skip to content

Commit

Permalink
Implement ESMF_FieldLog() and use it in ESMF_StateLog(). Formatting c…
Browse files Browse the repository at this point in the history
…onsistency

across classes when logging a valid object.
  • Loading branch information
theurich committed Jul 24, 2024
1 parent 07b59b1 commit 1aad8e4
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/Infrastructure/Array/src/ESMCI_Array.C
Original file line number Diff line number Diff line change
Expand Up @@ -4185,8 +4185,7 @@ void Array::log(
ESMC_LogDefault.Write(msg.str(), msgType);
}else{
msg.str(""); // clear
msg << prefix << "Array object is valid!"
<< " <name: " << getName() << ">";
msg << prefix << " <name: " << getName() << ">";
ESMC_LogDefault.Write(msg.str(), msgType);
if (deepFlag) getDistGrid()->log(prefix+"! ", msgType, deepFlag);
}
Expand Down
146 changes: 144 additions & 2 deletions src/Infrastructure/Field/src/ESMF_FieldHalo.F90
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ module ESMF_FieldHaloMod
use ESMF_VMMod
use ESMF_DELayoutMod
use ESMF_RHandleMod

use ESMF_UtilMod

implicit none

!------------------------------------------------------------------------------
Expand All @@ -64,6 +65,7 @@ module ESMF_FieldHaloMod
public ESMF_FieldHaloRelease
public ESMF_FieldHaloStore
public ESMF_FieldIsCreated ! Check if a Field object is created
public ESMF_FieldLog

!EOPI
!------------------------------------------------------------------------------
Expand Down Expand Up @@ -423,12 +425,152 @@ function ESMF_FieldIsCreated(field, keywordEnforcer, rc)
! \end{description}
!
!EOP
!-----------------------------------------------------------------------------
!------------------------------------------------------------------------------
ESMF_FieldIsCreated = .false. ! initialize
if (present(rc)) rc = ESMF_SUCCESS
if (ESMF_FieldGetInit(field)==ESMF_INIT_CREATED) &
ESMF_FieldIsCreated = .true.
end function
!------------------------------------------------------------------------------


!------------------------------------------------------------------------------
#undef ESMF_METHOD
#define ESMF_METHOD "ESMF_FieldLog()"
!BOP
! !IROUTINE: ESMF_FieldLog - Log Field information

! !INTERFACE:
subroutine ESMF_FieldLog(field, keywordEnforcer, prefix, logMsgFlag, deepFlag, rc)
!
! !ARGUMENTS:
type(ESMF_Field), intent(in) :: field
type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
character(len=*), intent(in), optional :: prefix
type(ESMF_LogMsg_Flag), intent(in), optional :: logMsgFlag
logical, intent(in), optional :: deepFlag
integer, intent(out), optional :: rc
!
! !DESCRIPTION:
! Write information about {\tt field} to the ESMF default Log.
!
! The arguments are:
! \begin{description}
! \item[field]
! The {\tt ESMF\_Field} object logged.
! \item [{[prefix]}]
! String to prefix the log message. Default is no prefix.
! \item [{[logMsgFlag]}]
! Type of log message generated. See section \ref{const:logmsgflag} for
! a list of valid message types. Default is {\tt ESMF\_LOGMSG\_INFO}.
! \item[{[deepFlag]}]
! When set to {\tt .false.} (default), only log top level information about
! the Field.
! When set to {\tt .true.}, additionally log deep information.
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
!EOP
!------------------------------------------------------------------------------
integer :: localrc ! local return code
type(ESMF_LogMsg_Flag) :: logMsg
character(len=:), allocatable :: prefixStr
logical :: deepLog
type(ESMF_FieldStatus_Flag) :: fieldStatus
type(ESMF_TypeKind_Flag) :: typekind
integer :: rank, dimCount
character(ESMF_MAXSTR) :: name, tempString
character(800) :: msgString

! initialize return code; assume routine not implemented
localrc = ESMF_RC_NOT_IMPL
if (present(rc)) rc = ESMF_RC_NOT_IMPL

! optional prefix
if (present(prefix)) then
prefixStr = trim(prefix)
else
prefixStr = ""
endif

! deal with optional logMsgFlag
logMsg = ESMF_LOGMSG_INFO ! default
if (present(logMsgFlag)) logMsg = logMsgFlag

! deal with optional deepFlag
deepLog = .false. ! default
if (present(deepFlag)) deepLog = deepFlag

call ESMF_LogWrite(ESMF_StringConcat(trim(prefixStr), &
"--- FieldLog() start -----------------"), logMsg, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return

if (.not. ESMF_FieldIsCreated(field)) then
call ESMF_LogWrite(ESMF_StringConcat(prefix, &
"Field object is invalid! Not created or deleted!"), &
logMsg, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
else
! query
call ESMF_FieldGet(field, name=name, status=fieldStatus, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return

if (fieldStatus==ESMF_FIELDSTATUS_EMPTY) then
tempString = "ESMF_FIELDSTATUS_EMPTY"
else if (fieldStatus==ESMF_FIELDSTATUS_GRIDSET) then
tempString = "ESMF_FIELDSTATUS_GRIDSET"
else if (fieldStatus==ESMF_FIELDSTATUS_COMPLETE) then
tempString = "ESMF_FIELDSTATUS_COMPLETE"
else
tempString = "Out or range FIELDSTATUS!!!"
endif

write (msgString,'(A,A,A,A,A,A)') &
prefix, "<name: ", trim(name), "> <fieldStatus: ", trim(tempString), ">"
call ESMF_LogWrite(trim(msgString), logMsg, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return

if (fieldStatus == ESMF_FIELDSTATUS_COMPLETE) then
call ESMF_FieldGet(field, typekind=typekind, rank=rank, &
dimCount=dimCount, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return

call ESMF_TypeKindString(typekind, string=tempString, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return

write (msgString,'(A,A,A,A,A,I4,A,A,I4,A)') &
prefix, "<typekind: ", trim(tempString), ">", &
" <rank: ", rank, ">", " <dimCount: ", dimCount, ">"
call ESMF_LogWrite(trim(msgString), logMsg, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
endif
endif

call ESMF_LogWrite(ESMF_StringConcat(trim(prefixStr), &
"--- FieldLog() end -------------------"), logMsg, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return

! return successfully
if (present(rc)) rc = ESMF_SUCCESS

end subroutine ESMF_FieldLog
!------------------------------------------------------------------------------

end module ESMF_FieldHaloMod
14 changes: 7 additions & 7 deletions src/Superstructure/State/src/ESMF_StateAPI.cppF90
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module ESMF_StateAPIMod
use ESMF_FieldMod
use ESMF_FieldGetMod
use ESMF_FieldCreateMod
use ESMF_FieldHaloMod
use ESMF_FieldBundleMod
use ESMF_RHandleMod
use ESMF_StateTypesMod
Expand Down Expand Up @@ -1889,7 +1890,7 @@ type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
deepLog = .false. ! default
if (present(deepFlag)) deepLog = deepFlag

! deal with optionl logMsgFlag
! deal with optional logMsgFlag
logMsg = ESMF_LOGMSG_INFO ! default
if (present(logMsgFlag)) logMsg = logMsgFlag

Expand Down Expand Up @@ -1964,9 +1965,8 @@ type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
tempString = "Out or range STATEINTENT!!!"
endif

write (msgString,'(A,A,A,A,A,A,A,A,I4,A)') &
prefix, "State object is valid!", &
" <name: ", trim(name), "> <intent: ", trim(tempString), ">", &
write (msgString,'(A,A,A,A,A,A,A,I4,A)') &
prefix, "<name: ", trim(name), "> <intent: ", trim(tempString), ">", &
" <itemCount: ", itemCount, ">"
call ESMF_LogWrite(trim(msgString), logMsg, rc=localrc)
if (ESMF_LogFoundError(localrc, &
Expand Down Expand Up @@ -2015,9 +2015,9 @@ type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! call ESMF_FieldLog(field, &
! prefix=ESMF_StringConcat(prefix, "! "), &
! logMsgFlag=logMsg, rc=localrc)
call ESMF_FieldLog(field, &
prefix=ESMF_StringConcat(prefix, "! "), &
logMsgFlag=logMsg, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
Expand Down

0 comments on commit 1aad8e4

Please sign in to comment.