Skip to content

Commit

Permalink
Updated handling of log level arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
interkosmos committed Mar 28, 2024
1 parent be6ef62 commit 7ec7759
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
4 changes: 2 additions & 2 deletions guide/guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ The following log levels are accepted:

| `--error _n_` | `-e` | 0 | DMPACK <<error-codes,error code>> (optional).
| `--help` | `-h` | – | Output available command-line arguments and quit.
| `--level _level_` | `-L` | 2 | <<data-log-level,Log level>>, from 1 to 5.
| `--level _level_` | `-L` | 2 | <<data-log-level,Log level>>, from 1 to 5 (level or name).
| `--logger _name_` | `-l` | `dmlogger` | Name of logger instance and POSIX message queue.
| `--message _string_` | `-m` | – | Log message (max. 512 characters).
| `--node _id_` | `-N` | – | Node id (optional).
Expand All @@ -1332,7 +1332,7 @@ The following log levels are accepted:
Send a log message to the message queue of logger `dmlogger`:

....
$ dmlog --level 3 --message "low battery" --source dmlog --verbose
$ dmlog --level warning --message "low battery" --source dmlog --verbose
2022-12-09T22:50:44.161000+01:00 [WARNING] dmlog - low battery
....

Expand Down
17 changes: 15 additions & 2 deletions src/dm_arg.f90
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ integer function dm_arg_read(args, app, major, minor, patch) result(rc)
end function dm_arg_read

integer function dm_arg_validate(arg) result(rc)
!! Validates given argument.
!! Validates given argument. Arguments of type `ARG_TYPE_LEVEL` are
!! additionally converted to integer if the passed argument value is a
!! valid log level name. For example, the argument value `warning` is
!! converted to integer `3`, to match log level `LVL_WARNING`.
use :: dm_id
use :: dm_log
use :: dm_string
Expand Down Expand Up @@ -399,8 +402,18 @@ integer function dm_arg_validate(arg) result(rc)

case (ARG_TYPE_LEVEL)
if (arg%length == 0) return

! Convert string to integer.
call dm_string_to(arg%value, level, error)
if (dm_is_error(error)) level = dm_log_level_from_name(trim(arg%value))

! On error, try to read level from level name, and convert the
! result back to string. An invalid log level name is turned
! into `LVL_NONE`.
if (dm_is_error(error)) then
level = dm_log_level_from_name(arg%value)
arg%value = dm_itoa(level)
end if

if (.not. dm_log_valid(level)) return

case (ARG_TYPE_FILE, ARG_TYPE_DB)
Expand Down
13 changes: 10 additions & 3 deletions src/dm_log.f90
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ module dm_log
integer, parameter, public :: LOG_SOURCE_LEN = ID_LEN !! Max. log source length.
integer, parameter, public :: LOG_MESSAGE_LEN = 512 !! Max. log message length.

integer, parameter, public :: LOG_LEVEL_NAME_LEN = 8

character(len=*), parameter, public :: LOG_LEVEL_NAMES(0:LVL_LAST) = [ &
character(len=8) :: 'NONE', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL' &
character(len=LOG_LEVEL_NAME_LEN) :: 'NONE', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL' &
] !! Log level strings.

character(len=*), parameter, public :: LOG_LEVEL_NAMES_LOWER(0:LVL_LAST) = [ &
character(len=8) :: 'none', 'debug', 'info', 'warning', 'error', 'critical' &
character(len=LOG_LEVEL_NAME_LEN) :: 'none', 'debug', 'info', 'warning', 'error', 'critical' &
] !! Log level strings in lower-case.

type, public :: log_type
Expand Down Expand Up @@ -107,7 +109,12 @@ pure elemental integer function dm_log_level_from_name(name) result(level)

character(len=*), intent(in) :: name !! Log level name.

select case (dm_lower(name))
character(len=LOG_LEVEL_NAME_LEN) :: name_

! Normalise name.
name_ = dm_lower(name)

select case (name_)
case (LOG_LEVEL_NAMES_LOWER(LVL_NONE))
level = LVL_NONE
case (LOG_LEVEL_NAMES_LOWER(LVL_DEBUG))
Expand Down
46 changes: 28 additions & 18 deletions src/dm_string.f90
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pure subroutine string_from_int32(i, str, error)
character(len=:), allocatable, intent(out) :: str !! Output.
integer, intent(out), optional :: error !! Error code.

integer :: n, rc
integer :: n, stat

