Skip to content

Commit

Permalink
Declare PyRunModule a friend of OpmLog and set inner Logger of the Op…
Browse files Browse the repository at this point in the history
…mLog Class that is exposed to Python
  • Loading branch information
lisajulia committed Jan 21, 2025
1 parent e93d9fa commit 4b55ea1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions opm/common/OpmLog/OpmLog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
namespace Opm {

class LogBackend;
#ifdef EMBEDDED_PYTHON
class PyRunModule;
#endif

/*
The OpmLog class is a fully static class which manages a proper
Expand Down Expand Up @@ -107,6 +110,9 @@ class OpmLog {
static bool setLogger(const std::shared_ptr<Logger>& logger);

private:
#ifdef EMBEDDED_PYTHON
friend class PyRunModule;
#endif
static std::shared_ptr<Logger> getLogger();
static std::shared_ptr<Logger> m_logger;
};
Expand Down
2 changes: 2 additions & 0 deletions opm/input/eclipse/Python/PyRunModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ PyRunModule::PyRunModule(std::shared_ptr<const Python> python, const std::string
// opm_embedded needs to be loaded before user defined module.
try {
this->opm_embedded = py::module::import("opm_embedded");
//Set the inner Logger of OpmLog, if we don't set this manually, any log messages from the Python script will not reach OpmLog
this->opm_embedded.attr("OpmLog").attr("_sL")(py::cast(OpmLog::getLogger()));
} catch (const std::exception& e) {
OpmLog::error(fmt::format("Exception thrown when loading Python module opm_embedded: {}. Possibly the PYTHONPATH of the system is not set correctly.", e.what()));
throw e;
Expand Down
5 changes: 4 additions & 1 deletion python/cxx/log.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <string>

#include <opm/common/OpmLog/OpmLog.hpp>
#include <opm/common/OpmLog/Logger.hpp>

#include "export.hpp"

Expand Down Expand Up @@ -38,10 +39,12 @@ void note(const std::string& msg) {

void python::common::export_Log(py::module& module)
{
py::class_<OpmLog, std::shared_ptr<OpmLog> >( module, "OpmLog", R"(
py::class_<Logger>(module, "Logger");
py::class_<OpmLog>( module, "OpmLog", R"(
The Opm::OpmLog class - this is a fully static class which manages a proper
Logger instance.
)")
.def_static("_sL", &OpmLog::setLogger, "Add an info message to the opm log.")
.def_static("info", info, "Add an info message to the opm log.")
.def_static("warning", warning, "Add a warning message to the opm log.")
.def_static("error", error, "Add an error message to the opm log.")
Expand Down

0 comments on commit 4b55ea1

Please sign in to comment.