MOM6
MOM_error_handler.F90
Go to the documentation of this file.
2 !********+*********+*********+*********+*********+*********+*********+**
3 !* *
4 !* This file is a part of MOM. See MOM.F90 for licensing. *
5 !* *
6 !* By R. Hallberg, 2005-2012. *
7 !* *
8 !* This module wraps the mpp_mod error handling code and the *
9 !* mpp functions stdlog() and stdout() that return open unit numbers. *
10 !* *
11 !********+*********+*********+*********+*********+*********+*********+**
12 
13 use mpp_mod, only : mpp_error, note, warning, fatal
14 use mpp_mod, only : mpp_pe, mpp_root_pe, stdlog, stdout
15 
16 implicit none ; private
17 
18 public mom_error, mom_mesg, note, warning, fatal, is_root_pe, stdlog, stdout
21 public assert
22 
23 ! Verbosity level:
24 ! 0 - FATAL messages only
25 ! 1 - FATAL + WARNING messages only
26 ! 2 - FATAL + WARNING + NOTE messages only [default]
27 ! 3 - above + informational
28 ! 4 -
29 ! 5 -
30 ! 6 - above + call tree
31 ! 7 -
32 ! 8 -
33 ! 9 - anything and everything (also set with #define DEBUG)
34 integer :: verbosity = 6
35 ! Note that this module default will only hold until the
36 ! VERBOSITY parameter is parsed and the given default imposed.
37 ! We set it to 6 here so that the call tree will print before
38 ! the parser has been initialized
39 ! Also note that this is a module variable rather than contained in
40 ! a type passed by argument (preferred for most data) for convenience
41 ! and to reduce obfuscation of code
42 
43 ! The level of calling within the call tree
44 integer :: calltreeindentlevel = 0
45 
46 contains
47 
48 function is_root_pe()
49  ! This returns .true. if the current PE is the root PE.
50  logical :: is_root_pe
51  is_root_pe = .false.
52  if (mpp_pe() == mpp_root_pe()) is_root_pe = .true.
53  return
54 end function is_root_pe
55 
56 subroutine mom_mesg(message, verb, all_print)
57  character(len=*), intent(in) :: message
58  integer, optional, intent(in) :: verb
59  logical, optional, intent(in) :: all_print
60  ! This provides a convenient interface for writing an informative comment.
61  integer :: verb_msg
62  logical :: write_msg
63 
64  write_msg = is_root_pe()
65  if (present(all_print)) write_msg = write_msg .or. all_print
66 
67  verb_msg = 2 ; if (present(verb)) verb_msg = verb
68  if (write_msg .and. (verbosity >= verb_msg)) call mpp_error(note, message)
69 
70 end subroutine mom_mesg
71 
72 subroutine mom_error(level, message, all_print)
73  integer, intent(in) :: level
74  character(len=*), intent(in) :: message
75  logical, optional, intent(in) :: all_print
76  ! This provides a convenient interface for writing an mpp_error message
77  ! with run-time filter based on a verbosity.
78  logical :: write_msg
79 
80  write_msg = is_root_pe()
81  if (present(all_print)) write_msg = write_msg .or. all_print
82 
83  select case (level)
84  case (note)
85  if (write_msg.and.verbosity>=2) call mpp_error(note, message)
86  case (warning)
87  if (write_msg.and.verbosity>=1) call mpp_error(warning, message)
88  case (fatal)
89  if (verbosity>=0) call mpp_error(fatal, message)
90  case default
91  call mpp_error(level, message)
92  end select
93 end subroutine mom_error
94 
95 subroutine mom_set_verbosity(verb)
96  integer, intent(in) :: verb
97  character(len=80) :: msg
98  if (verb>0 .and. verb<10) then
99  verbosity=verb
100  else
101  write(msg(1:80),'("Attempt to set verbosity outside of range (0-9). verb=",I0)') verb
102  call mom_error(fatal,msg)
103  endif
104 end subroutine mom_set_verbosity
105 
106 function mom_get_verbosity()
107  integer :: MOM_get_verbosity
108  mom_get_verbosity = verbosity
109 end function mom_get_verbosity
110 
111 function mom_verbose_enough(verb)
112  integer, intent(in) :: verb
113  logical :: MOM_verbose_enough
114  mom_verbose_enough = (verbosity >= verb)
115 end function mom_verbose_enough
116 
117 !> Returns True, if the verbosity>=6 indicating to show the call tree
118 function calltree_showquery()
119  ! Local variables
120  logical :: callTree_showQuery
121  calltree_showquery = (verbosity >= 6)
122 end function calltree_showquery
123 
124 !> Writes a message about entering a subroutine if call tree reporting is active
125 subroutine calltree_enter(mesg,n)
126  character(len=*) :: mesg !< Message to write
127  integer, optional :: n !< An optional integer to write at end of message
128  ! Local variables
129  character(len=8) :: nAsString
131  if (verbosity<6) return
132  if (is_root_pe()) then
133  nasstring = ''
134  if (present(n)) then
135  write(nasstring(1:8),'(i8)') n
136  call mpp_error(note, 'callTree: '// &
137  repeat(' ',calltreeindentlevel-1)//'loop '//trim(mesg)//trim(nasstring))
138  else
139  call mpp_error(note, 'callTree: '// &
140  repeat(' ',calltreeindentlevel-1)//'---> '//trim(mesg))
141  endif
142  endif
143 end subroutine calltree_enter
144 
145 !> Writes a message about leaving a subroutine if call tree reporting is active
146 subroutine calltree_leave(mesg)
147  character(len=*) :: mesg !< Message to write
148  if (calltreeindentlevel<1) write(0,*) 'callTree_leave: error callTreeIndentLevel=',calltreeindentlevel,trim(mesg)
150  if (verbosity<6) return
151  if (is_root_pe()) call mpp_error(note, 'callTree: '// &
152  repeat(' ',calltreeindentlevel)//'<--- '//trim(mesg))
153 end subroutine calltree_leave
154 
155 !> Writes a message about reaching a milestone if call tree reporting is active
156 subroutine calltree_waypoint(mesg,n)
157  character(len=*) :: mesg !< Message to write
158  integer, optional :: n !< An optional integer to write at end of message
159  ! Local variables
160  character(len=8) :: nAsString
161  if (calltreeindentlevel<0) write(0,*) 'callTree_waypoint: error callTreeIndentLevel=',calltreeindentlevel,trim(mesg)
162  if (verbosity<6) return
163  if (is_root_pe()) then
164  nasstring = ''
165  if (present(n)) then
166  write(nasstring(1:8),'(i8)') n
167  call mpp_error(note, 'callTree: '// &
168  repeat(' ',calltreeindentlevel)//'loop '//trim(mesg)//trim(nasstring))
169  else
170  call mpp_error(note, 'callTree: '// &
171  repeat(' ',calltreeindentlevel)//'o '//trim(mesg))
172  endif
173  endif
174 end subroutine calltree_waypoint
175 
176 !> Issues a FATAL error if the assertion fails, i.e. the first argument is false.
177 subroutine assert(logical_arg, msg)
178  logical, intent(in) :: logical_arg !< If false causes a FATAL error
179  character(len=*), intent(in) :: msg !< Message to issue in case of failed assertion
180 
181  if (.not. logical_arg) then
182  call mom_error(fatal, msg)
183  endif
184 
185 end subroutine assert
186 
187 end module mom_error_handler
integer function, public mom_get_verbosity()
logical function, public mom_verbose_enough(verb)
subroutine, public calltree_leave(mesg)
Writes a message about leaving a subroutine if call tree reporting is active.
subroutine, public calltree_waypoint(mesg, n)
Writes a message about reaching a milestone if call tree reporting is active.
logical function, public is_root_pe()
subroutine, public assert(logical_arg, msg)
Issues a FATAL error if the assertion fails, i.e. the first argument is false.
subroutine, public mom_set_verbosity(verb)
subroutine, public mom_mesg(message, verb, all_print)
logical function, public calltree_showquery()
Returns True, if the verbosity>=6 indicating to show the call tree.
subroutine, public mom_error(level, message, all_print)
subroutine, public calltree_enter(mesg, n)
Writes a message about entering a subroutine if call tree reporting is active.