if (present(error)) error = E_FORMAT

Expand All @@ -279,7 +279,8 @@ pure subroutine string_from_int32(i, str, error)
end if

allocate (character(len=n) :: str)
write (str, '(i0)', iostat=rc) i
write (str, '(i0)', iostat=stat) i
if (stat /= 0) return

if (present(error)) error = E_NONE
end subroutine string_from_int32
Expand All @@ -290,7 +291,7 @@ pure subroutine string_from_int64(i, str, error)
character(len=:), allocatable, intent(out) :: str !! Output.
integer, intent(out), optional :: error !! Error code.

integer :: n, rc
integer :: n, stat

if (present(error)) error = E_FORMAT

Expand All @@ -302,7 +303,8 @@ pure subroutine string_from_int64(i, str, error)
end if

allocate (character(len=n) :: str)
write (str, '(i0)', iostat=rc) i
write (str, '(i0)', iostat=stat) i
if (stat /= 0) return

if (present(error)) error = E_NONE
end subroutine string_from_int64
Expand All @@ -313,13 +315,13 @@ pure subroutine string_from_real32(f, str, error)
character(len=:), allocatable, intent(out) :: str !! Output.
integer, intent(out), optional :: error !! Error code.

integer :: rc
integer :: stat
character(len=20) :: buf

str = ''
if (present(error)) error = E_FORMAT
write (buf, '(f0.12)', iostat=rc) f
if (rc /= 0) return
write (buf, '(f0.12)', iostat=stat) f
if (stat /= 0) return
str = trim(buf)
if (present(error)) error = E_NONE
end subroutine string_from_real32
Expand All @@ -330,13 +332,13 @@ pure subroutine string_from_real64(f, str, error)
character(len=:), allocatable, intent(out) :: str !! Output.
integer, intent(out), optional :: error !! Error code.

integer :: rc
integer :: stat
character(len=20) :: buf

str = ''
if (present(error)) error = E_FORMAT
write (buf, '(f0.12)', iostat=rc) f
if (rc /= 0) return
write (buf, '(f0.12)', iostat=stat) f
if (stat /= 0) return
str = trim(buf)
if (present(error)) error = E_NONE
end subroutine string_from_real64
Expand All @@ -346,11 +348,13 @@ pure elemental subroutine string_to_int32(str, i, error)
character(len=*), intent(in) :: str !! Input.
integer(kind=i4), intent(out) :: i !! Output.
integer, intent(out), optional :: error !! error code.
integer :: rc

integer :: stat

i = 0_i4
if (present(error)) error = E_TYPE
read (str, *, iostat=rc) i
read (str, *, iostat=stat) i
if (stat /= 0) return
if (present(error)) error = E_NONE
end subroutine string_to_int32

Expand All @@ -359,11 +363,13 @@ pure elemental subroutine string_to_int64(str, i, error)
character(len=*), intent(in) :: str !! Input.
integer(kind=i8), intent(out) :: i !! Output.
integer, intent(out), optional :: error !! error code.
integer :: rc

integer :: stat

i = 0_i8
if (present(error)) error = E_TYPE
read (str, *, iostat=rc) i
read (str, *, iostat=stat) i
if (stat /= 0) return
if (present(error)) error = E_NONE
end subroutine string_to_int64

Expand All @@ -372,11 +378,13 @@ pure elemental subroutine string_to_real32(str, f, error)
character(len=*), intent(in) :: str !! Input.
real(kind=r4), intent(out) :: f !! Output.
integer, intent(out), optional :: error !! error code.
integer :: rc

integer :: stat

f = 0.0_r4
if (present(error)) error = E_TYPE
read (str, *, iostat=rc) f
read (str, *, iostat=stat) f
if (stat /= 0) return
if (present(error)) error = E_NONE
end subroutine string_to_real32

Expand All @@ -385,11 +393,13 @@ pure elemental subroutine string_to_real64(str, f, error)
character(len=*), intent(in) :: str !! Input.
real(kind=r8), intent(out) :: f !! Output.
integer, intent(out), optional :: error !! error code.
integer :: rc

integer :: stat

f = 0.0_r8
if (present(error)) error = E_TYPE
read (str, *, iostat=rc) f
read (str, *, iostat=stat) f
if (stat /= 0) return
if (present(error)) error = E_NONE
end subroutine string_to_real64
end module dm_string

0 comments on commit 7ec7759

Please sign in to comment.