Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | level | |||
character(len=*), | intent(in) | :: | tag | |||
character(len=*), | intent(in) | :: | message | |||
logical, | intent(in), | optional | :: | colour | ||
logical, | intent(in), | optional | :: | datetime |
function log_format_message(level, tag, message, colour, datetime) result(msg)
! Format log message with tag, message and optionally coloured level
! The level for which to get the designator
integer, intent(in) :: level
character(len=*), intent(in) :: tag
character(len=*), intent(in) :: message
! Whether to colourise the level tag, should only be done if the designator is
! printed to the terminal. Defaults to `.true.`.
! Whether to add datetime stamp at beginning, defaults to .true.
logical, intent(in), optional :: colour, datetime
character(len=23+len(tag)+2+21+len(message)) :: msg
character(len=21) :: levelstr, dt
character(len=len(tag)+2) :: tagb
integer :: descriminator
logical :: col
if (present(colour)) then
col = colour
else
col = .true.
end if
descriminator = level/10
if (descriminator < 1) then
levelstr = ''
else if (descriminator < 2) then
levelstr = '<DEBUG>'
if (col) levelstr = colourise(levelstr, 'cyan')
else if (descriminator < 3) then
levelstr = '<INFO>'
if (col) levelstr = colourise(levelstr, 'blue')
else if (descriminator < 4) then
levelstr = '<WARNING>'
if (col) levelstr = colourise(levelstr, 'yellow')
else if (descriminator < 5) then
levelstr = '<ERROR>'
if (col) levelstr = colourise(levelstr, 'red')
end if
dt = ''
if (present(datetime)) then
if (datetime) dt = date_time_str()
else
dt = "["//date_time_str()//"]"
end if
tagb = tag
if (len_trim(tag) > 0) tagb = '['//tag//']'
! put together
write(msg, "(3a,(1x,a))") trim(levelstr), trim(dt), trim(tagb), trim(message)
end function log_format_message