WAVEWATCH III  beta 0.0.1
scrip_errormod.f90
Go to the documentation of this file.
1 !||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 
4 
5  !BOP
6  ! !MODULE: SCRIP_ErrorMod
7  ! !DESCRIPTION:
8  ! This module contains SCRIP error flags and facilities for logging and
9  ! printing error messages. Note that error flags are local to a
10  ! process and there is no synchronization of error flags across
11  ! processes. As routines trap error flags, they may add a message
12  ! to the error log to aid in tracking the call sequence.
13  !
14  ! Users should not need to change any values in this module.
15  !
16  ! All routines in SCRIP which encounter an error should return to
17  ! the calling routine with the SCRIP\_Fail error code set and a message
18  ! added to the error log using the SCRIP\_ErrorCheck or
19  ! SCRIP\_ErrorSet function. Routines in SCRIP should also check
20  ! error codes returned by called routines and add a message to the
21  ! error log to help users track the calling sequence that generated
22  ! the error. This process enables the error code to be propagated
23  ! to the highest level or calling routine to enable a graceful
24  ! exit. At that level, the SCRIP_ErrorPrint call can be used to
25  ! print the entire error trace or error log.
26  !
27  ! !REVISION HISTORY:
28  ! SVN:$Id: SCRIP_ErrorMod.F90 14 2006-08-17 17:07:05Z $
29  !
30  ! !USES:
31 
32  use scrip_kindsmod
33  !use SCRIP_CommMod
35 
36  implicit none
37  private
38  save
39 
40  ! !DEFINED PARAMETERS:
41 
42  integer (SCRIP_i4), parameter, public :: &
43  scrip_success = 0, & ! standard SCRIP error flags
44  scrip_fail = -1
45 
46  ! !PUBLIC MEMBER FUNCTIONS:
47 
48  public :: scrip_errorset, &
51 
52  !EOP
53  !BOC
54  !-----------------------------------------------------------------------
55  !
56  ! module variables
57  !
58  !-----------------------------------------------------------------------
59 
60  integer (SCRIP_i4), parameter :: &
61  scrip_errorlogdepth = 20 ! Max depth of call tree to properly
62  ! size the error log array
63 
64  integer (SCRIP_i4) :: &
65  scrip_errormsgcount = 0 ! tracks current number of log messages
66 
67  character (SCRIP_CharLength), dimension(SCRIP_ErrorLogDepth) :: &
68  scrip_errorlog ! list of error messages to be output
69 
70  !EOC
71  !***********************************************************************
72 
73 contains
74 
75  !***********************************************************************
76  !BOP
77  ! !IROUTINE: SCRIP_ErrorSet -- sets error code and logs error message
78  ! !INTERFACE:
79 
80  subroutine scrip_errorset(errorCode, rtnName, errorMsg)
81 
82  ! !DESCRIPTION:
83  ! This routine sets an error code to SCRIP\_Fail and adds a message to
84  ! the error log for later printing.
85  !
86  ! !REVISION HISTORY:
87  ! same as module
88 
89  ! !OUTPUT PARAMETERS:
90 
91  integer (SCRIP_i4), intent(out) :: &
92  errorcode ! Error code to set to fail
93 
94  ! !INPUT PARAMETERS:
95 
96  character (*), intent(in) :: &
97  rtnname, &! name of calling routine
98  errormsg ! message to add to error log for printing
99 
100  !EOP
101  !BOC
102  !-----------------------------------------------------------------------
103  !
104  ! Local variables
105  !
106  !-----------------------------------------------------------------------
107 
108  character(SCRIP_charLength) :: &
109  logerrormsg ! constructed error message with routine name
110 
111  !-----------------------------------------------------------------------
112  !
113  ! Set error code to fail
114  !
115  !-----------------------------------------------------------------------
116 
117  errorcode = scrip_fail
118 
119  !-----------------------------------------------------------------------
120  !
121  ! Add error message to error log
122  !
123  !-----------------------------------------------------------------------
124 
125  scrip_errormsgcount = scrip_errormsgcount + 1
126 
127  if (scrip_errormsgcount <= scrip_errorlogdepth) then
128  write(logerrormsg,'(a,a2,a)') rtnname,': ',errormsg
129  scrip_errorlog(scrip_errormsgcount) = logerrormsg
130  endif
131 
132  !-----------------------------------------------------------------------
133  !EOC
134 
135  end subroutine scrip_errorset
136 
137  !***********************************************************************
138  !BOP
139  ! !IROUTINE: SCRIP_ErrorCheck -- checks error code and logs error message
140  ! !INTERFACE:
141 
142  function scrip_errorcheck(errorCode, rtnName, errorMsg)
143 
144  ! !DESCRIPTION:
145  ! This function checks an error code and adds a message to the error
146  ! log for later printing. It is a more compact form of the ErrorSet
147  ! routine that is especially useful for checking an error code after
148  ! returning from a routine or function. If the errorCode is the
149  ! failure code SCRIP\_Fail, it returns a logical true value so that
150  ! it can be used in a typical call like:
151  ! \begin{verbatim}
152  ! if (SCRIP_ErrorCheck(errorCode, rtnName, errorMsg)) return
153  ! \end{verbatim}
154  !
155  ! !REVISION HISTORY:
156  ! same as module
157 
158  ! !OUTPUT PARAMETERS:
159 
160  logical (SCRIP_logical) :: &
162 
163  ! !INPUT PARAMETERS:
164 
165  integer (SCRIP_i4), intent(in) :: &
166  errorcode ! Error code to check
167 
168  character (*), intent(in) :: &
169  rtnname, &! name of calling routine
170  errormsg ! message to add to error log for printing
171 
172  !EOP
173  !BOC
174  !-----------------------------------------------------------------------
175  !
176  ! local variables
177  !
178  !-----------------------------------------------------------------------
179 
180  character (SCRIP_charLength) :: &
181  logerrormsg ! constructed error message with routine name
182 
183  !-----------------------------------------------------------------------
184  !
185  ! If the error code is success, set the return value to false.
186  !
187  !-----------------------------------------------------------------------
188 
189  if (errorcode == scrip_success) then
190  scrip_errorcheck = .false.
191 
192  !-----------------------------------------------------------------------
193  !
194  ! If the error code is a fail, set the return value to true and
195  ! add the error message to the log.
196  !
197  !-----------------------------------------------------------------------
198 
199  else
200  scrip_errorcheck = .true.
201 
202  scrip_errormsgcount = scrip_errormsgcount + 1
203 
204  if (scrip_errormsgcount <= scrip_errorlogdepth) then
205  write(logerrormsg,'(a,a2,a)') rtnname,': ',errormsg
206  scrip_errorlog(scrip_errormsgcount) = logerrormsg
207  endif
208  endif
209 
210  !-----------------------------------------------------------------------
211  !EOC
212 
213  end function scrip_errorcheck
214 
215  !***********************************************************************
216  !BOP
217  ! !IROUTINE: SCRIP_ErrorPrint -- prints the error log
218  ! !INTERFACE:
219 
220  subroutine scrip_errorprint(printTask)
221 
222  ! !DESCRIPTION:
223  ! This routine prints all messages in the error log. If a printTask
224  ! is specified, only the message log on that task will be printed.
225  !
226  ! !REVISION HISTORY:
227  ! same as module
228 
229  ! !INPUT PARAMETERS:
230 
231  ! integer (SCRIP_i4), intent(in) :: &
232  ! errorCode ! input error code to check success/fail
233 
234  !*** currently this has no meaning, but will be used in parallel
235  !*** SCRIP version
236  integer (SCRIP_i4), intent(in), optional :: &
237  printtask ! Task from which to print error log
238 
239  !EOP
240  !BOC
241  !-----------------------------------------------------------------------
242  !
243  ! local variables
244  !
245  !-----------------------------------------------------------------------
246 
247  integer (SCRIP_i4) :: n
248 
249  !-----------------------------------------------------------------------
250  !
251  ! Print all error messages to stdout
252  !
253  !-----------------------------------------------------------------------
254 
255  if (present(printtask)) then
256 
257  !*** parallel SCRIP not yet supported
258  !if (SCRIP_myTask == printTask) then
259 
263 
264  if (scrip_errormsgcount == 0) then ! no errors
265 
266  write(scrip_stdout,'(a34)') &
267  'Successful completion of SCRIP model'
268 
269  else
270 
271  write(scrip_stdout,'(a14)') 'SCRIP Exiting...'
272 
273  do n=1,min(scrip_errormsgcount,scrip_errorlogdepth)
274  write(scrip_stderr,'(a)') trim(scrip_errorlog(n))
275  if (scrip_stdout /= scrip_stderr) then
276  write(scrip_stdout,'(a)') trim(scrip_errorlog(n))
277  endif
278  end do
279 
280  if (scrip_errormsgcount > scrip_errorlogdepth) then
281  write(scrip_stderr,'(a23)') 'Too many error messages'
282  if (scrip_stdout /= scrip_stderr) then
283  write(scrip_stdout,'(a23)') 'Too many error messages'
284  endif
285  endif
286 
287  endif
288 
292 
293  !endif
294 
295  else
296 
300 
301  if (scrip_errormsgcount == 0) then ! no errors
302 
303  write(scrip_stdout,'(a34)') 'Successful completion of SCRIP'
304 
305  else
306 
307  write(scrip_stdout,'(a14)') 'SCRIP Exiting...'
308 
309  do n=1,min(scrip_errormsgcount,scrip_errorlogdepth)
310  write(scrip_stderr,'(a)') trim(scrip_errorlog(n))
311  if (scrip_stdout /= scrip_stderr) then
312  write(scrip_stdout,'(a)') trim(scrip_errorlog(n))
313  endif
314  end do
315 
316  if (scrip_errormsgcount > scrip_errorlogdepth) then
317  write(scrip_stderr,'(a23)') 'Too many error messages'
318  if (scrip_stdout /= scrip_stderr) then
319  write(scrip_stdout,'(a23)') 'Too many error messages'
320  endif
321  endif
322 
323  endif
324 
328 
329  endif
330 
331  !-----------------------------------------------------------------------
332  !EOC
333 
334  end subroutine scrip_errorprint
335 
336  !***********************************************************************
337 
338 end module scrip_errormod
339 
340 !|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
scrip_errormod::scrip_errorprint
subroutine, public scrip_errorprint(printTask)
Definition: scrip_errormod.f90:221
scrip_iounitsmod::scrip_stderr
integer(scrip_i4), parameter, public scrip_stderr
Definition: scrip_iounitsmod.f90:47
scrip_iounitsmod::scrip_delimformat
character(9), parameter, public scrip_delimformat
Definition: scrip_iounitsmod.f90:54
scrip_errormod::scrip_errorset
subroutine, public scrip_errorset(errorCode, rtnName, errorMsg)
Definition: scrip_errormod.f90:81
scrip_errormod::scrip_success
integer(scrip_i4), parameter, public scrip_success
Definition: scrip_errormod.f90:42
scrip_kindsmod
Definition: scrip_kindsmod.f90:3
scrip_errormod
Definition: scrip_errormod.f90:3
scrip_errormod::scrip_fail
integer(scrip_i4), parameter, public scrip_fail
Definition: scrip_errormod.f90:42
scrip_iounitsmod
Definition: scrip_iounitsmod.f90:3
scrip_iounitsmod::scrip_blankformat
character(5), parameter, public scrip_blankformat
Definition: scrip_iounitsmod.f90:57
scrip_iounitsmod::scrip_stdout
integer(scrip_i4), parameter, public scrip_stdout
Definition: scrip_iounitsmod.f90:47
scrip_errormod::scrip_errorcheck
logical(scrip_logical) function, public scrip_errorcheck(errorCode, rtnName, errorMsg)
Definition: scrip_errormod.f90:143