#2085: generate time based log files

This commit is contained in:
Nick Todd
2023-11-30 16:11:44 +00:00
parent 3ab911f6af
commit 5cd69f343f
2 changed files with 13 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
# © Crown-owned copyright 2023, Defence Science and Technology Laboratory UK
import datetime as datetime
import logging
import logging.config
import shutil
@@ -38,7 +39,7 @@ class _PrimaitePaths:
self.app_config_file_path = self.generate_app_config_file_path()
self.app_log_dir_path = self.generate_app_log_dir_path()
self.app_log_file_path = self.generate_app_log_file_path()
self.episode_steps_log_file_path = self.generate_episode_step_log_file_path()
self.episode_log_file_path = self.generate_episode_log_file_path()
def _get_dirs_properties(self) -> List[str]:
class_items = self.__class__.__dict__.items()
@@ -106,9 +107,12 @@ class _PrimaitePaths:
"""The PrimAITE app log file path."""
return self.app_log_dir_path / "primaite.log"
def generate_episode_step_log_file_path(self) -> Path:
def generate_episode_log_file_path(self) -> Path:
"""The PrimAITE app episode step log file path."""
return self.app_log_dir_path / "epi_step.log"
date_string = datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
self.episode_log_dir_path = self.app_log_dir_path / date_string
self.episode_log_dir_path.mkdir(exist_ok=True, parents=True)
return self.episode_log_dir_path / "episode.log"
def __repr__(self) -> str:
properties_str = ", ".join([f"{p}='{getattr(self, p)}'" for p in self._get_dirs_properties()])

View File

@@ -1,4 +1,4 @@
import os
# import os
from typing import Any, Dict, Final, Optional, SupportsFloat, Tuple
import gymnasium
@@ -37,11 +37,11 @@ class PrimaiteGymEnv(gymnasium.Env):
dump_state = {self.game.episode_counter: {self.game.step_counter: state}}
# Dump to file
if os.path.isfile(PRIMAITE_PATHS.episode_steps_log_file_path):
with open(PRIMAITE_PATHS.episode_steps_log_file_path, "a", encoding="utf-8") as f:
f.write(str(dump_state))
f.write("\n=================\n")
f.flush()
# if os.path.isfile(PRIMAITE_PATHS.episode_steps_log_file_path):
with open(PRIMAITE_PATHS.episode_log_file_path, "a", encoding="utf-8") as f:
f.write(str(dump_state))
f.write("\n=================\n")
f.flush()
self.game.update_agents(state)