mirror of
https://github.com/OPM/opm-simulators.git
synced 2024-11-25 02:30:18 -06:00
Merge pull request #1698 from fgfuchs/new-feature-deferredlogger
New feature deferredlogger
This commit is contained in:
commit
9c799b9c97
@ -40,6 +40,7 @@ list (APPEND MAIN_SOURCE_FILES
|
||||
opm/core/wells/well_controls.c
|
||||
opm/core/wells/wells.c
|
||||
opm/simulators/WellSwitchingLogger.cpp
|
||||
opm/simulators/DeferredLogger.cpp
|
||||
opm/simulators/timestepping/TimeStepControl.cpp
|
||||
opm/simulators/timestepping/AdaptiveSimulatorTimer.cpp
|
||||
opm/simulators/timestepping/SimulatorTimer.cpp
|
||||
@ -57,6 +58,7 @@ list (APPEND TEST_SOURCE_FILES
|
||||
tests/test_multmatrixtransposed.cpp
|
||||
tests/test_wellmodel.cpp
|
||||
tests/test_wellswitchlogger.cpp
|
||||
tests/test_deferredlogger.cpp
|
||||
tests/test_timer.cpp
|
||||
tests/test_invert.cpp
|
||||
tests/test_wells.cpp
|
||||
@ -162,6 +164,7 @@ list (APPEND PUBLIC_HEADER_FILES
|
||||
opm/core/wells/WellsManager_impl.hpp
|
||||
opm/simulators/ParallelFileMerger.hpp
|
||||
opm/simulators/WellSwitchingLogger.hpp
|
||||
opm/simulators/DeferredLogger.hpp
|
||||
opm/simulators/timestepping/AdaptiveSimulatorTimer.hpp
|
||||
opm/simulators/timestepping/AdaptiveTimeSteppingEbos.hpp
|
||||
opm/simulators/timestepping/ConvergenceReport.hpp
|
||||
|
91
opm/simulators/DeferredLogger.cpp
Normal file
91
opm/simulators/DeferredLogger.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
Copyright 2018 SINTEF Digital, Mathematics and Cybernetics.
|
||||
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <opm/simulators/DeferredLogger.hpp>
|
||||
|
||||
namespace Opm
|
||||
{
|
||||
|
||||
void DeferredLogger::info(const std::string& tag, const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Info, tag, message});
|
||||
}
|
||||
void DeferredLogger::warning(const std::string& tag, const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Warning, tag, message});
|
||||
}
|
||||
void DeferredLogger::error(const std::string& tag, const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Error, tag, message});
|
||||
}
|
||||
void DeferredLogger::problem(const std::string& tag, const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Problem, tag, message});
|
||||
}
|
||||
void DeferredLogger::bug(const std::string& tag, const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Bug, tag, message});
|
||||
}
|
||||
void DeferredLogger::debug(const std::string& tag, const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Debug, tag, message});
|
||||
}
|
||||
void DeferredLogger::note(const std::string& tag, const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Note, tag, message});
|
||||
}
|
||||
|
||||
void DeferredLogger::info(const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Info, "", message});
|
||||
}
|
||||
void DeferredLogger::warning(const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Warning, "", message});
|
||||
}
|
||||
void DeferredLogger::error(const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Error, "", message});
|
||||
}
|
||||
void DeferredLogger::problem(const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Problem, "", message});
|
||||
}
|
||||
void DeferredLogger::bug(const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Bug, "", message});
|
||||
}
|
||||
void DeferredLogger::debug(const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Debug, "", message});
|
||||
}
|
||||
void DeferredLogger::note(const std::string& message)
|
||||
{
|
||||
messages_.push_back({Log::MessageType::Note, "", message});
|
||||
}
|
||||
|
||||
void DeferredLogger::logMessages()
|
||||
{
|
||||
for (const auto& m : messages_) {
|
||||
OpmLog::addTaggedMessage(m.flag, m.tag, m.text);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Opm
|
69
opm/simulators/DeferredLogger.hpp
Normal file
69
opm/simulators/DeferredLogger.hpp
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
Copyright 2018 SINTEF Digital, Mathematics and Cybernetics.
|
||||
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef OPM_DEFERREDLOGGER_HEADER_INCLUDED
|
||||
#define OPM_DEFERREDLOGGER_HEADER_INCLUDED
|
||||
|
||||
#include <opm/common/OpmLog/OpmLog.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Opm
|
||||
{
|
||||
/** This class implements a deferred logger:
|
||||
* 1) messages can be pushed back to a vector
|
||||
* 2) a call to logMessages adds the messages to OpmLog backends
|
||||
* */
|
||||
|
||||
class DeferredLogger
|
||||
{
|
||||
public:
|
||||
void info(const std::string& tag, const std::string& message);
|
||||
void warning(const std::string& tag, const std::string& message);
|
||||
void error(const std::string& tag, const std::string& message);
|
||||
void problem(const std::string& tag, const std::string& message);
|
||||
void bug(const std::string& tag, const std::string& message);
|
||||
void debug(const std::string& tag, const std::string& message);
|
||||
void note(const std::string& tag, const std::string& message);
|
||||
|
||||
void info(const std::string& message);
|
||||
void warning(const std::string& message);
|
||||
void error(const std::string& message);
|
||||
void problem(const std::string& message);
|
||||
void bug(const std::string& message);
|
||||
void debug(const std::string& message);
|
||||
void note(const std::string& message);
|
||||
|
||||
void logMessages();
|
||||
|
||||
private:
|
||||
struct Message
|
||||
{
|
||||
int64_t flag;
|
||||
std::string tag;
|
||||
std::string text;
|
||||
};
|
||||
std::vector<Message> messages_;
|
||||
};
|
||||
|
||||
} // namespace Opm
|
||||
|
||||
#endif // OPM_DEFERREDLOGGER_HEADER_INCLUDED
|
100
tests/test_deferredlogger.cpp
Normal file
100
tests/test_deferredlogger.cpp
Normal file
@ -0,0 +1,100 @@
|
||||
/*
|
||||
Copyright 2018 SINTEF Digital, Mathematics and Cybernetics.
|
||||
Copyright 2018 Equinor.
|
||||
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#define BOOST_TEST_MODULE TestDeferredLogger
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include "opm/simulators/DeferredLogger.hpp"
|
||||
|
||||
#include <opm/common/OpmLog/OpmLog.hpp>
|
||||
#include <opm/common/OpmLog/LogBackend.hpp>
|
||||
#include <opm/common/OpmLog/CounterLog.hpp>
|
||||
#include <opm/common/OpmLog/TimerLog.hpp>
|
||||
#include <opm/common/OpmLog/StreamLog.hpp>
|
||||
#include <opm/common/OpmLog/LogUtil.hpp>
|
||||
|
||||
using namespace Opm;
|
||||
|
||||
void initLogger(std::ostringstream& log_stream) {
|
||||
OpmLog::removeAllBackends();
|
||||
std::shared_ptr<CounterLog> counter = std::make_shared<CounterLog>();
|
||||
std::shared_ptr<StreamLog> streamLog = std::make_shared<StreamLog>( log_stream , Log::DefaultMessageTypes);
|
||||
|
||||
OpmLog::addBackend("COUNTER" , counter);
|
||||
OpmLog::addBackend("STREAM" , streamLog);
|
||||
BOOST_CHECK_EQUAL( true , OpmLog::hasBackend("COUNTER"));
|
||||
BOOST_CHECK_EQUAL( true , OpmLog::hasBackend("STREAM"));
|
||||
|
||||
streamLog->setMessageFormatter(std::make_shared<SimpleMessageFormatter>(true, false));
|
||||
streamLog->setMessageLimiter(std::make_shared<MessageLimiter>(2));
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(deferredlogger)
|
||||
{
|
||||
|
||||
const std::string expected = Log::prefixMessage(Log::MessageType::Info, "info 1") + "\n"
|
||||
+ Log::prefixMessage(Log::MessageType::Warning, "warning 1") + "\n"
|
||||
+ Log::prefixMessage(Log::MessageType::Error, "error 1") + "\n"
|
||||
+ Log::prefixMessage(Log::MessageType::Error, "error 2") + "\n"
|
||||
+ Log::prefixMessage(Log::MessageType::Problem, "problem 1") + "\n"
|
||||
+ Log::prefixMessage(Log::MessageType::Bug, "bug 1") + "\n"
|
||||
+ Log::prefixMessage(Log::MessageType::Debug, "debug 1") + "\n"
|
||||
+ Log::prefixMessage(Log::MessageType::Note, "note 1") + "\n"
|
||||
+ Log::prefixMessage(Log::MessageType::Note, "note 2") + "\n"
|
||||
+ Log::prefixMessage(Log::MessageType::Note, "note 3") + "\n"
|
||||
+ Log::prefixMessage(Log::MessageType::Note, "Message limit reached for message tag: tagme") + "\n";
|
||||
|
||||
std::ostringstream log_stream;
|
||||
initLogger(log_stream);
|
||||
auto deferredlogger = Opm::DeferredLogger();
|
||||
deferredlogger.info("info 1");
|
||||
deferredlogger.warning("warning 1");
|
||||
deferredlogger.error("error 1");
|
||||
deferredlogger.error("error 2");
|
||||
deferredlogger.problem("problem 1");
|
||||
deferredlogger.bug("bug 1");
|
||||
deferredlogger.debug("debug 1");
|
||||
deferredlogger.note("note 1");
|
||||
deferredlogger.note("tagme", "note 2");
|
||||
deferredlogger.note("tagme", "note 3");
|
||||
deferredlogger.note("tagme", "note 3");
|
||||
deferredlogger.note("tagme", "note 3");
|
||||
deferredlogger.note("tagme", "note 3");
|
||||
deferredlogger.note("tagme", "note 3");
|
||||
deferredlogger.note("tagme", "note 3");
|
||||
|
||||
deferredlogger.logMessages();
|
||||
|
||||
auto counter = OpmLog::getBackend<CounterLog>("COUNTER");
|
||||
BOOST_CHECK_EQUAL( 1 , counter->numMessages(Log::MessageType::Warning) );
|
||||
BOOST_CHECK_EQUAL( 1 , counter->numMessages(Log::MessageType::Info) );
|
||||
BOOST_CHECK_EQUAL( 2 , counter->numMessages(Log::MessageType::Error) );
|
||||
BOOST_CHECK_EQUAL( 1 , counter->numMessages(Log::MessageType::Problem) );
|
||||
BOOST_CHECK_EQUAL( 1 , counter->numMessages(Log::MessageType::Bug) );
|
||||
BOOST_CHECK_EQUAL( 1 , counter->numMessages(Log::MessageType::Debug) );
|
||||
BOOST_CHECK_EQUAL( 8 , counter->numMessages(Log::MessageType::Note) );
|
||||
|
||||
BOOST_CHECK_EQUAL(log_stream.str(), expected);
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user