From 9bc43bf7bc5359c8179e14014cc12bba2170e9d1 Mon Sep 17 00:00:00 2001
From: lukas leufen <l.leufen@fz-juelich.de>
Date: Mon, 23 Mar 2020 14:55:09 +0100
Subject: [PATCH] add log file counter

---
 src/run_modules/run_environment.py | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/run_modules/run_environment.py b/src/run_modules/run_environment.py
index c5bc2cb4..9b37d2bc 100644
--- a/src/run_modules/run_environment.py
+++ b/src/run_modules/run_environment.py
@@ -39,11 +39,7 @@ class RunEnvironment(object):
             logging.info(f"{self.__class__.__name__} finished after {self.time}")
             self.del_by_exit = True
         if self.__class__.__name__ == "RunEnvironment":
-            try:
-                new_file = os.path.join(self.data_store.get("experiment_path"), "logging.log")
-                shutil.copyfile(self.logger.log_file, new_file)
-            except (NameNotFoundInDataStore, FileNotFoundError):
-                pass
+            self.__copy_log_file()
             self.data_store.clear_data_store()
 
     def __enter__(self):
@@ -52,6 +48,20 @@ class RunEnvironment(object):
     def __exit__(self, exc_type, exc_val, exc_tb):
         self.__del__()
 
+    def __copy_log_file(self):
+        try:
+            counter = 0
+            filename_pattern = os.path.join(self.data_store.get("experiment_path"), "logging_%03i.log")
+            new_file = filename_pattern % counter
+            while os.path.exists(new_file):
+                counter += 1
+                new_file = filename_pattern % counter
+            logging.info(f"Copy log file to {new_file}")
+            shutil.copyfile(self.logger.log_file, new_file)
+        except (NameNotFoundInDataStore, FileNotFoundError):
+            pass
+
+
     @staticmethod
     def do_stuff(length=2):
         time.sleep(length)
-- 
GitLab