diff --git a/binarycpython/utils/custom_logging_functions.py b/binarycpython/utils/custom_logging_functions.py index 51c87068e3b789b64b0839c6ac2ebf960506a872..949ba3c2916292c0b026baee21d87c20168e6484 100644 --- a/binarycpython/utils/custom_logging_functions.py +++ b/binarycpython/utils/custom_logging_functions.py @@ -13,7 +13,7 @@ from typing import Union, Tuple, Optional from binarycpython.utils.functions import temp_dir, remove_file, verbose_print -def autogen_C_logging_code(logging_dict: dict, verbose: int = 0) -> Optional[str]: +def autogen_C_logging_code(logging_dict: dict, verbosity: int = 0) -> Optional[str]: """ Function that auto-generates PRINTF statements for binaryc. Input is a dictionary where the key is the header of that logging line @@ -50,12 +50,13 @@ def autogen_C_logging_code(logging_dict: dict, verbose: int = 0) -> Optional[str # Loop over dict keys for key in logging_dict: - if verbose > 0: - print( - "Generating Print statement for custom logging code with {} as a header".format( + verbose_print( + "Generating Print statement for custom logging code with {} as a header".format( key - ) - ) + ), + verbosity, + 1, + ) logging_dict_entry = logging_dict[key] # Check if item is of correct type: @@ -83,7 +84,7 @@ def autogen_C_logging_code(logging_dict: dict, verbose: int = 0) -> Optional[str #################################################################################### -def binary_c_log_code(code: str, verbose: int = 0) -> str: +def binary_c_log_code(code: str, verbosity: int = 0) -> str: """ Function to construct the code to construct the custom logging function @@ -114,16 +115,17 @@ def binary_c_log_code(code: str, verbose: int = 0) -> str: Args: code: Exact c-statement to output information in binary_c. Can be wrapped in logical statements. - verbose: Level of verbosity. Defaults to zero if not set explicitly. + verbosity: Level of verbosity. Defaults to zero if not set explicitly. Returns: string containing the custom logging code. This includes all the includes and other definitions. This code will be used as the shared library """ verbose_print( - "Creating the code for the shared library for the custom logging", verbose, 0 + "Creating the code for the shared library for the custom logging", + verbosity, + 1, ) - if not "Printf" in code: print( "Error: There has to be at least a printf statement in the provided code. Aborting" @@ -158,14 +160,14 @@ void binary_c_API_function custom_output_function(struct stardata_t * stardata) return textwrap.dedent(custom_logging_function_string) -def binary_c_write_log_code(code: str, filename: str, verbose: int = 0) -> None: +def binary_c_write_log_code(code: str, filename: str, verbosity: int = 0) -> None: """ Function to write the generated logging code to a file Args: code: string containing the custom logging code to write to a file. filename: target filename. - verbose: Level of verbosity. Defaults to zero if not set explicitly. + verbosity: Level of verbosity. Defaults to zero if not set explicitly. """ # TODO: change this. I don't like the cwd @@ -173,10 +175,12 @@ def binary_c_write_log_code(code: str, filename: str, verbose: int = 0) -> None: filePath = os.path.join(cwd, filename) # Remove if it exists - remove_file(filePath, verbose) - - if verbose > 0: - print("Writing the custom logging code to {}".format(filePath)) + remove_file(filePath, verbosity) + verbose_print( + "Writing the custom logging code to {}".format(filePath), + verbosity, + 1, + ) # Write again with open(filePath, "w") as file: @@ -208,7 +212,7 @@ def from_binary_c_config(config_file: str, flag: str) -> str: return res -def return_compilation_dict(verbose: int = 0) -> dict: +def return_compilation_dict(verbosity: int = 0) -> dict: """ Function to build the compile command for the shared library @@ -218,16 +222,18 @@ def return_compilation_dict(verbose: int = 0) -> dict: # https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc/ Args: - verbose: Level of verbosity. Defaults to zero if not set explicitly. + verbosity: Level of verbosity. Defaults to zero if not set explicitly. Returns: string containing the command to build the shared library """ - if verbose > 0: - print( - "Calling the binary_c config code to get the info to build the shared library" - ) + verbose_print( + "Calling the binary_c config code to get the info to build the shared library", + verbosity, + 1, + ) + # use binary_c-config to get necessary flags BINARY_C_DIR = os.getenv("BINARY_C") if BINARY_C_DIR: @@ -297,18 +303,20 @@ def return_compilation_dict(verbose: int = 0) -> dict: ] libs = "{} {}".format(" ".join(library_paths), " ".join(non_library_paths)) - if verbose > 0: - print( - "Got options to compile:\n\tcc = {cc}\n\tccflags = {ccflags}\n\tld = {ld}\n\tlibs = {libs}\n\tinc = {inc}\n\n".format( + # + verbose_print( + "Got options to compile:\n\tcc = {cc}\n\tccflags = {ccflags}\n\tld = {ld}\n\tlibs = {libs}\n\tinc = {inc}\n\n".format( cc=cc, ccflags=ccflags, ld=ld, libs=libs, inc=inc - ) - ) + ), + verbosity, + 1, + ) return {"cc": cc, "ld": ld, "ccflags": ccflags, "libs": libs, "inc": inc} def compile_shared_lib( - code: str, sourcefile_name: str, outfile_name: str, verbose: int = 0 + code: str, sourcefile_name: str, outfile_name: str, verbosity: int = 0 ) -> None: """ Function to write the custom logging code to a file and then compile it. @@ -320,17 +328,17 @@ def compile_shared_lib( code: string containing the custom logging code sourcefile_name: name of the file that will contain the code outfile_name: name of the file that will be the shared library - verbose: Level of verbosity. Defaults to zero if not set explicitly. + verbosity: Level of verbosity. Defaults to zero if not set explicitly. """ # Write code to file - binary_c_write_log_code(code, sourcefile_name, verbose) + binary_c_write_log_code(code, sourcefile_name, verbosity) # Remove the library if present: - remove_file(outfile_name, verbose) + remove_file(outfile_name, verbosity) # create compilation command - compilation_dict = return_compilation_dict(verbose) + compilation_dict = return_compilation_dict(verbosity) # Construct full command command = ( @@ -348,26 +356,32 @@ def compile_shared_lib( command = " ".join(command.split()) # Execute compilation and create the library - if verbose > 0: - print( - "Building shared library for custom logging with (binary_c.h) on {}\n".format( + verbose_print( + "Building shared library for custom logging with (binary_c.h) on {}\n".format( socket.gethostname() - ) - ) - print( - "Executing following command to compile the shared library:\n{command}".format( - command=command - ) - ) - res = subprocess.check_output("{command}".format(command=command), shell=True) - # print(res) - if verbose > 0: - if res: - print("Output of compilation command:\n{}".format(res)) + ), + verbosity, + 1, + ) + verbose_print( + "Executing following command to compile the shared library:\n{command}".format( + command=command + ), + verbosity, + 1, + ) + # + res = subprocess.check_output("{command}".format(command=command), shell=True) + if res: + verbose_print( + "Output of compilation command:\n{}".format(res), + verbosity, + 1, + ) def create_and_load_logging_function( - custom_logging_code: str, verbose: int = 0, custom_tmp_dir=None + custom_logging_code: str, verbosity: int = 0, custom_tmp_dir=None ) -> Tuple[int, str]: """ Function to automatically compile the shared library with the given @@ -378,7 +392,7 @@ def create_and_load_logging_function( Args: custom_logging_code: string containing the custom logging code - verbose: Level of verbosity. Defaults to zero if not set explicitly. + verbosity: Level of verbosity. Defaults to zero if not set explicitly. Returns: memory address of the custom logging function in a capsule. @@ -404,11 +418,14 @@ def create_and_load_logging_function( custom_logging_code, sourcefile_name=os.path.join(custom_logging_dir, "custom_logging.c"), outfile_name=library_name, - verbose=verbose, + verbosity=verbosity, ) - if verbose > 0: - print("loading shared library for custom logging") + verbose_print( + "loading shared library for custom logging", + verbosity, + 1, + ) # Loading library _ = ctypes.CDLL("libgslcblas.so", mode=ctypes.RTLD_GLOBAL) @@ -433,12 +450,13 @@ def create_and_load_logging_function( ) raise ValueError - if verbose > 0: - print( - "loaded shared library for custom logging. \ - custom_output_function is loaded in memory at {}".format( - func_memaddr - ) - ) + verbose_print( + "loaded shared library for custom logging. \ + custom_output_function is loaded in memory at {}".format( + func_memaddr + ), + verbosity, + 1, + ) return func_memaddr, library_name diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 293215d0431e193b7353d30f29531c329946e4f4..c2ef2fea3a1c6121bb148b01b10aea1700306555 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -47,6 +47,7 @@ from binarycpython.utils.grid_options_defaults import ( grid_options_defaults_dict, moe_di_stefano_default_options, _MS_VERBOSITY_LEVEL, + _CUSTOM_LOGGING_VERBOSITY_LEVEL, ) from binarycpython.utils.custom_logging_functions import ( @@ -229,11 +230,13 @@ class Population: # The of the keys go into a custom_options dict else: - print( - "!! Key doesnt match previously known parameter: \ - adding: {}={} to custom_options".format( + verbose_print( + "<<<< Warning: Key does not match previously known parameter: \ + adding: {}={} to custom_options >>>>".format( key, kwargs[key] - ) + ), + self.grid_options["verbosity"], + 1, ) self.custom_options[key] = kwargs[key] def parse_cmdline(self) -> None: @@ -677,7 +680,7 @@ class Population: # Generate entire shared lib code around logging lines custom_logging_code = binary_c_log_code( self.grid_options["C_logging_code"], - verbose=self.grid_options["verbosity"], + verbosity=self.grid_options["verbosity"]-(_CUSTOM_LOGGING_VERBOSITY_LEVEL-1), ) # Load memory address @@ -686,7 +689,7 @@ class Population: self.grid_options["_custom_logging_shared_library_file"], ) = create_and_load_logging_function( custom_logging_code, - verbose=self.grid_options["verbosity"], + verbosity=self.grid_options["verbosity"]-(_CUSTOM_LOGGING_VERBOSITY_LEVEL-1), custom_tmp_dir=self.grid_options["tmp_dir"], ) @@ -694,12 +697,12 @@ class Population: # Generate real logging code logging_line = autogen_C_logging_code( self.grid_options["C_auto_logging"], - verbose=self.grid_options["verbosity"], + verbosity=self.grid_options["verbosity"]-(_CUSTOM_LOGGING_VERBOSITY_LEVEL-1), ) # Generate entire shared lib code around logging lines custom_logging_code = binary_c_log_code( - logging_line, verbose=self.grid_options["verbosity"] + logging_line, verbosity=self.grid_options["verbosity"]-(_CUSTOM_LOGGING_VERBOSITY_LEVEL-1) ) # Load memory address @@ -708,9 +711,10 @@ class Population: self.grid_options["_custom_logging_shared_library_file"], ) = create_and_load_logging_function( custom_logging_code, - verbose=self.grid_options["verbosity"], + verbosity=self.grid_options["verbosity"]-(_CUSTOM_LOGGING_VERBOSITY_LEVEL-1), custom_tmp_dir=self.grid_options["tmp_dir"], ) + ################################################### # Ensemble functions ################################################### diff --git a/binarycpython/utils/grid_options_defaults.py b/binarycpython/utils/grid_options_defaults.py index 7f80a0e4f3392c7d621f9cbe8db0766172a0ac9a..e74f9c395125161901a9707024ba16e5b0ba388c 100644 --- a/binarycpython/utils/grid_options_defaults.py +++ b/binarycpython/utils/grid_options_defaults.py @@ -18,6 +18,7 @@ import os from binarycpython.utils.custom_logging_functions import temp_dir from binarycpython.utils.functions import return_binary_c_version_info +_CUSTOM_LOGGING_VERBOSITY_LEVEL = 2 _MS_VERBOSITY_LEVEL = 5 _MS_VERBOSITY_INTERPOLATOR_LEVEL = 6 _MS_VERBOSITY_INTERPOLATOR_EXTRA_LEVEL = 7 diff --git a/examples/notebook_individual_systems.ipynb b/examples/notebook_individual_systems.ipynb index 63f4e6a6134a492ce99d5e68c9635c1de9160a71..f5d0b22cf9950584ec4333314b0945ad088cd122 100644 --- a/examples/notebook_individual_systems.ipynb +++ b/examples/notebook_individual_systems.ipynb @@ -2561,7 +2561,9 @@ "## Single system via API functionality\n", "It is possible to construct your own functionality to run a single system by directly calling the API function to run a system. Under the hood all the other functions and wrappers actually use this API.\n", "\n", - "There are less failsafes for this method, so this make sure the input is correct and binary_c knows all the arguments you pass in." + "There are less failsafes for this method, so this make sure the input is correct and binary_c knows all the arguments you pass in.\n", + "\n", + "for more details on this API function see `notebook_api_functions`" ] }, { @@ -2621,7 +2623,7 @@ "id": "55c8ea24-0fc0-452c-8121-1e7667433479", "metadata": {}, "source": [ - "As we can see above, the output is rather empty. But if SINGLE_STAR_LIFETIME is printed we know we caught the output correctly. To get actual output we should have timesteps printed in the `log_every_timestep.c` in binary_c, or add some custom_logging (see notebook_custom_logging) " + "As we can see above, the output is rather empty. But if SINGLE_STAR_LIFETIME is printed we know we caught the output correctly. To get actual output we should have timesteps printed in the `log_every_timestep.c` in binary_c, or add some custom_logging (see notebook_custom_logging). " ] }, { @@ -2701,20 +2703,6 @@ "\n", "\n" ] - }, - { - "cell_type": "markdown", - "id": "ec3ebc81-daf5-4052-8e71-3034ea8d764b", - "metadata": {}, - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "afbe134c-e124-486c-9bcf-98187b35fe4a", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {