diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index abfd9a4843d3564827e6e125b5eb573e78583493..ccd9ae19c3682828e218a94d2f54e6d23585767e 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -8,10 +8,12 @@ but copying functionality isn't recommended except if you know what you are doin """ import os +import sys import copy import json import datetime import time +import logging import argparse import importlib.util @@ -20,7 +22,6 @@ from pathos.helpers import mp as pathos_multiprocess # from pathos.multiprocessing import ProcessingPool as Pool from pathos.pools import _ProcessPool as Pool - from binarycpython.utils.grid_options_defaults import grid_options_defaults_dict from binarycpython.utils.custom_logging_functions import ( autogen_C_logging_code, @@ -1381,6 +1382,51 @@ class Population: """ self.grid_options["count"] += 1 + + def set_loggers(self): + """ + Function to set the loggers for the execution of the grid + """ + + # Set logfile + binary_c_logfile = self.grid_options["log_file"] + + # Create directory + os.makedirs(os.path.dirname(binary_c_logfile), exist_ok=True) + + # Set up logger + self.logger = logging.getLogger('binary_c_python_logger') + self.logger.setLevel(self.grid_options["verbose"]) + + # Reset handlers + self.logger.handlers = [] + + # Set formatting of output + log_formatter = logging.Formatter( + '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + ) + + # Make and add filehandlers + # make handler for output to file + handler_file = logging.FileHandler( + filename=os.path.join(binary_c_logfile) + ) + handler_file.setFormatter(log_formatter) + handler_file.setLevel(logging.INFO) + + # Make handler for output to stdout + handler_stdout = logging.StreamHandler(sys.stdout) + handler_stdout.setFormatter(log_formatter) + handler_stdout.setLevel(logging.INFO) + + # Add the loggers + self.logger.addHandler(handler_file) + self.logger.addHandler(handler_stdout) + + + + + # def join_result_dicts(self): # """ # Function to join the result dictionaries diff --git a/binarycpython/utils/grid_options_defaults.py b/binarycpython/utils/grid_options_defaults.py index 47273b9a6537e26ff5b441c4efa0088dd645d2fc..68eeef0f9c366141454efe8316ad4615fef4bc6b 100644 --- a/binarycpython/utils/grid_options_defaults.py +++ b/binarycpython/utils/grid_options_defaults.py @@ -11,13 +11,17 @@ grid_options_defaults_dict = { # general (or unordered..) ########################## "amt_cores": 1, # total amount of cores used to evolve the population - "verbose": 0, # Level of verbosity of the simulation "binary": 0, # FLag on whether the systems are binary systems or single systems. "parse_function": None, # FUnction to parse the output with. "tmp_dir": temp_dir(), # Setting the temp dir of the program "main_pid": -1, # Placeholder for the main process id of the run. # "output_dir": ########################## + # Execution log: + ########################## + "verbose": 0, # Level of verbosity of the simulation. 0=INFO, + "log_file": os.path.join(temp_dir(), 'binary_c_python.log'), # Set to None to not log to file. The directory will be created + ########################## # binary_c files ########################## "binary_c_executable": os.path.join( @@ -43,7 +47,7 @@ grid_options_defaults_dict = { # Store pre-loading: ########################## "store_memaddr": -1, # Contains the store object memory adress, useful for preloading. - # defaults to -1 and isnt used if thats the default then. + # defaults to -1 and isnt used if thats the default then. ########################## # Log args: logging of arguments ##########################