idaholab / raven

RAVEN is a flexible and multi-purpose probabilistic risk analysis, validation and uncertainty quantification, parameter optimization, model reduction and data knowledge-discovering framework.
https://raven.inl.gov/
Apache License 2.0
216 stars 132 forks source link

[TASK] Be able to direct output somewhere other than sys.stdout #2255

Closed j-bryan closed 6 months ago

j-bryan commented 6 months ago

Issue Description

Is your feature request related to a problem? Please describe. Due to an implementation detail in the MessageHandler class, users are unable to redirect output to somewhere other than sys.stdout. Adding the ability to redirect that output would facilitate development of other user interfaces and generally allow users more control over how and where the text output of their RAVEN case is directed.

Describe the solution you'd like The function in question in the MessageHandler class is this one:

  def message(self, caller, message, tag, verbosity, color=None, writeTo=sys.stdout, forcePrint=False):
    """
      Print a message
      @ In, caller, object, the entity desiring to print a message
      @ In, message, string, the message to print
      @ In, tag, string, the printed message type (usually Message, Debug, or Warning, and sometimes FIXME)
      @ In, verbosity, string, the print priority of the message
      @ In, color, string, optional, color to apply to message
      @ In, forcePrint, bool, optional, force the print independetly on the verbosity level? Defaul False
      @ Out, None
    """
    verbval = self.checkVerbosity(verbosity)
    okay, msg = self._printMessage(caller, message, tag, verbval, color, forcePrint)
    if tag.lower().strip() == 'warning':
      self.addWarning(message)
    if okay:
      print(msg, file=writeTo)
    sys.stdout.flush()

The issue is the default value writeTo=sys.stdout reverting overwrites of sys.stdout to sys.__stdout__, and there is no opportunity through functions higher up the call stack (e.g. in the MessageUser methods) to specify a different value for that argument.

Through offline discussion with @PaulTalbot-INL and @joshua-cogliati-inl, two paths forward have been proposed:

  1. Simply remove the option since it's unused and apparently only partially implemented. This would allow Python scripts running RAVEN as a module to overwrite sys.stdout to redirect output to a desired location.
  2. Store writeTo as an instance variable (e.g. as self._writeTo in MessageHandler), which can then be referenced throughout the class to direct output to the appropriate place.

Some initial work leads me to believe option 1 is the better option. When using RAVEN's command-line interface, stdout and stderr can easily be captured and directed to files through additional command line arguments. The real benefit of addressing this issue is for use of RAVEN from a Python environment, in which case, direction of output can once again be handled externally more simply than internally within MessageHandler.

Describe alternatives you've considered See the above discussion.


For Change Control Board: Issue Review

This review should occur before any development is performed as a response to this issue.


For Change Control Board: Issue Closure

This review should occur when the issue is imminently going to be closed.