# custom_logging_functions module

Module containing functions for the custom logging functionality. The functions here make it possible for the user to define binaryc output logs on runtime

`binarycpython.utils.custom_logging_functions.``autogen_C_logging_code`(logging_dict, verbosity=0)

  Function that auto-generates PRINTF statements for binaryc. Input is a dictionary where the key is the header of that logging line and items which are lists of parameters that will be put in that logging line

  The list elements are all appended to ‘stardata->’ in the auto-generated code.

  Example

  Input dictionary should look like this:

      {'MY_STELLAR_DATA':
          [
              'model.time',
              'star[0].mass',
              'model.probability',
              'model.dt'
          ]
      }

  Parameters

    - logging_dict (`dict`) – Dictionary containing lists of parameters that binary_c has to output. The keys are used by binary_c as start of the sentence.

    - verbose – Level of verbosity. Defaults to zero if not set explicitly.

  Return type

    `Optional`[`str`]

  Returns

    string containing C printf statement built to output the parameters given as input.

`binarycpython.utils.custom_logging_functions.``binary_c_log_code`(code, verbosity=0)

  Function to construct the code to construct the custom logging function

  Example

  Code to log and terminate evolution when the primary star becomes a NS:

      if(stardata->star[0].stellar_type>=NS)
      {
          if (stardata->model.time < stardata->model.max_evolution_time)
          {
              Printf("EXAMPLE_LOG_CO %30.12e %g %g %g %g %d %d\n",
                  //
                  stardata->model.time, // 1

                  stardata->star[0].mass, //2
                  stardata->previous_stardata->star[0].mass, //3

                  stardata->star[0].radius, //4
                  stardata->previous_stardata->star[0].radius, //5

                  stardata->star[0].stellar_type, //6
                  stardata->previous_stardata->star[0].stellar_type //7
            );
          };
          /* Kill the simulation to save time */
          stardata->model.max_evolution_time = stardata->model.time - stardata->model.dtm;
      };

  Parameters

    - code (`str`) – Exact c-statement to output information in binary_c. Can be wrapped in logical statements.

    - verbosity (`int`) – Level of verbosity. Defaults to zero if not set explicitly.

  Return type

    `str`

  Returns

    string containing the custom logging code. This includes all the includes and other definitions. This code will be used as the shared library

`binarycpython.utils.custom_logging_functions.``binary_c_write_log_code`(code, filename, verbosity=0)

  Function to write the generated logging code to a file

  Parameters

    - code (`str`) – string containing the custom logging code to write to a file.

    - filename (`str`) – target filename.

    - verbosity (`int`) – Level of verbosity. Defaults to zero if not set explicitly.

  Return type

    `None`

`binarycpython.utils.custom_logging_functions.``compile_shared_lib`(code, sourcefile_name, outfile_name, verbosity=0)

  Function to write the custom logging code to a file and then compile it.

  TODO: nicely put in the -fPIC TODO: consider returning a status

  Parameters

    - code (`str`) – string containing the custom logging code

    - sourcefile_name (`str`) – name of the file that will contain the code

    - outfile_name (`str`) – name of the file that will be the shared library

    - verbosity (`int`) – Level of verbosity. Defaults to zero if not set explicitly.

  Return type

    `None`

`binarycpython.utils.custom_logging_functions.``create_and_load_logging_function`(custom_logging_code, verbosity=0, custom_tmp_dir=None)

  Function to automatically compile the shared library with the given custom logging code and load it with ctypes.

  This function is more or less the main function of this module and unless you know what you’re doing with the other functions I recommend using this in function in combination with a function that generates the exact code (like `binary_c_log_code()`)

  Parameters

    - custom_logging_code (`str`) – string containing the custom logging code

    - verbosity (`int`) – Level of verbosity. Defaults to zero if not set explicitly.

  Return type

    `Tuple`[`int`, `str`]

  Returns

    memory address of the custom logging function in a capsule.

`binarycpython.utils.custom_logging_functions.``from_binary_c_config`(config_file, flag)

  Function to run the binaryc_config command with flags

  Parameters

    - config_file (`str`) – binary_c-config filepath TODO: change the name of this

    - flag (`str`) – flag used in the binary_c-config call.

  Return type

    `str`

  Returns

    returns the result of <binary_c-config> <flag>

`binarycpython.utils.custom_logging_functions.``return_compilation_dict`(verbosity=0)

  Function to build the compile command for the shared library

  Inspired by binary_c_inline_config command in Perl

  TODO: this function still has some cleaning up to do w.r.t. default values for the compile command # https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc/

  Parameters

    verbosity (`int`) – Level of verbosity. Defaults to zero if not set explicitly.

  Return type

    `dict`

  Returns

    string containing the command to build the shared library