SCALE-RM
dc_log.f90
Go to the documentation of this file.
1 !-------------------------------------------------------------------------------
13 !-------------------------------------------------------------------------------
14 module dc_log
15  !-----------------------------------------------------------------------------
16  !
17  !++ used modules
18  !
19  !-----------------------------------------------------------------------------
20  implicit none
21  private
22  !-----------------------------------------------------------------------------
23  !
24  !++ Public procedure
25  !
26  public :: loginit
27  public :: logfinalize
28  public :: log
29  public :: log_nml
30 
31  !-----------------------------------------------------------------------------
32  !
33  !++ Private parameters
34  !
35  integer, parameter :: log_none = 0
36  integer, parameter :: log_error = 1
37  integer, parameter :: log_warn = 2
38  integer, parameter :: log_info = 3
39  integer, parameter :: log_debug = 4
40 
41  integer, parameter :: stderr = 0
42  integer, parameter :: stdout = 6
43 
44  !-----------------------------------------------------------------------------
45  !
46  !++ Public parameters & variables
47  !
48  integer, public, parameter :: log_lmsg = 4096
49  integer, public :: log_fid
50  integer, public :: log_fid_nml
51 
52  !-----------------------------------------------------------------------------
53  !
54  !++ Private variables
55  !
56  integer, private :: log_ilevel = log_info
57 
58  logical, private :: log_master
59 #if defined(PGI) || defined(SX)
60  logical, public :: log_master_nml
61 #else
62  logical, private :: log_master_nml
63 #endif
64  logical, private :: log_opened = .false.
65 
66 contains
67 
68  subroutine loginit( &
69  fid_conf, & ! (in)
70  fid_log, & ! (in) optional
71  master, & ! (in) optional
72  fid_nml, & ! (in) optional
73  master_nml & ! (in) optional
74  )
75  implicit none
76  integer, intent(in) :: fid_conf
77  integer, intent(in), optional :: fid_log
78  logical, intent(in), optional :: master
79  integer, intent(in), optional :: fid_nml
80  logical, intent(in), optional :: master_nml
81 
82  character(len=5) :: LOG_LEVEL = 'I'
83  character(len=100) :: LOG_FILE = "LOG_"
84 
85  integer :: ierr
86 
87  namelist / param_dc_log / &
88  log_level, &
89  log_file
90 
91  character(len=LOG_LMSG) :: message
92  !-----------------------------------------------------------------------------
93 
94  log_fid = stdout
95  log_fid_nml = stdout
96  log_master = .true.
97 
98  if ( present(master) ) log_master = master
99  log_master_nml = log_master
100  if ( present(master_nml) ) log_master_nml = master_nml
101 
102  call date_and_time(log_file(4:11), log_file(12:21))
103 
104  !--- read PARAM
105  if ( fid_conf > 0 ) then
106  rewind(fid_conf)
107  read(fid_conf,nml=param_dc_log,iostat=ierr)
108  if ( ierr > 0 ) then
109  call log('E', 'xxx Not appropriate names in namelist PARAM_DC_LOG. Check!')
110  end if
111  end if
112 
113  if ( present(fid_log) ) then
114  log_fid = fid_log
115  log_file = ''
116  else
117  open( log_fid, & ! (out)
118  file = trim(log_file), & ! (in)
119  form = 'formatted', & ! (in)
120  iostat = ierr ) ! (in)
121  if ( ierr /= 0 ) then
122  call log('E', 'xxx File open error! :' // trim(log_file))
123  end if
124  log_opened = .true.
125  end if
126 
128  if ( present(fid_nml) ) then
129  log_fid_nml = fid_nml
130  end if
131 
132 #if defined(PGI) || defined(SX)
133  if ( log_master_nml ) write(log_fid_nml,nml=param_dc_log)
134 #else
135  write(message,nml=param_dc_log)
136  call log_nml('I',message)
137 #endif
138 
139  select case (trim(log_level))
140  case ('E', 'e', 'ERROR', 'error')
141  log_ilevel = log_error
142  case ('W', 'w', 'WARN', 'warn')
143  log_ilevel = log_warn
144  case ('I', 'i', 'INFO', 'info')
145  log_ilevel = log_info
146  case ('D', 'd', 'DEBUG', 'debug')
147  log_ilevel = log_debug
148  case default
149  call log('E', 'xxx LOG_LEVEL is invalid. Check!')
150  end select
151 
152  return
153  end subroutine loginit
154 
155  subroutine logfinalize
157  if ( log_opened ) close(log_fid)
158  log_opened = .false.
159 
160  end subroutine logfinalize
161 
162  subroutine log( &
163  type, & ! (in)
164  message & ! (in)
165  )
166  implicit none
167  character(len=*), intent(in) :: type
168  character(len=*), intent(in) :: message
169 
170  select case (trim(type))
171  case ('E', 'e')
172  if ( log_ilevel >= log_error ) call logput(message)
173  write(stderr,*) trim(message)
174  call abort
175  case ('W', 'w')
176  if ( log_ilevel >= log_warn ) call logput(message)
177  case ('I', 'i')
178  if ( log_ilevel >= log_info ) call logput(message)
179  case ('D', 'd')
180  if ( log_ilevel >= log_debug ) call logput(message)
181  case default
182  write(stderr,*) 'BUG: wrong log level'
183  call abort
184  end select
185 
186  return
187  end subroutine log
188 
189 ! private
190  subroutine logput( &
191  message & ! (in)
192  )
193  character(len=*) :: message
194 
195  if ( log_master ) write(log_fid, *) trim(message)
196 
197  return
198  end subroutine logput
199 
200  subroutine log_nml( &
201  type, & ! (in)
202  message & ! (in)
203  )
204  implicit none
205  character(len=*), intent(in) :: type
206  character(len=*), intent(in) :: message
207 
208  select case (trim(type))
209  case ('E', 'e')
210  if ( log_ilevel >= log_error ) call logput_nml(message)
211  write(stderr,*) trim(message)
212  call abort
213  case ('W', 'w')
214  if ( log_ilevel >= log_warn ) call logput_nml(message)
215  case ('I', 'i')
216  if ( log_ilevel >= log_info ) call logput_nml(message)
217  case ('D', 'd')
218  if ( log_ilevel >= log_debug ) call logput_nml(message)
219  case default
220  write(stderr,*) 'BUG: wrong log level'
221  call abort
222  end select
223 
224  return
225  end subroutine log_nml
226 
227 ! private
228  subroutine logput_nml( &
229  message & ! (in)
230  )
231  character(len=*) :: message
232 
233  if ( log_master_nml ) write(log_fid_nml, *) trim(message)
234 
235  return
236  end subroutine logput_nml
237 
238 end module dc_log
integer, public log_fid
Definition: dc_log.f90:49
module DC_Log
Definition: dc_log.f90:14
integer, parameter, public log_lmsg
Definition: dc_log.f90:48
subroutine, public log_nml(type, message)
Definition: dc_log.f90:204
integer, public log_fid_nml
Definition: dc_log.f90:50
subroutine, public log(type, message)
Definition: dc_log.f90:166
subroutine, public logfinalize
Definition: dc_log.f90:156
subroutine, public loginit(fid_conf, fid_log, master, fid_nml, master_nml)
Definition: dc_log.f90:75