9 integer, parameter :: IO_NODE = 0
10 integer :: my_proc_id = 0
14 integer, parameter :: QUIET=-100, LOGFILE=-2, DEBUG=0, INFORM=1, WARN=2, ERROR=3, STDOUT=100
16 integer :: the_debug_level = DEBUG
18 logical :: have_set_logname = .false.
20 logical :: continuing_line_logfile = .false.
21 logical :: continuing_line_debug = .false.
22 logical :: continuing_line_inform = .false.
23 logical :: continuing_line_warn = .false.
24 logical :: continuing_line_error = .false.
25 logical :: continuing_line_stdout = .false.
30 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
31 ! Name: set_debug_level
33 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
34 subroutine set_debug_level(ilev)
39 integer, intent(in) :: ilev
41 the_debug_level = ilev
43 end subroutine set_debug_level
46 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
49 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
50 subroutine mprintf(assertion, level, fmtstring, &
52 i1, i2, i3, i4, i5, i6, &
53 f1, f2, f3, f4, f5, f6, &
54 s1, s2, s3, s4, s5, s6, &
55 l1, l2, l3, l4, l5, l6)
60 integer, intent(in) :: level
61 logical, intent(in) :: assertion
62 character (len=*), intent(in) :: fmtstring
63 logical, intent(in), optional :: newline
64 integer, intent(in), optional :: i1, i2, i3, i4, i5, i6
65 real, intent(in), optional :: f1, f2, f3, f4, f5, f6
66 logical, intent(in), optional :: l1, l2, l3, l4, l5, l6
67 character (len=*), intent(in), optional :: s1, s2, s3, s4, s5, s6
70 integer :: idxi, idxf, idxs, idxl, istart, i, iend, ia
72 logical :: continuing_line, la
73 character (len=8) :: cur_date
74 character (len=10) :: cur_time
75 character (len=10) :: print_date
76 character (len=12) :: print_time
77 !BUG: sa should be as long as the largest string length used anywhere in WPS
78 character (len=1024) :: sa
79 character (len=1024) :: ctemp
81 if (.not. have_set_logname) then
82 write(ctemp,'(a)') 'logfile.log'
83 call cio_set_log_filename(ctemp,len_trim(ctemp))
86 write(ctemp,'(a)') 'geogrid.log'
87 call cio_set_log_filename(ctemp,len_trim(ctemp))
89 write(ctemp,'(a,i4.4)') 'geogrid.log.',my_proc_id
90 call cio_set_log_filename(ctemp,len_trim(ctemp))
95 write(ctemp,'(a)') 'metgrid.log'
96 call cio_set_log_filename(ctemp,len_trim(ctemp))
98 write(ctemp,'(a,i4.4)') 'metgrid.log.',my_proc_id
99 call cio_set_log_filename(ctemp,len_trim(ctemp))
103 write(ctemp,'(a)') 'ungrib.log'
104 call cio_set_log_filename(ctemp,len_trim(ctemp))
106 have_set_logname = .true.
114 iend = len_trim(fmtstring)
116 #if (defined _GEOGRID) || (defined _METGRID)
117 if (assertion .and. (.not. (level == STDOUT .and. my_proc_id /= IO_NODE))) then
122 ! If this is a debug message give up if level is not high enough
123 if (level == DEBUG .and. the_debug_level > DEBUG) return
125 if (level /= STDOUT) then
126 call date_and_time(date=cur_date,time=cur_time)
129 if (level == LOGFILE .and. .not.continuing_line_logfile) then
130 write(print_date,'(a10)') cur_date(1:4)//'-'//cur_date(5:6)//'-'//cur_date(7:8)
131 write(print_time,'(a12)') cur_time(1:2)//':'//cur_time(3:4)//':'//cur_time(5:10)
132 write(ctemp,'(a)') print_date//' '//print_time//' --- '
133 call cio_prints(1,ctemp,len(print_date//' '//print_time//' --- '))
134 else if (level == DEBUG .and. .not.continuing_line_debug) then
135 write(print_date,'(a10)') cur_date(1:4)//'-'//cur_date(5:6)//'-'//cur_date(7:8)
136 write(print_time,'(a12)') cur_time(1:2)//':'//cur_time(3:4)//':'//cur_time(5:10)
137 write(ctemp,'(a)') print_date//' '//print_time//' --- '
138 call cio_prints(1,ctemp,len(print_date//' '//print_time//' --- '))
139 write(ctemp,'(a)') 'DEBUG: '
140 call cio_prints(1,ctemp,7)
141 else if (level == INFORM .and. .not.continuing_line_inform) then
142 write(print_date,'(a10)') cur_date(1:4)//'-'//cur_date(5:6)//'-'//cur_date(7:8)
143 write(print_time,'(a12)') cur_time(1:2)//':'//cur_time(3:4)//':'//cur_time(5:10)
144 write(ctemp,'(a)') print_date//' '//print_time//' --- '
145 call cio_prints(1,ctemp,len(print_date//' '//print_time//' --- '))
146 write(ctemp,'(a)') 'INFORM: '
147 if (level >= the_debug_level) &
148 call cio_prints(0,ctemp,8)
149 call cio_prints(1,ctemp,8)
150 else if (level == WARN .and. .not.continuing_line_warn) then
151 write(print_date,'(a10)') cur_date(1:4)//'-'//cur_date(5:6)//'-'//cur_date(7:8)
152 write(print_time,'(a12)') cur_time(1:2)//':'//cur_time(3:4)//':'//cur_time(5:10)
153 write(ctemp,'(a)') print_date//' '//print_time//' --- '
154 call cio_prints(1,ctemp,len(print_date//' '//print_time//' --- '))
155 write(ctemp,'(a)') 'WARNING: '
156 if (level >= the_debug_level) &
157 call cio_prints(0,ctemp,9)
158 call cio_prints(1,ctemp,9)
159 else if (level == ERROR .and. .not.continuing_line_error) then
160 write(print_date,'(a10)') cur_date(1:4)//'-'//cur_date(5:6)//'-'//cur_date(7:8)
161 write(print_time,'(a12)') cur_time(1:2)//':'//cur_time(3:4)//':'//cur_time(5:10)
162 write(ctemp,'(a)') print_date//' '//print_time//' --- '
163 call cio_prints(1,ctemp,len(print_date//' '//print_time//' --- '))
164 write(ctemp,'(a)') 'ERROR: '
165 if (level >= the_debug_level) &
166 call cio_prints(0,ctemp,7)
167 call cio_prints(1,ctemp,7)
170 i = index(fmtstring(istart:iend),'%')
171 do while (i > 0 .and. i < iend)
173 write(ctemp,'(a)') fmtstring(istart:i-1)
174 if (level >= the_debug_level .and. level /= DEBUG) &
175 call cio_prints(0,ctemp,i-istart)
176 if (level /= STDOUT) &
177 call cio_prints(1,ctemp,i-istart)
179 if (fmtstring(i+1:i+1) == '%') then
180 write(ctemp,'(a)') '%'
181 if (level >= the_debug_level .and. level /= DEBUG) &
182 call cio_prints(0,ctemp,1)
183 if (level /= STDOUT) &
184 call cio_prints(1,ctemp,1)
186 else if (fmtstring(i+1:i+1) == 'i') then
187 if (idxi == 1 .and. present(i1)) then
189 else if (idxi == 2 .and. present(i2)) then
191 else if (idxi == 3 .and. present(i3)) then
193 else if (idxi == 4 .and. present(i4)) then
195 else if (idxi == 5 .and. present(i5)) then
197 else if (idxi == 6 .and. present(i6)) then
201 if (level >= the_debug_level .and. level /= DEBUG) &
202 call cio_printi(0,ia)
203 if (level /= STDOUT) &
204 call cio_printi(1,ia)
208 else if (fmtstring(i+1:i+1) == 'f') then
209 if (idxf == 1 .and. present(f1)) then
211 else if (idxf == 2 .and. present(f2)) then
213 else if (idxf == 3 .and. present(f3)) then
215 else if (idxf == 4 .and. present(f4)) then
217 else if (idxf == 5 .and. present(f5)) then
219 else if (idxf == 6 .and. present(f6)) then
223 if (level >= the_debug_level .and. level /= DEBUG) &
224 call cio_printf(0,fa)
225 if (level /= STDOUT) &
226 call cio_printf(1,fa)
230 else if (fmtstring(i+1:i+1) == 's') then
231 if (idxs == 1 .and. present(s1)) then
233 else if (idxs == 2 .and. present(s2)) then
235 else if (idxs == 3 .and. present(s3)) then
237 else if (idxs == 4 .and. present(s4)) then
239 else if (idxs == 5 .and. present(s5)) then
241 else if (idxs == 6 .and. present(s6)) then
245 write(ctemp,'(a)') trim(sa)
246 if (level >= the_debug_level .and. level /= DEBUG) &
247 call cio_prints(0,ctemp,len_trim(ctemp))
248 if (level /= STDOUT) &
249 call cio_prints(1,ctemp,len_trim(ctemp))
252 else if (fmtstring(i+1:i+1) == 'l') then
253 if (idxl == 1 .and. present(l1)) then
255 else if (idxl == 2 .and. present(l2)) then
257 else if (idxl == 3 .and. present(l3)) then
259 else if (idxl == 4 .and. present(l4)) then
261 else if (idxl == 5 .and. present(l5)) then
263 else if (idxl == 6 .and. present(l6)) then
268 write(ctemp,'(a)') '.TRUE.'
270 write(ctemp,'(a)') '.FALSE.'
272 if (level >= the_debug_level .and. level /= DEBUG) &
273 call cio_prints(0,ctemp,len_trim(ctemp))
274 if (level /= STDOUT) &
275 call cio_prints(1,ctemp,len_trim(ctemp))
281 i = index(fmtstring(istart:iend),'%')
284 continuing_line = .false.
285 if (present(newline)) then
286 if (.not.newline) then
287 continuing_line = .true.
291 if (continuing_line) then
292 write(ctemp,'(a)') fmtstring(istart:iend)
294 write(ctemp,'(a)') fmtstring(istart:iend)//achar(10) ! Add newline character 0xA
297 if (level == LOGFILE) then
298 continuing_line_logfile = continuing_line
299 else if (level == DEBUG) then
300 continuing_line_debug = continuing_line
301 else if (level == INFORM) then
302 continuing_line_inform = continuing_line
303 else if (level == WARN) then
304 continuing_line_warn = continuing_line
305 else if (level == ERROR) then
306 continuing_line_error = continuing_line
307 else if (level == STDOUT) then
308 continuing_line_stdout = continuing_line
311 if (level >= the_debug_level .and. level /= DEBUG) &
312 call cio_prints(0,ctemp,iend-istart+2)
313 if (level /= STDOUT) &
314 call cio_prints(1,ctemp,iend-istart+2)
316 if (level == ERROR) then
318 call parallel_abort()
321 call parallel_abort()
329 end subroutine mprintf
331 end module module_debug