Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing global variable from runLog.py #1370

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions armi/runLog.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
if self.isEnabledFor({1}):
self._log({1}, message, args, **kws)
logging.Logger.{0} = {0}"""
_WHITE_SPACE = " " * 6
LOG_DIR = os.path.join(os.getcwd(), "logs")
OS_SECONDS_TIMEOUT = 2 * 60
SEP = "|"
Expand All @@ -88,7 +87,6 @@ def __init__(self, mpiRank=0):
----------
mpiRank : int
If this is zero, we are in the parent process, otherwise child process.
The default of 0 means we assume the parent process.
This should not be adjusted after instantiation.
"""
self._mpiRank = mpiRank
Expand All @@ -107,25 +105,49 @@ def setNullLoggers(self):
self.logger = NullLogger("NULL")
self.stderrLogger = NullLogger("NULL2", isStderr=True)

def _setLogLevels(self):
"""Here we fill the logLevels dict with custom strings that depend on the MPI rank."""
@staticmethod
def getLogLevels(mpiRank):
"""Helper method to build an important data object this class needs.

Parameters
----------
mpiRank : int
If this is zero, we are in the parent process, otherwise child process.
This should not be adjusted after instantiation.
"""
rank = "" if mpiRank == 0 else "-{:>03d}".format(mpiRank)

# NOTE: using ordereddict so we can get right order of options in GUI
_rank = "" if self._mpiRank == 0 else "-{:>03d}".format(self._mpiRank)
self.logLevels = collections.OrderedDict(
return collections.OrderedDict(
[
("debug", (logging.DEBUG, "[dbug{}] ".format(_rank))),
("extra", (15, "[xtra{}] ".format(_rank))),
("info", (logging.INFO, "[info{}] ".format(_rank))),
("important", (25, "[impt{}] ".format(_rank))),
("prompt", (27, "[prmt{}] ".format(_rank))),
("warning", (logging.WARNING, "[warn{}] ".format(_rank))),
("error", (logging.ERROR, "[err {}] ".format(_rank))),
("header", (100, "{}".format(_rank))),
("debug", (logging.DEBUG, f"[dbug{rank}] ")),
("extra", (15, f"[xtra{rank}] ")),
("info", (logging.INFO, f"[info{rank}] ")),
("important", (25, f"[impt{rank}] ")),
("prompt", (27, f"[prmt{rank}] ")),
("warning", (logging.WARNING, f"[warn{rank}] ")),
("error", (logging.ERROR, f"[err {rank}] ")),
("header", (100, f"{rank}")),
]
)

@staticmethod
def getWhiteSpace(mpiRank):
"""Helper method to build the white space used to left-adjust the log lines.

Parameters
----------
mpiRank : int
If this is zero, we are in the parent process, otherwise child process.
This should not be adjusted after instantiation.
"""
logLevels = _RunLog.getLogLevels(mpiRank)
return " " * len(max([ll[1] for ll in logLevels.values()]))

def _setLogLevels(self):
"""Here we fill the logLevels dict with custom strings that depend on the MPI rank."""
self.logLevels = self.getLogLevels(self._mpiRank)
self._logLevelNumbers = sorted([ll[0] for ll in self.logLevels.values()])
global _WHITE_SPACE
_WHITE_SPACE = " " * len(max([ll[1] for ll in self.logLevels.values()]))

# modify the logging module strings for printing
for longLogString, (logValue, shortLogString) in self.logLevels.items():
Expand Down Expand Up @@ -462,7 +484,8 @@ def filter(self, record):
return False

# Handle some special string-mangling we want to do, for multi-line messages
record.msg = msg.rstrip().replace("\n", "\n" + _WHITE_SPACE)
whiteSpace = _RunLog.getWhiteSpace(context.MPI_RANK)
record.msg = msg.rstrip().replace("\n", "\n" + whiteSpace)
return True


Expand Down
13 changes: 13 additions & 0 deletions armi/tests/test_runLog.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ def test_parentRunLogging(self):
self.assertIn("Hello", streamVal, msg=streamVal)
self.assertIn("world", streamVal, msg=streamVal)

def test_getWhiteSpace(self):
log = runLog._RunLog(0)
space0 = len(log.getWhiteSpace(0))
space1 = len(log.getWhiteSpace(1))
space9 = len(log.getWhiteSpace(9))

self.assertGreater(space1, space0)
self.assertEqual(space1, space9)

def test_warningReport(self):
"""A simple test of the warning tracking and reporting logic."""
# create the logger and do some logging
Expand Down Expand Up @@ -125,6 +134,10 @@ def test_warningReport(self):
self.assertNotIn("invisible", streamVal, msg=streamVal)
self.assertEqual(streamVal.count("test_warningReport"), 2, msg=streamVal)

# bonus check: edge case in duplicates filter
log.logger = None
self.assertIsNone(log.getDuplicatesFilter())

def test_warningReportInvalid(self):
"""A test of warningReport in an invalid situation."""
# create the logger and do some logging
Expand Down