From c9ace096be6741f1ccdc1cbac3a7eaefff18b045 Mon Sep 17 00:00:00 2001 From: grufoony Date: Wed, 4 Feb 2026 13:46:18 +0100 Subject: [PATCH 1/2] Better Dynamics constructor --- src/dsf/base/Dynamics.hpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/dsf/base/Dynamics.hpp b/src/dsf/base/Dynamics.hpp index d1782939..b2e98f5a 100644 --- a/src/dsf/base/Dynamics.hpp +++ b/src/dsf/base/Dynamics.hpp @@ -43,8 +43,9 @@ namespace dsf { class Dynamics { private: network_t m_graph; - std::string m_name; - std::time_t m_timeInit, m_timeStep; + std::string m_name = "unnamed simulation"; + std::time_t m_timeInit = 0; + std::time_t m_timeStep = 0; protected: tbb::task_arena m_taskArena; @@ -120,14 +121,10 @@ namespace dsf { template Dynamics::Dynamics(network_t& graph, std::optional seed) - : m_graph{std::move(graph)}, - m_name{"unnamed simulation"}, - m_timeInit{0}, - m_timeStep{0}, - m_generator{std::random_device{}()} { + : m_graph{std::move(graph)}, m_generator{std::random_device{}()} { if (seed.has_value()) { m_generator.seed(*seed); } m_taskArena.initialize(); } -}; // namespace dsf +}; // namespace dsf \ No newline at end of file From 10dfeaa5dd4b88b3f74a10b030065e71751f52cc Mon Sep 17 00:00:00 2001 From: grufoony Date: Wed, 4 Feb 2026 14:27:34 +0100 Subject: [PATCH 2/2] Add `log_to_file` function --- src/dsf/__init__.py | 10 +++++++++- src/dsf/bindings.cpp | 6 ++++++ src/dsf/dsf.hpp | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/dsf/__init__.py b/src/dsf/__init__.py index b3b8743e..2de57dc6 100644 --- a/src/dsf/__init__.py +++ b/src/dsf/__init__.py @@ -1,7 +1,15 @@ import sys import os -from dsf_cpp import __version__, LogLevel, get_log_level, set_log_level, mobility, mdt +from dsf_cpp import ( + __version__, + LogLevel, + get_log_level, + set_log_level, + log_to_file, + mobility, + mdt, +) from .python.cartography import ( get_cartography, diff --git a/src/dsf/bindings.cpp b/src/dsf/bindings.cpp index b2e6d786..4a5778e9 100644 --- a/src/dsf/bindings.cpp +++ b/src/dsf/bindings.cpp @@ -13,6 +13,12 @@ PYBIND11_MODULE(dsf_cpp, m) { m.doc() = "Python bindings for the DSF library"; m.attr("__version__") = dsf::version(); + // Bind the dsf::log_to_file function + m.def("log_to_file", + &dsf::log_to_file, + pybind11::arg("path"), + "Set up logging to a specified file"); + // Create mobility submodule auto mobility = m.def_submodule("mobility", "Bindings for mobility-related classes and functions, " diff --git a/src/dsf/dsf.hpp b/src/dsf/dsf.hpp index 6fbd08da..61a30e40 100644 --- a/src/dsf/dsf.hpp +++ b/src/dsf/dsf.hpp @@ -4,6 +4,9 @@ #include #include +#include +#include + static constexpr uint8_t DSF_VERSION_MAJOR = 4; static constexpr uint8_t DSF_VERSION_MINOR = 7; static constexpr uint8_t DSF_VERSION_PATCH = 8; @@ -15,6 +18,18 @@ namespace dsf { /// @brief Returns the version of the DSF library /// @return The version of the DSF library auto const& version() { return DSF_VERSION; }; + + /// @brief Set up logging to a specified file + /// @param path The path to the log file + void log_to_file(std::string const& path) { + try { + spdlog::info("Logging to file: {}", path); + auto file_logger = spdlog::basic_logger_mt("dsf_file_logger", path); + spdlog::set_default_logger(file_logger); + } catch (const spdlog::spdlog_ex& ex) { + spdlog::error("Log initialization failed: {}", ex.what()); + } + }; } // namespace dsf #include "base/AdjacencyMatrix.hpp"