MONC
test_logging.F90
Go to the documentation of this file.
1 ! Tests the logging_mod utility functions
3  use fruit, only : assert_equals, assert_not_equals, assert_true
7  implicit none
8 
9  contains
10 
11  ! Tests the logging_mod by sending messages into a string. It will run at different
12  ! levels to ensure that
13  ! messages are written iff the logging_mod level is appropriate
14  subroutine test_logging_level
15  character(len=30) :: outpt
16 
17  integer i, j, space
18 
19  do i=1,4
20  call log_set_logging_level(i)
21  call assert_equals(i, log_get_logging_level(), "Logging level has been set correctly")
22  do j=1,4
23  call log_log(j, str(j)//" level", outpt)
24  if (j .le. i) then
25  space = index(outpt, "] ")
26  call assert_true(space .gt. 0, "The logging_mod message terminates")
27  outpt = outpt(space+2 : len(outpt))
28  call assert_equals(str(j)//" level", outpt, &
29  "Message included for specific logging_mod level")
30  else
31  call assert_not_equals(str(j)//" level", outpt, &
32  "Message ignored for specific logging_mod level")
33  end if
34  end do
35  end do
36  end subroutine test_logging_level
37 
38  ! Tests that the specific logging_mod levels produce the correct preambles
39  subroutine test_logging_preamble()
40  character(len=7) :: preamble
41 
43  call assert_equals(error_preamble, preamble)
44 
45  preamble = get_logging_preamble(log_warn)
46  call assert_equals(warn_preamble, preamble)
47 
48  preamble = get_logging_preamble(log_info)
49  call assert_equals(info_preamble, preamble)
50 
52  call assert_equals(debug_preamble, preamble)
53  end subroutine test_logging_preamble
54 
55  ! Helper function to convert an integer into a string
56  character(len=15) function str(k)
57  integer, intent(in) :: k
58  write (str, *) k
59  str = adjustl(str)
60  end function str
61 
62 end module test_logging_mod
63 
64 ! Driver for logging_mod utility tests
66  use fruit, only : init_fruit, run_test_case, fruit_summary
68 
69  implicit none
70  logical :: error_fatal
71  call init_fruit
72  error_fatal = .false. ! Error messages continue to allow for testing
73  call run_test_case(test_logging_level, "Test logging_mod level")
74  call run_test_case(test_logging_preamble, "Test logging_mod preamble")
75  call fruit_summary
76 end program test_logging_mod_driver
character(len= *), parameter error_preamble
Error message preamble.
Definition: logging.F90:16
logical, parameter error_is_fatal
Whether an error log message is fatal and stops execution.
Definition: logging.F90:24
integer, parameter, public log_error
Only log ERROR messages.
Definition: logging.F90:11
program test_logging_mod_driver
Logging utility.
Definition: logging.F90:2
character(len=15) function str(k)
integer, parameter, public log_debug
Log DEBUG, INFO, WARNING and ERROR messages.
Definition: logging.F90:14
character(len=7) function get_logging_preamble(level)
Returns the string preamble that applies to the specific logging level.
Definition: logging.F90:98
subroutine, public log_log(level, message, str)
Logs a message at the specified level. If the level is above the current level then the message is ig...
Definition: logging.F90:75
subroutine, public log_set_logging_level(level)
Sets the logging level, messages with less priority will be ignored.
Definition: logging.F90:114
integer, parameter, public log_warn
Log WARNING and ERROR messages.
Definition: logging.F90:12
character(len= *), parameter warn_preamble
Warning message preable.
Definition: logging.F90:16
subroutine test_logging_level
subroutine test_logging_preamble()
integer, parameter, public log_info
Log INFO, WARNING and ERROR messages.
Definition: logging.F90:13
character(len= *), parameter info_preamble
Info message preamble.
Definition: logging.F90:16
integer function, public log_get_logging_level()
Retrieves the current logging level.
Definition: logging.F90:122
character(len= *), parameter debug_preamble
Debug message preamble.
Definition: logging.F90:16