Skip to content

Commit

Permalink
Add setLogger function to OpmLog
Browse files Browse the repository at this point in the history
  • Loading branch information
lisajulia committed Jan 21, 2025
1 parent 07fb927 commit e93d9fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions opm/common/OpmLog/OpmLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ namespace Opm {
}
}

bool OpmLog::setLogger(const std::shared_ptr<Logger>& logger) {
if (!m_logger) {
m_logger = logger;
return true;
} else if (m_logger.get() == logger.get()){
return true;
} else {
return false; // Logger was set to something else
}
}

std::shared_ptr<Logger> OpmLog::getLogger() {
if (!m_logger)
Expand Down
16 changes: 16 additions & 0 deletions opm/common/OpmLog/OpmLog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ class OpmLog {

static bool stdoutIsTerminal();

/**
* @brief Sets the global logger instance for the `OpmLog` class.
*
* This static function allows setting a shared pointer to a `Logger` instance
* as the global logger. It ensures that the logger is set only if it has not
* been previously set or if the provided logger is the same as the existing one.
* This function prevents changing the logger once it has been set to a different instance.
* If the logger is already set to a different instance, the function will return `false`.
*
* @param logger A `std::shared_ptr` to a `Logger` instance that will be set as the global logger.
*
* @return `true` if the logger was successfully set or if the same logger was already set.
* `false` if a different logger had already been set and the new logger could not be set.
*/
static bool setLogger(const std::shared_ptr<Logger>& logger);

private:
static std::shared_ptr<Logger> getLogger();
static std::shared_ptr<Logger> m_logger;
Expand Down

0 comments on commit e93d9fa

Please sign in to comment.