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 
30  !-----------------------------------------------------------------------------
31  !
32  !++ Private parameters
33  !
34  integer, parameter :: log_none = 0
35  integer, parameter :: log_error = 1
36  integer, parameter :: log_warn = 2
37  integer, parameter :: log_info = 3
38  integer, parameter :: log_debug = 4
39 
40  integer, parameter :: stderr = 0
41  integer, parameter :: stdout = 6
42 
43  !-----------------------------------------------------------------------------
44  !
45  !++ Public parameters & variables
46  !
47  integer, public, parameter :: log_lmsg = 4096
48  integer, public :: log_fid = stdout
49 
50  !-----------------------------------------------------------------------------
51  !
52  !++ Private variables
53  !
54  integer, private :: log_ilevel = log_info
55 
56  logical, private :: log_master = .true.
57  logical, private :: log_opened = .false.
58 
59 contains
60 
61  subroutine loginit( &
62  fid_conf, & ! (in)
63  fid_log, & ! (in) optional
64  master & ! (in) optional
65  )
66  implicit none
67  integer, intent(in) :: fid_conf
68  integer, intent(in), optional :: fid_log
69  logical, intent(in), optional :: master
70 
71  character(len=5) :: LOG_LEVEL = 'I'
72  character(len=100) :: LOG_FILE = "LOG_"
73 
74  integer :: ierr
75 
76  namelist / param_dc_log / &
77  log_level, &
78  log_file
79  !-----------------------------------------------------------------------------
80 
81  if ( present(master) ) log_master = master
82 
83  call date_and_time(log_file(4:11), log_file(12:21))
84 
85  !--- read PARAM
86  rewind(fid_conf)
87  read(fid_conf,nml=param_dc_log,iostat=ierr)
88 
89  if ( ierr > 0 ) then
90  call log('E', 'xxx Not appropriate names in namelist PARAM_DC_LOG. Check!')
91  end if
92 
93  if ( present(fid_log) ) then
94  log_fid = fid_log
95  else
96  open( log_fid, & ! (out)
97  file = trim(log_file), & ! (in)
98  form = 'formatted', & ! (in)
99  iostat = ierr ) ! (in)
100  if ( ierr /= 0 ) then
101  call log('E', 'xxx File open error! :' // trim(log_file))
102  end if
103  log_opened = .true.
104  end if
105 
106  select case (trim(log_level))
107  case ('E', 'e', 'ERROR', 'error')
108  log_ilevel = log_error
109  case ('W', 'w', 'WARN', 'warn')
110  log_ilevel = log_warn
111  case ('I', 'i', 'INFO', 'info')
112  log_ilevel = log_info
113  case ('D', 'd', 'DEBUG', 'debug')
114  log_ilevel = log_debug
115  case default
116  call log('E', 'xxx LOG_LEVEL is invalid. Check!')
117  end select
118 
119  return
120  end subroutine loginit
121 
122  subroutine logfinalize
124  if ( log_opened ) close(log_fid)
125  log_opened = .false.
126 
127  end subroutine logfinalize
128 
129  subroutine log( &
130  type, & ! (in)
131  message & ! (in)
132  )
133  implicit none
134  character(len=*), intent(in) :: type
135  character(len=*), intent(in) :: message
136 
137  select case (trim(type))
138  case ('E', 'e')
139  if ( log_ilevel >= log_error ) call logput(message)
140  write(stderr,*) trim(message)
141  call abort
142  case ('W', 'w')
143  if ( log_ilevel >= log_warn ) call logput(message)
144  case ('I', 'i')
145  if ( log_ilevel >= log_info ) call logput(message)
146  case ('D', 'd')
147  if ( log_ilevel >= log_debug ) call logput(message)
148  case default
149  write(stderr,*) 'BUG: wrong log level'
150  call abort
151  end select
152 
153  return
154  end subroutine log
155 
156 ! private
157  subroutine logput( &
158  message & ! (in)
159  )
160  character(len=*) :: message
161 
162  if ( log_master ) write(log_fid, *) trim(message)
163 
164  return
165  end subroutine logput
166 
167 end module dc_log
integer, public log_fid
Definition: dc_log.f90:48
module DC_Log
Definition: dc_log.f90:14
integer, parameter, public log_lmsg
Definition: dc_log.f90:47
subroutine, public loginit(fid_conf, fid_log, master)
Definition: dc_log.f90:66
subroutine, public log(type, message)
Definition: dc_log.f90:133
subroutine, public logfinalize
Definition: dc_log.f90:123