|
1 | 1 | module stdlib_experimental_error |
2 | | -use, intrinsic :: iso_fortran_env, only: stderr=>error_unit |
| 2 | +use, intrinsic :: iso_fortran_env, only: stderr => error_unit |
| 3 | +use stdlib_experimental_optval, only: optval |
3 | 4 | implicit none |
4 | 5 | private |
5 | 6 |
|
6 | 7 | interface ! f{08,18}estop.f90 |
7 | | -module subroutine error_stop(msg, code) |
8 | | -character(*), intent(in) :: msg |
9 | | -integer, intent(in), optional :: code |
10 | | -end subroutine error_stop |
| 8 | + module subroutine error_stop(msg, code) |
| 9 | + character(*), intent(in) :: msg |
| 10 | + integer, intent(in), optional :: code |
| 11 | + end subroutine error_stop |
11 | 12 | end interface |
12 | 13 |
|
13 | | -public :: assert, error_stop |
| 14 | +public :: check, error_stop |
14 | 15 |
|
15 | 16 | contains |
16 | 17 |
|
17 | | -subroutine assert(condition, code) |
18 | | -! If condition == .false., it aborts the program. |
19 | | -! |
20 | | -! Arguments |
21 | | -! --------- |
22 | | -! |
23 | | -logical, intent(in) :: condition |
24 | | -integer, intent(in), optional :: code |
25 | | -! |
26 | | -! Example |
27 | | -! ------- |
28 | | -! |
29 | | -! call assert(a == 5) |
30 | | - |
31 | | -if (.not. condition) call error_stop("Assert failed.", code) |
32 | | -end subroutine |
33 | | - |
34 | | -end module |
| 18 | +subroutine check(condition, msg, code, warn) |
| 19 | + |
| 20 | + ! Checks the value of a logical condition. If condition == .false. and: |
| 21 | + ! |
| 22 | + ! * No other arguments are provided, it stops the program with the default |
| 23 | + ! message and exit code 1; |
| 24 | + ! * msg is provided, it prints the value of msg; |
| 25 | + ! * code is provided, it stops the program with the given exit code; |
| 26 | + ! * warn is provided and .true., it doesn't stop the program and prints |
| 27 | + ! * the message. |
| 28 | + ! |
| 29 | + ! Arguments |
| 30 | + ! --------- |
| 31 | + |
| 32 | + logical, intent(in) :: condition |
| 33 | + character(*), intent(in), optional :: msg |
| 34 | + integer, intent(in), optional :: code |
| 35 | + logical, intent(in), optional :: warn |
| 36 | + character(*), parameter :: msg_default = 'Check failed.' |
| 37 | + |
| 38 | + ! Examples |
| 39 | + ! -------- |
| 40 | + ! |
| 41 | + ! ! If a /= 5, stops the program with exit code 1 |
| 42 | + ! ! and prints 'Check failed.' |
| 43 | + ! call check(a == 5) |
| 44 | + ! |
| 45 | + ! ! As above, but prints 'a == 5 failed.' |
| 46 | + ! call check(a == 5, msg='a == 5 failed.') |
| 47 | + ! |
| 48 | + ! ! As above, but doesn't stop the program. |
| 49 | + ! call check(a == 5, msg='a == 5 failed.', warn=.true.) |
| 50 | + ! |
| 51 | + ! ! As example #2, but stops the program with exit code 77 |
| 52 | + ! call check(a == 5, msg='a == 5 failed.', code=77) |
| 53 | + |
| 54 | + if (.not. condition) then |
| 55 | + if (optval(warn, .false.)) then |
| 56 | + write(stderr,*) optval(msg, msg_default) |
| 57 | + else |
| 58 | + call error_stop(optval(msg, msg_default), optval(code, 1)) |
| 59 | + end if |
| 60 | + end if |
| 61 | + |
| 62 | +end subroutine check |
| 63 | + |
| 64 | +end module stdlib_experimental_error |
0 commit comments