Initialize a logging data type.
: this should move to a proper place and have a proper format!
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(tem_logging_type), | intent(out) | :: | me |
Logger to initialize |
||
integer, | intent(in) | :: | level |
Level of output to log with this logger |
||
integer, | intent(in) | :: | rank |
Rank of the process executing the initialization. |
||
character(len=*), | intent(in), | optional | :: | filename |
File to write output to, default is null device. If this is empty or not provided, the output will be to the null device. To send messages to the stdout set this parameter to '/stdout:'. |
|
logical, | intent(in), | optional | :: | root_only |
Indicate if root only should write messages Default is true. |
|
character(len=*), | intent(in), | optional | :: | real_form |
Format to write real numbers. Default is 'EN12.3' |
|
character(len=*), | intent(in), | optional | :: | int_form |
Format to write integer numbers. Default is 'I0' |
subroutine tem_logging_init_logger( me, level, rank, filename, root_only, &
& real_form, int_form )
! ---------------------------------------------------------------------------
!> Logger to initialize
type(tem_logging_type), intent(out) :: me
!> Level of output to log with this logger
integer, intent(in) :: level
!> Rank of the process executing the initialization.
integer, intent(in) :: rank
!> File to write output to, default is null device.
!!
!! If this is empty or not provided, the output will be to the
!! null device.
!! To send messages to the stdout set this parameter to
!! '/stdout:'.
character(len=*), optional, intent(in) :: filename
!> Indicate if root only should write messages
!! Default is true.
logical, optional, intent(in) :: root_only
!> Format to write real numbers.
!! Default is 'EN12.3'
character(len=*), optional, intent(in) :: real_form
!> Format to write integer numbers.
!! Default is 'I0'
character(len=*), optional, intent(in) :: int_form
! ---------------------------------------------------------------------------
logical :: root_out
character(len=7) :: rankstamp
character(len=pathLen) :: fname
logical :: nUnitOpened
integer :: UnitNumber
logical :: file_exists
! ---------------------------------------------------------------------------
me%log_level = level
if (present(root_only)) then
root_out = root_only
else
root_out = .true.
end if
if (root_out) then
me%participating = (rank == 0)
else
me%participating = .true.
end if
if (present(filename)) then
if (trim(filename) == '/stdout:') then
! Only root should write to stdout.
root_out = .true.
if (rank == 0) then
fname = filename
me%participating = .true.
else
fname = ''
me%participating = .false.
end if
else
fname = trim(filename)
end if
else
fname = ''
end if
if (me%participating .and. (trim(adjustl(fname)) /= '')) then
! If I am participating and the filename is not actually empty,
! proceed connecting to appropriate external files.
if (trim(adjustl(fname)) == '/stdout:') then
! Messages should be written to the standard output.
me%funit(0) = stdOutUnit
else
! Messages should be written to some file.
if (root_out) then
rankstamp = ''
else
write(rankstamp, '(a1,I6.6)') '.', rank
end if
! changes for dynamic load balancing
! check if the file exists
inquire( file = trim(fname)//trim(rankstamp), &
& exist = file_exists )
if( file_exists )then
! in case the file exists, check wether it is already opened somewhere
! else (dyn load balancing)
inquire( file = trim(fname)//trim(rankstamp), &
& opened = nUnitOpened, &
& number = UnitNumber )
if (nUNitOpened) then
me%funit(0) = UnitNumber
else
me%funit(0) = newunit()
open( unit = me%funit(0), &
& file = trim(fname)//trim(rankstamp), &
& position = 'APPEND', &
& status = 'OLD' )
end if
else
me%funit(0) = newunit()
open( unit = me%funit(0), &
& file = trim(fname)//trim(rankstamp), &
& status = 'REPLACE' )
end if
end if
else
! Output should be deactivated for this logger.
! Connect its unit to the null device.
call tem_connect_toNull(me%funit(0))
end if
! Always connect the last logging unit to the null device.
call tem_connect_toNull(me%funit(tem_last_lu))
! Set all units according to the configured logging level:
me%funit(1:level) = me%funit(0)
me%funit(level+1:tem_last_lu-1) = me%funit(tem_last_lu)
if (present(real_form)) then
me%real_form = real_form
else
me%real_form = 'EN12.3'
end if
if (present(int_form)) then
me%int_form = int_form
else
me%int_form = 'I0'
end if
!! @todo: this should move to a proper place and have a proper format!
!if (present(root_only)) then
! write(me%fUnit(0),*) 'rank= ', rank, 'filename= ', trim(filename), &
! & 'root_only= ', root_only
!end if
!write(me%fUnit(0),*) rank, root_out, 'log unit ', me%fUnit
end subroutine tem_logging_init_logger