diff --git a/binarycpython/utils/custom_logging_functions.py b/binarycpython/utils/custom_logging_functions.py
index c0d963c43406e25041684c1b318156b9ec4d7866..218f3ae97736ba127c837b9f9e7d08e198633866 100644
--- a/binarycpython/utils/custom_logging_functions.py
+++ b/binarycpython/utils/custom_logging_functions.py
@@ -366,7 +366,7 @@ def compile_shared_lib(
 
 
 def create_and_load_logging_function(
-    custom_logging_code: str, verbose: int = 0
+    custom_logging_code: str, verbose: int = 0, custom_tmp_dir=None
 ) -> Tuple[int, str]:
     """
     Function to automatically compile the shared library with the given
@@ -382,9 +382,13 @@ def create_and_load_logging_function(
     Returns:
         memory adress of the custom logging function in a int type.
     """
-
     #
 
+    if not custom_tmp_dir:
+        tmp_dir = temp_dir()
+    else:
+        tmp_dir = custom_tmp_dir
+
     library_name = os.path.join(
         temp_dir(), "libcustom_logging_{}.so".format(uuid.uuid4().hex)
     )
diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py
index 91f6b16ef7717e3259a09f57ce01567beaeeff07..8d788d11c9631140a15c68c23ccc3f38b3a6874c 100644
--- a/binarycpython/utils/grid.py
+++ b/binarycpython/utils/grid.py
@@ -586,7 +586,7 @@ class Population:
                 self.grid_options["custom_logging_func_memaddr"],
                 self.grid_options["_custom_logging_shared_library_file"],
             ) = create_and_load_logging_function(
-                custom_logging_code, verbose=self.grid_options["verbosity"]
+                custom_logging_code, verbose=self.grid_options["verbosity"], custom_tmp_dir=self.grid_options["tmp_dir"]
             )
 
         elif self.grid_options["C_auto_logging"]:
@@ -606,7 +606,7 @@ class Population:
                 self.grid_options["custom_logging_func_memaddr"],
                 self.grid_options["_custom_logging_shared_library_file"],
             ) = create_and_load_logging_function(
-                custom_logging_code, verbose=self.grid_options["verbosity"]
+                custom_logging_code, verbose=self.grid_options["verbosity"], custom_tmp_dir=self.grid_options["tmp_dir"]
             )
 
     ###################################################