subroutine log_write(fid, msg, i1, i2, int, real, ints, reals, advance, clear)
! Write msg to fid appending various numerical values
integer, intent(in) :: fid
character(len=*), intent(in) :: msg
! optional (indeces), single int/real or 1D arrays to append to message
integer, intent(in), optional :: i1, i2, int, ints(:)
real(dp), intent(in), optional :: real, reals(:)
! add line break (default true)
logical, intent(in), optional :: advance
! clear n characters before writing (default 0)
integer, intent(in), optional :: clear
integer i
! clear line if needed
if (present(clear)) then
if (clear > 0) write(fid, '(60a)', advance='no') (char(8), i=1, clear)
end if
write(fid, '(a)', advance='no') trim(msg)
if (present(i1) .and. .not. present(i2)) &
write(fid, '(1x,"(",a,")")', advance='no') trim(to_string(int=i1))
if (present(i2) .and. .not. present(i1)) &
write(fid, '(1x,"(",a,")")', advance='no') trim(to_string(int=i2))
if (present(i2) .and. present(i1)) &
write(fid, '(1x,"(",a,", ",a,")")', advance='no') &
trim(to_string(int=i1)), trim(to_string(int=i2))
if (present(int)) write(fid, '(1x,a)', advance='no') trim(to_string(int=int))
if (present(real)) write(fid, '(1x,a)', advance='no') trim(to_string(real=real))
if (present(ints)) write(fid, '(*(1x,a))', advance='no') &
(trim(to_string(int=ints(i))), i=1, size(ints))
if (present(reals)) write(fid, '(*(1x,a))', advance='no') &
(trim(to_string(real=reals(i))), i=1, size(reals))
! New line unless advance=False
if (present(advance)) then
if (advance) write(fid, '(a)')
else
write(fid, '(a)')
end if
end subroutine log_write