Skip to content

Add logging

Sebastian Müller requested to merge add_logging into main

Added mo_logging that was derived from the flogging project of Daan van Vugt.

The logging module is integrated into mo_cli to setup a command line interface with logging options on the fly:

#include "logging.h"
program test_log
  use mo_logging
  use mo_cli, only : cli_parser
  implicit none
  type(cli_parser) :: parser
  parser = cli_parser( &
    description="Program with cli and logger.", &
    add_help_option=.true., &
    add_logger_options=.true.)
  call parser%parse()
  log_fatal(*) "fatal"
  log_error(*) "error"
  log_warn(*) "warn"
  log_info(*) "info"
  log_debug(*) "debug"
  log_trace(*) "trace"
  log_subtrace(*) "subtrace"
end program test_log

You can now call this program with:

$ ./prog --quiet
  test.F90:12    FATAL fatal
  test.F90:13    ERROR error
  test.F90:14     WARN warn

You can see the logging options with:

$ ./prog -h

mo_message provides a format reset switch, since logging uses formatting, you need to reset the format before you can write standard messages again (not colored or bold)

log_info(*) "info in green"
call message("normal text again", reset_format=.true.)

Also mo_message now has public switches to turn message on and off: show_msg and show_err. An optional show argument was added to message and error_message with these global variables as default values.

use mo_message, only : message, show_msg
show_msg = .false.
call message("not showing")
call message("forced to be showing", show=.true.)
show_msg = .true.
call message("showing up again")
call message("will never show up", show=.false.)

Finally, an optional raise argument was added to error_message to control the stop call:

call error_message("***ERROR: this went wrong", raise=.false.)
call error_message("          some more details and a stop..")
Edited by Sebastian Müller

Merge request reports

Loading