Files
PrimAITE/src/primaite/simulator/__init__.py

60 lines
2.1 KiB
Python
Raw Normal View History

2023-11-17 10:20:26 +00:00
"""Warning: SIM_OUTPUT is a mutable global variable for the simulation output directory."""
from datetime import datetime
2024-04-18 16:38:42 +01:00
from enum import IntEnum
2023-11-17 10:20:26 +00:00
from pathlib import Path
from primaite import _PRIMAITE_ROOT
2023-11-17 10:20:26 +00:00
__all__ = ["SIM_OUTPUT"]
2024-04-29 13:45:10 +01:00
from primaite.utils.cli.primaite_config_utils import get_primaite_config_dict, is_dev_mode
2023-11-17 10:20:26 +00:00
2024-04-18 16:38:42 +01:00
class LogLevel(IntEnum):
"""Enum containing all the available log levels for PrimAITE simulation output."""
2024-04-18 16:38:42 +01:00
DEBUG = 10
"""Debug items will be output to terminal or log file."""
2024-04-18 16:38:42 +01:00
INFO = 20
"""Info items will be output to terminal or log file."""
2024-04-18 16:38:42 +01:00
WARNING = 30
"""Warnings will be output to terminal or log file."""
2024-04-18 16:38:42 +01:00
ERROR = 40
"""Errors will be output to terminal or log file."""
2024-04-18 16:38:42 +01:00
CRITICAL = 50
"""Critical errors will be output to terminal or log file."""
2024-01-05 11:25:57 +00:00
class _SimOutput:
2024-04-29 13:45:10 +01:00
_default_path = _PRIMAITE_ROOT.parent.parent / "simulation_output" / datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
2023-11-17 10:20:26 +00:00
def __init__(self):
2024-04-29 13:45:10 +01:00
self._path: Path = self._default_path
self.save_pcap_logs: bool = False
self.save_sys_logs: bool = False
2024-04-15 11:50:08 +01:00
self.write_sys_log_to_terminal: bool = False
self.sys_log_level: LogLevel = LogLevel.WARNING # default log level is at WARNING
2023-11-17 10:20:26 +00:00
2024-04-29 13:45:10 +01:00
if is_dev_mode():
# if dev mode, override with the values configured via the primaite dev-mode command
dev_config = get_primaite_config_dict().get("developer_mode")
self.save_pcap_logs = dev_config["output_pcap_logs"]
self.save_sys_logs = dev_config["output_sys_logs"]
self.write_sys_log_to_terminal = dev_config["output_to_terminal"]
2023-11-17 10:20:26 +00:00
@property
def path(self) -> Path:
2024-04-29 13:45:10 +01:00
if not is_dev_mode():
return self._path
if is_dev_mode():
dev_config = get_primaite_config_dict().get("developer_mode")
return Path(dev_config["output_dir"]) if dev_config["output_dir"] else self._default_path
2023-11-17 10:20:26 +00:00
@path.setter
def path(self, new_path: Path) -> None:
self._path = new_path
self._path.mkdir(exist_ok=True, parents=True)
2024-01-05 11:25:57 +00:00
SIM_OUTPUT = _SimOutput()