Skip to content

Commit

Permalink
Set the default logger to a stdout stream, and add option to manually…
Browse files Browse the repository at this point in the history
… specify an output directory for file loggers
  • Loading branch information
IshaanDesai committed Dec 30, 2024
1 parent d83176d commit 3b68cf3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
13 changes: 10 additions & 3 deletions micro_manager/adaptivity/adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ def __init__(self, configurator, rank) -> None:
configurator.get_adaptivity_similarity_measure()
)

self._metrics_logger = Logger(
"Adaptivity", "adaptivity-metrics.csv", rank, csv_logger=True
)
output_dir = configurator.get_output_dir()

if output_dir is not None:
self._metrics_logger = Logger(
__name__, output_dir + "adaptivity-metrics.csv", rank, csv_logger=True
)
else:
self._metrics_logger = Logger(
__name__, "adaptivity-metrics.csv", rank=rank, csv_logger=True
)

self._metrics_logger.log_info_one_rank(
"Time Window,Avg Active Sims,Avg Inactive Sims,Max Active,Max Inactive"
Expand Down
20 changes: 19 additions & 1 deletion micro_manager/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, config_file_name):
self._postprocessing_file_name = None
self._initialize_once = False

self._output_micro_sim_time = False
self._output_dir = None

def set_logger(self, logger):
"""
Expand Down Expand Up @@ -93,6 +93,13 @@ def _read_json(self, config_file_name):
.replace(".py", "")
)

try:
self._output_dir = self._data["output_dir"]
except BaseException:
self._logger.log_info_one_rank(
"No output directory provided. Output (including logging) will be saved in the current working directory."
)

try:
self._write_data_names = self._data["coupling_params"]["write_data_names"]
assert isinstance(
Expand Down Expand Up @@ -642,3 +649,14 @@ def create_single_sim_object(self):
True if initialization is done only once, False otherwise.
"""
return self._initialize_once

def get_output_dir_name(self):
"""
Get the name of the output directory.
Returns
-------
output_dir : string
Name of the output folder.
"""
return self._output_dir
4 changes: 1 addition & 3 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ def __init__(self, config_file: str) -> None:
"""
super().__init__(config_file)

self._logger = Logger(
"MicroManagerCoupling", "micro-manager-coupling.log", self._rank
)
self._logger = Logger(__name__, self._rank)

self._config.set_logger(self._logger)
self._config.read_json_micro_manager()
Expand Down
12 changes: 9 additions & 3 deletions micro_manager/tools/logging_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class Logger:
Provides a logging wrapper for the Micro Manager classes.
"""

def __init__(self, name, log_file, rank=0, level=logging.INFO, csv_logger=False):
def __init__(
self, name, log_file=None, rank=0, level=logging.INFO, csv_logger=False
):
"""
Set up a logger.
Expand All @@ -19,7 +21,7 @@ def __init__(self, name, log_file, rank=0, level=logging.INFO, csv_logger=False)
name : string
Name of the logger.
log_file : string
Name of the log file.
Name of the log file (default is "micro-manager.log").
rank : int, optional
Rank of the logger (default is 0).
level : int, optional
Expand All @@ -30,7 +32,11 @@ def __init__(self, name, log_file, rank=0, level=logging.INFO, csv_logger=False)

self._rank = rank

handler = logging.FileHandler(log_file)
if log_file is None:
handler = logging.StreamHandler()
else:
handler = logging.FileHandler(log_file)

handler.setLevel(level)

if csv_logger:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"micro_file_name": "micro_dummy",
"output_dir": "adaptivity_output",
"coupling_params": {
"precice_config_file_name": "precice-config.xml",
"macro_mesh_name": "macro-cube-mesh",
Expand Down

0 comments on commit 3b68cf3

Please sign in to comment.