FUTILS
Some basic Fortran Utilities
Loading...
Searching...
No Matches
error_handling.f90
Go to the documentation of this file.
2 use strings_m
3
4 !This module goes hand-in-hand with the macros defined in error_handling.h
5
6
7 !> @page error_handling_page Error Handling
8 !> @ref index "< Back to main page"
9 !>
10 !> The @ref error_handling_m module provides error handling routines bound to a static
11 !> type, @ref error_handling_m::error_handling "error_handling".
12 !>
13 !>
14 !> @n
15 !> # @ref error_handling_m::error_handling_t::assert_routine "error_handling%assert_routine"
16 !>
17 !> @copydoc error_handling_m::error_handling_assert_routine
18 !>
19 !> @note
20 !> The `futils.h` header file provides a macro to automatically insert the file name
21 !> and line number where the assert routine was called from:
22 !> @code{.fortran}
23 !> use futils_m
24 !> #include 'futils.h'
25 !> call error_handling%assert(x > 0 .and. x <= 10)
26 !> @endcode
27 !> This is the intended use of the assert routine. It will stop the program if the condition
28 !> is not satisfied, and print a message including the condition in character form.
29 !>
30 !>
31 !>
32 !> @n
33 !> # @ref error_handling_m::error_handling_t::abort_routine "error_handling%abort_routine"
34 !>
35 !> @copydoc error_handling_m::error_handling_abort_routine
36 !>
37 !> @note
38 !> The `futils.h` header file provides a macro to automatically insert the file name
39 !> and line number where the abort routine was called from:
40 !> @code{.fortran}
41 !> use futils_m
42 !> #include 'futils.h'
43 !> call error_handling%abort('Error message')
44 !> @endcode
45 !> This is the intended use of the abort routine. One should only call the underlying
46 !> `error_handling%%abort_routine` in special cases. One such special case is when you
47 !> want to specify the line number and file name of an input file to inform the user of
48 !> invalid input.
49
50
51 private
52 public :: error_handling
53
54 type, private :: error_handling_t
55 contains
56 procedure, nopass :: abort_routine => error_handling_abort_routine
59 end type error_handling_t
60
62
63contains
64
65 !> @brief
66 !> Causes an ERROR STOP, printing an error message, as well as the file name and
67 !> line number where the error occurred.
68 !>
69 !> @details
70 !> The abort routine should be kept for situations where the
71 !> @ref error_handling_m::error_handling_assert_routine "assert" routine is not appropriate.
72 pure subroutine error_handling_abort_routine(line,file,msg)
73 implicit none
74
75 !-=-= INPUT/OUTPUT
76 integer, intent(in) :: line !< (integer) : Line number where the error occurred
77 character(len=*), intent(in) :: file !< (character) : Name of file where error occurred
78 character(len=*), intent(in) :: msg !< (character) : Error message
79 type(string_t) :: file_s, line_s, msg_s
80 character*12, parameter :: newline = new_line('a')//repeat(' ',11)
81 integer :: n, i
82
83 file_s = trim(adjustl(file))
84 line_s = line
85
86 n = len_trim(file) + 7
87
88 msg_s = repeat('-',n)
89 msg_s = msg_s//newline//'FILE: '//file_s
90 msg_s = msg_s//newline//'LINE: '//line_s
91 msg_s = msg_s//newline//'MSG:: '//trim(adjustl(msg))
92 msg_s = msg_s//newline//repeat('-',n)
93
94 error stop msg_s%s
95
96 end subroutine error_handling_abort_routine
97
98 pure subroutine error_handling_assert_array_routine(condition,condition_str,line,file)
99 implicit none
100
101 !-=-= INPUT/OUTPUT
102 logical, dimension(:), intent(in) :: condition !< (logical(:)) : Array of logical conditions to assert
103 character(len=*), intent(in) :: condition_str !< (character) : The conditions as a string
104 integer, intent(in) :: line !< (integer) : Line number where the assertion is made
105 character(len=*), intent(in) :: file !< (character) : Name of file where the assertion is made
106
107 if(.not.all(condition)) call error_handling_abort_routine(line,file,"ASSERTION FAILED: "//trim(adjustl(condition_str)))
108
110
111
112 !> @brief
113 !> Causes an ERROR STOP, printing an error message, as well as the file name and
114 !> line number where the error occurred.
115 !>
116 !> @details
117 !> The assert routine should be used for internal validity checks only. It is a safety
118 !> mechanism to ensure that program bugs are found and fixed, with the idea that the code
119 !> developer uses the file name and line number to inspect the source location where the
120 !> assertion failure occurred and determine the cause.
121 !> If you want to ERROR STOP as a result of invalid/improper input parameters, use the
122 !> @ref error_handling_m::error_handling_abort_routine "abort" routine, with a useful
123 !> error message.
124 pure subroutine error_handling_assert_routine(condition,condition_str,line,file)
125 implicit none
126
127 !-=-= INPUT/OUTPUT
128 logical, intent(in) :: condition !< (logical) : Logical condition to assert
129 character(len=*), intent(in) :: condition_str !< (character) : The condition as a string
130 integer, intent(in) :: line !< (integer) : Line number where the assertion is made
131 character(len=*), intent(in) :: file !< (character) : Name of file where the assertion is made
132
133 if(.not.condition) call error_handling_abort_routine(line,file,"ASSERTION FAILED: "//trim(adjustl(condition_str)))
134
135 end subroutine error_handling_assert_routine
136
137
138end module error_handling_m
type(error_handling_t), parameter, public error_handling
pure subroutine error_handling_abort_routine(line, file, msg)
Causes an ERROR STOP, printing an error message, as well as the file name and line number where the e...
pure subroutine error_handling_assert_array_routine(condition, condition_str, line, file)
pure subroutine error_handling_assert_routine(condition, condition_str, line, file)
Causes an ERROR STOP, printing an error message, as well as the file name and line number where the e...