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  rewind(fid_conf)
106  read(fid_conf,nml=param_dc_log,iostat=ierr)
107 
108  if ( ierr > 0 ) then
109  call log('E', 'xxx Not appropriate names in namelist PARAM_DC_LOG. Check!')
110  end if
111 
112  if ( present(fid_log) ) then
113  log_fid = fid_log
114  log_file = ''
115  else
116  open( log_fid, & ! (out)
117  file = trim(log_file), & ! (in)
118  form = 'formatted', & ! (in)
119  iostat = ierr ) ! (in)
120  if ( ierr /= 0 ) then
121  call log('E', 'xxx File open error! :' // trim(log_file))
122  end if
123  log_opened = .true.
124  end if
125 
127  if ( present(fid_nml) ) then
128  log_fid_nml = fid_nml
129  end if
130 
131 #if defined(PGI) || defined(SX)
132  if ( log_master_nml ) write(log_fid_nml,nml=param_dc_log)
133 #else
134  write(message,nml=param_dc_log)
135  call log_nml('I',message)
136 #endif
137 
138  select case (trim(log_level))
139  case ('E', 'e', 'ERROR', 'error')
140  log_ilevel = log_error
141  case ('W', 'w', 'WARN', 'warn')
142  log_ilevel = log_warn
143  case ('I', 'i', 'INFO', 'info')
144  log_ilevel = log_info
145  case ('D', 'd', 'DEBUG', 'debug')
146  log_ilevel = log_debug
147  case default
148  call log('E', 'xxx LOG_LEVEL is invalid. Check!')
149  end select
150 
151  return
152  end subroutine loginit
153 
154  subroutine logfinalize
156  if ( log_opened ) close(log_fid)
157  log_opened = .false.
158 
159  end subroutine logfinalize
160 
161  subroutine log( &
162  type, & ! (in)
163  message & ! (in)
164  )
165  implicit none
166  character(len=*), intent(in) :: type
167  character(len=*), intent(in) :: message
168 
169  select case (trim(type))
170  case ('E', 'e')
171  if ( log_ilevel >= log_error ) call logput(message)
172  write(stderr,*) trim(message)
173  call abort
174  case ('W', 'w')
175  if ( log_ilevel >= log_warn ) call logput(message)
176  case ('I', 'i')
177  if ( log_ilevel >= log_info ) call logput(message)
178  case ('D', 'd')
179  if ( log_ilevel >= log_debug ) call logput(message)
180  case default
181  write(stderr,*) 'BUG: wrong log level'
182  call abort
183  end select
184 
185  return
186  end subroutine log
187 
188 ! private
189  subroutine logput( &
190  message & ! (in)
191  )
192  character(len=*) :: message
193 
194  if ( log_master ) write(log_fid, *) trim(message)
195 
196  return
197  end subroutine logput
198 
199  subroutine log_nml( &
200  type, & ! (in)
201  message & ! (in)
202  )
203  implicit none
204  character(len=*), intent(in) :: type
205  character(len=*), intent(in) :: message
206 
207  select case (trim(type))
208  case ('E', 'e')
209  if ( log_ilevel >= log_error ) call logput_nml(message)
210  write(stderr,*) trim(message)
211  call abort
212  case ('W', 'w')
213  if ( log_ilevel >= log_warn ) call logput_nml(message)
214  case ('I', 'i')
215  if ( log_ilevel >= log_info ) call logput_nml(message)
216  case ('D', 'd')
217  if ( log_ilevel >= log_debug ) call logput_nml(message)
218  case default
219  write(stderr,*) 'BUG: wrong log level'
220  call abort
221  end select
222 
223  return
224  end subroutine log_nml
225 
226 ! private
227  subroutine logput_nml( &
228  message & ! (in)
229  )
230  character(len=*) :: message
231 
232  if ( log_master_nml ) write(log_fid_nml, *) trim(message)
233 
234  return
235  end subroutine logput_nml
236 
237 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:203
integer, public log_fid_nml
Definition: dc_log.f90:50
subroutine, public log(type, message)
Definition: dc_log.f90:165
subroutine, public logfinalize
Definition: dc_log.f90:155
subroutine, public loginit(fid_conf, fid_log, master, fid_nml, master_nml)
Definition: dc_log.f90:75