Moved in logfunctionality from opm-parser

This commit is contained in:
chflo 2016-03-17 12:03:07 +01:00
parent 0d3d46e199
commit 73ad043006
19 changed files with 1332 additions and 4 deletions

View File

@ -22,11 +22,20 @@
list (APPEND MAIN_SOURCE_FILES
opm/common/data/SimulationDataContainer.cpp
opm/common/OpmLog/CounterLog.cpp
opm/common/OpmLog/EclipsePRTLog.cpp
opm/common/OpmLog/LogBackend.cpp
opm/common/OpmLog/Logger.cpp
opm/common/OpmLog/LogUtil.cpp
opm/common/OpmLog/OpmLog.cpp
opm/common/OpmLog/StreamLog.cpp
opm/common/OpmLog/TimerLog.cpp
)
list (APPEND TEST_SOURCE_FILES
tests/test_SimulationDataContainer.cpp
tests/test_cmp.cpp
tests/test_OpmLog.cpp
)
list (APPEND TEST_DATA_FILES
@ -42,9 +51,17 @@ list (APPEND PROGRAM_SOURCE_FILES
list( APPEND PUBLIC_HEADER_FILES
opm/common/data/SimulationDataContainer.hpp
opm/common/util/numeric/cmp.hpp
opm/common/ErrorMacros.hpp
opm/common/Exceptions.hpp
opm/common/data/SimulationDataContainer.hpp
opm/common/OpmLog/CounterLog.hpp
opm/common/OpmLog/EclipsePRTLog.hpp
opm/common/OpmLog/LogBackend.hpp
opm/common/OpmLog/Logger.hpp
opm/common/OpmLog/LogUtil.hpp
opm/common/OpmLog/OpmLog.hpp
opm/common/OpmLog/StreamLog.hpp
opm/common/OpmLog/TimerLog.hpp
opm/common/util/numeric/cmp.hpp
opm/common/utility/platform_dependent/disable_warnings.h
opm/common/utility/platform_dependent/reenable_warnings.h )
opm/common/utility/platform_dependent/reenable_warnings.h)

View File

@ -16,8 +16,8 @@ set (opm-material_DEPS
# compile with C++0x/11 support if available
"CXX11Features REQUIRED"
# prerequisite OPM modules
"opm-common"
"opm-parser"
"opm-common"
# DUNE dependency
"dune-common REQUIRED"
)

View File

@ -0,0 +1,64 @@
/*
Copyright 2014 Andreas Lauser
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 <stdexcept>
#include <sstream>
#include <cassert>
#include <opm/common/OpmLog/OpmLog.hpp>
#include <opm/common/OpmLog/LogUtil.hpp>
#include <opm/common/OpmLog/CounterLog.hpp>
namespace Opm {
CounterLog::CounterLog(int64_t messageTypes) : LogBackend(messageTypes)
{ }
CounterLog::CounterLog() : LogBackend(Log::DefaultMessageTypes)
{ }
size_t CounterLog::numMessages(int64_t messageType) const {
if (Log::isPower2( messageType )) {
auto iter = m_count.find( messageType );
if (iter == m_count.end())
return 0;
else
return (*iter).second;
} else
throw std::invalid_argument("The messageType ID must be 2^n");
}
void CounterLog::addMessage(int64_t messageType , const std::string& ) {
if (includeMessage( messageType ))
m_count[messageType]++;
}
void CounterLog::clear()
{
m_count.clear();
}
} // namespace Opm

View File

@ -0,0 +1,57 @@
/*
Copyright 2014 Andreas Lauser
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_COUNTERLOG_HPP
#define OPM_COUNTERLOG_HPP
#include <string>
#include <memory>
#include <map>
#include <opm/common/OpmLog/LogBackend.hpp>
namespace Opm {
/*!
* \brief Provides a simple sytem for log message which are found by the
* Parser/Deck/EclipseState classes during processing the deck.
*/
class CounterLog : public LogBackend {
public:
CounterLog(int64_t messageMask);
CounterLog();
size_t numMessages(int64_t messageType) const;
void addMessage(int64_t messageFlag ,
const std::string& message);
void clear();
~CounterLog() {};
private:
std::map<int64_t , size_t> m_count;
};
typedef std::shared_ptr<CounterLog> CounterLogPtr;
typedef std::shared_ptr<const CounterLog> CounterLogConstPtr;
} // namespace Opm
#endif

View File

@ -0,0 +1,59 @@
/*
Copyright 2016 Statoil ASA.
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/common/OpmLog/EclipsePRTLog.hpp>
#include <opm/common/OpmLog/LogUtil.hpp>
namespace Opm {
void EclipsePRTLog::addMessage(int64_t messageType, const std::string& message)
{
StreamLog::addMessage(messageType, message);
m_count[messageType]++;
}
size_t EclipsePRTLog::numMessages(int64_t messageType) const
{
if (Log::isPower2( messageType )) {
auto iter = m_count.find( messageType );
if (iter == m_count.end())
return 0;
else
return (*iter).second;
} else
throw std::invalid_argument("The messageType ID must be 2^n");
}
EclipsePRTLog::~EclipsePRTLog()
{
//output summary.
const std::string summary_msg = "\n\nError summary:" +
std::string("\nWarnings " + std::to_string(numMessages(Log::MessageType::Warning))) +
std::string("\nProblems " + std::to_string(numMessages(Log::MessageType::Problem))) +
std::string("\nErrors " + std::to_string(numMessages(Log::MessageType::Error))) +
std::string("\nBugs " + std::to_string(numMessages(Log::MessageType::Bug))) +
std::string("\nDebug " + std::to_string(numMessages(Log::MessageType::Debug))) +
std::string("\nProblems " + std::to_string(numMessages(Log::MessageType::Problem))) +"\n";
addMessage(Log::MessageType::Info, summary_msg);
}
}

View File

@ -0,0 +1,44 @@
/*
Copyright 2016 Statoil ASA.
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 ECLIPSEPRTLOG_H
#define ECLIPSEPRTLOG_H
#include <map>
#include <string>
#include <opm/common/OpmLog/StreamLog.hpp>
namespace Opm {
class EclipsePRTLog : public StreamLog {
public:
using StreamLog::StreamLog;
void addMessage(int64_t messageType, const std::string& message);
size_t numMessages(int64_t messageType) const;
~EclipsePRTLog();
private:
std::map<int64_t, size_t> m_count;
};
}
#endif // ECLIPSEPRTLOG_H

View File

@ -0,0 +1,44 @@
/*
Copyright 2015 Statoil ASA.
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 <cstdint>
#include <opm/common/OpmLog/LogBackend.hpp>
namespace Opm {
LogBackend::LogBackend( int64_t mask ) :
m_mask(mask)
{
}
bool LogBackend::includeMessage(int64_t messageFlag) {
if (((messageFlag & m_mask) == messageFlag) &&
(messageFlag > 0))
return true;
else
return false;
}
int64_t LogBackend::getMask() const {
return m_mask;
}
}

View File

@ -0,0 +1,45 @@
/*
Copyright 2015 Statoil ASA.
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_LOGBACKEND_HPP
#define OPM_LOGBACKEND_HPP
#include <cstdint>
#include <string>
namespace Opm {
class LogBackend {
public:
LogBackend( int64_t mask );
virtual ~LogBackend() { };
virtual void addMessage(int64_t , const std::string& ) { };
int64_t getMask() const;
bool includeMessage(int64_t messageFlag);
private:
int64_t m_mask;
};
}
#endif

View File

@ -0,0 +1,76 @@
/*
Copyright 2015 Statoil ASA.
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 <sstream>
#include <stdexcept>
#include <opm/common/OpmLog/LogUtil.hpp>
namespace Opm {
namespace Log {
bool isPower2(int64_t x) {
return ((x != 0) && !(x & (x - 1)));
}
std::string fileMessage(const std::string& filename , int line , const std::string& message) {
std::ostringstream oss;
oss << filename << ":" << line << ": " << message;
return oss.str();
}
std::string fileMessage(int64_t messageType , const std::string& filename , int line , const std::string& message) {
return fileMessage( filename , line , prefixMessage( messageType , message ));
}
std::string prefixMessage(int64_t messageType, const std::string& message) {
std::string prefix;
switch (messageType) {
case MessageType::Debug:
prefix = "debug";
break;
case MessageType::Info:
prefix = "info";
break;
case MessageType::Warning:
prefix = "warning";
break;
case MessageType::Error:
prefix = "error";
break;
case MessageType::Problem:
prefix = "problem";
break;
case MessageType::Bug:
prefix = "bug";
break;
default:
throw std::invalid_argument("Unhandled messagetype");
}
return prefix + ": " + message;
}
}
}

View File

@ -0,0 +1,47 @@
/*
Copyright 2015 Statoil ASA.
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_LOG_UTIL_HPP
#define OPM_LOG_UTIL_HPP
#include <cstdint>
#include <string>
namespace Opm {
namespace Log {
namespace MessageType {
const int64_t Debug = 1; /* Excessive information */
const int64_t Info = 2; /* Normal status information */
const int64_t Warning = 4; /* Input anomaly - possible error */
const int64_t Error = 8; /* Error in the input data - should probably exit. */
const int64_t Problem = 16; /* Calculation problems - e.g. convergence failure. */
const int64_t Bug = 32; /* An inconsistent state has been encountered in the simulator - should probably exit. */
}
const int64_t DefaultMessageTypes = MessageType::Debug + MessageType::Info + MessageType::Warning + MessageType::Error + MessageType::Problem + MessageType::Bug;
bool isPower2(int64_t x);
std::string fileMessage(const std::string& path, int line , const std::string& msg);
std::string fileMessage(int64_t messageType , const std::string& path, int line , const std::string& msg);
std::string prefixMessage(int64_t messageType , const std::string& msg);
}
}
#endif

View File

@ -0,0 +1,114 @@
/*
Copyright 2015 Statoil ASA.
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 <sstream>
#include <stdexcept>
#include <iostream>
#include <opm/common/OpmLog/LogBackend.hpp>
#include <opm/common/OpmLog/Logger.hpp>
#include <opm/common/OpmLog/LogUtil.hpp>
namespace Opm {
Logger::Logger()
: m_globalMask(0),
m_enabledTypes(0)
{
addMessageType( Log::MessageType::Debug , "debug");
addMessageType( Log::MessageType::Info , "info");
addMessageType( Log::MessageType::Warning , "warning");
addMessageType( Log::MessageType::Error , "error");
addMessageType( Log::MessageType::Problem , "problem");
addMessageType( Log::MessageType::Bug , "bug");
}
void Logger::addMessage(int64_t messageType , const std::string& message) const {
if ((m_enabledTypes & messageType) == 0)
throw std::invalid_argument("Tried to issue message with unrecognized message ID");
if (m_globalMask & messageType) {
for (auto iter = m_backends.begin(); iter != m_backends.end(); ++iter) {
std::shared_ptr<LogBackend> backend = (*iter).second;
backend->addMessage( messageType , message );
}
}
}
void Logger::updateGlobalMask( int64_t mask ) {
m_globalMask |= mask;
}
bool Logger::hasBackend(const std::string& name) {
if (m_backends.find( name ) == m_backends.end())
return false;
else
return true;
}
bool Logger::removeBackend(const std::string& name) {
size_t eraseCount = m_backends.erase( name );
if (eraseCount == 1)
return true;
else
return false;
}
void Logger::addBackend(const std::string& name , std::shared_ptr<LogBackend> backend) {
updateGlobalMask( backend->getMask() );
m_backends[ name ] = backend;
}
int64_t Logger::enabledMessageTypes() const {
return m_enabledTypes;
}
//static:
bool Logger::enabledMessageType( int64_t enabledTypes , int64_t messageType) {
if (Log::isPower2( messageType)) {
if ((messageType & enabledTypes) == 0)
return false;
else
return true;
} else
throw std::invalid_argument("The message type id must be ~ 2^n");
}
//static:
bool Logger::enabledDefaultMessageType( int64_t messageType) {
return enabledMessageType( Log::DefaultMessageTypes , messageType );
}
bool Logger::enabledMessageType( int64_t messageType) const {
return enabledMessageType( m_enabledTypes , messageType );
}
void Logger::addMessageType( int64_t messageType , const std::string& /* prefix */) {
if (Log::isPower2( messageType)) {
m_enabledTypes |= messageType;
} else
throw std::invalid_argument("The message type id must be ~ 2^n");
}
}

View File

@ -0,0 +1,80 @@
/*
Copyright 2015 Statoil ASA.
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_LOGGER_HPP
#define OPM_LOGGER_HPP
#include <stdexcept>
#include <cstdint>
#include <map>
#include <memory>
#include <string>
namespace Opm {
class LogBackend;
class Logger {
public:
Logger();
void addMessage(int64_t messageType , const std::string& message) const;
static bool enabledDefaultMessageType( int64_t messageType);
bool enabledMessageType( int64_t messageType) const;
void addMessageType( int64_t messageType , const std::string& prefix);
int64_t enabledMessageTypes() const;
void addBackend(const std::string& name , std::shared_ptr<LogBackend> backend);
bool hasBackend(const std::string& name);
bool removeBackend(const std::string& name);
template <class BackendType>
std::shared_ptr<BackendType> getBackend(const std::string& name) const {
auto pair = m_backends.find( name );
if (pair == m_backends.end())
throw std::invalid_argument("Invalid backend name: " + name);
else
return std::static_pointer_cast<BackendType>(m_backends.find(name)->second);
}
template <class BackendType>
std::shared_ptr<BackendType> popBackend(const std::string& name) {
auto pair = m_backends.find( name );
if (pair == m_backends.end())
throw std::invalid_argument("Invalid backend name: " + name);
else {
std::shared_ptr<LogBackend> backend = (*pair).second;
removeBackend( name );
return std::static_pointer_cast<BackendType>(backend);
}
}
private:
void updateGlobalMask( int64_t mask );
static bool enabledMessageType( int64_t enabledTypes , int64_t messageType);
int64_t m_globalMask;
int64_t m_enabledTypes;
std::map<std::string , std::shared_ptr<LogBackend> > m_backends;
};
}
#endif

View File

@ -0,0 +1,120 @@
/*
Copyright 2014 Statoil ASA.
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/common/OpmLog/OpmLog.hpp>
#include <opm/common/OpmLog/Logger.hpp>
namespace Opm {
std::shared_ptr<Logger> OpmLog::getLogger() {
if (!m_logger)
m_logger.reset( new Logger() );
return m_logger;
}
void OpmLog::addMessage(int64_t messageFlag , const std::string& message) {
if (m_logger)
m_logger->addMessage( messageFlag , message );
}
void OpmLog::info(const std::string& message)
{
const std::string msg = Log::prefixMessage(Log::MessageType::Info, message);
addMessage(Log::MessageType::Info, msg);
}
void OpmLog::warning(const std::string& message)
{
const std::string msg = Log::prefixMessage(Log::MessageType::Warning, message);
addMessage(Log::MessageType::Warning, msg);
}
void OpmLog::problem(const std::string& message)
{
const std::string msg = Log::prefixMessage(Log::MessageType::Problem, message);
addMessage(Log::MessageType::Problem, msg);
}
void OpmLog::error(const std::string& message)
{
const std::string msg = Log::prefixMessage(Log::MessageType::Error, message);
addMessage(Log::MessageType::Error, msg);
}
void OpmLog::bug(const std::string& message)
{
const std::string msg = Log::prefixMessage(Log::MessageType::Bug, message);
addMessage(Log::MessageType::Bug, msg);
}
void OpmLog::debug(const std::string& message)
{
const std::string msg = Log::prefixMessage(Log::MessageType::Debug, message);
addMessage(Log::MessageType::Debug, msg);
}
bool OpmLog::enabledMessageType( int64_t messageType ) {
if (m_logger)
return m_logger->enabledMessageType( messageType );
else
return Logger::enabledDefaultMessageType( messageType );
}
bool OpmLog::hasBackend(const std::string& name) {
if (m_logger)
return m_logger->hasBackend( name );
else
return false;
}
bool OpmLog::removeBackend(const std::string& name) {
if (m_logger)
return m_logger->removeBackend( name );
else
return false;
}
void OpmLog::addMessageType( int64_t messageType , const std::string& prefix) {
auto logger = OpmLog::getLogger();
logger->addMessageType( messageType , prefix );
}
void OpmLog::addBackend(const std::string& name , std::shared_ptr<LogBackend> backend) {
auto logger = OpmLog::getLogger();
return logger->addBackend( name , backend );
}
/******************************************************************/
std::shared_ptr<Logger> OpmLog::m_logger;
}

View File

@ -0,0 +1,81 @@
/*
Copyright 2014 Statoil ASA.
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 OPMLOG_HPP
#define OPMLOG_HPP
#include <memory>
#include <cstdint>
#include <opm/common/OpmLog/Logger.hpp>
#include <opm/common/OpmLog/LogUtil.hpp>
namespace Opm {
class LogBackend;
/*
The OpmLog class is a fully static class which manages a proper
Logger instance.
*/
class OpmLog {
public:
static void addMessage(int64_t messageFlag , const std::string& message);
static void info(const std::string& message);
static void warning(const std::string& message);
static void error(const std::string& message);
static void problem(const std::string& message);
static void bug(const std::string& message);
static void debug(const std::string& message);
static bool hasBackend( const std::string& backendName );
static void addBackend(const std::string& name , std::shared_ptr<LogBackend> backend);
static bool removeBackend(const std::string& name);
static bool enabledMessageType( int64_t messageType );
static void addMessageType( int64_t messageType , const std::string& prefix);
template <class BackendType>
static std::shared_ptr<BackendType> getBackend(const std::string& name) {
auto logger = OpmLog::getLogger();
return logger->getBackend<BackendType>(name);
}
template <class BackendType>
static std::shared_ptr<BackendType> popBackend(const std::string& name) {
auto logger = OpmLog::getLogger();
return logger->popBackend<BackendType>(name);
}
private:
static std::shared_ptr<Logger> getLogger();
static std::shared_ptr<Logger> m_logger;
};
}
#endif

View File

@ -0,0 +1,61 @@
/*
Copyright 2015 Statoil ASA.
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 <stdexcept>
#include <opm/common/OpmLog/StreamLog.hpp>
namespace Opm {
StreamLog::StreamLog(const std::string& logFile , int64_t messageMask) : LogBackend(messageMask)
{
m_ofstream.open( logFile.c_str() , std::ofstream::out );
m_streamOwner = true;
m_ostream = &m_ofstream;
}
StreamLog::StreamLog(std::ostream& os , int64_t messageMask) : LogBackend(messageMask)
{
m_ostream = &os;
m_streamOwner = false;
}
void StreamLog::close() {
if (m_streamOwner && m_ofstream.is_open()) {
m_ofstream.close();
m_ostream = NULL;
}
}
void StreamLog::addMessage(int64_t messageType , const std::string& message) {
if (includeMessage( messageType )) {
(*m_ostream) << message << std::endl;
if (m_ofstream.is_open())
m_ofstream.flush();
}
}
StreamLog::~StreamLog() {
close();
}
}

View File

@ -0,0 +1,48 @@
/*
Copyright 2015 Statoil ASA.
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 STREAMLOG_H
#define STREAMLOG_H
#include <fstream>
#include <iostream>
#include <cstdint>
#include <opm/common/OpmLog/LogBackend.hpp>
namespace Opm {
class StreamLog : public LogBackend {
public:
StreamLog(const std::string& logFile , int64_t messageMask);
StreamLog(std::ostream& os , int64_t messageMask);
void addMessage(int64_t messageType , const std::string& message);
~StreamLog();
private:
void close();
std::ofstream m_ofstream;
std::ostream * m_ostream;
bool m_streamOwner;
};
}
#endif

View File

@ -0,0 +1,60 @@
/*
Copyright 2014 Statoil ASA.
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 <stdexcept>
#include <cassert>
#include <iomanip>
#include <opm/common/OpmLog/OpmLog.hpp>
#include <opm/common/OpmLog/LogUtil.hpp>
#include <opm/common/OpmLog/TimerLog.hpp>
#include <opm/common/OpmLog/StreamLog.hpp>
namespace Opm {
TimerLog::TimerLog(const std::string& logFile) : StreamLog( logFile , StopTimer | StartTimer )
{
m_work.precision(8);
}
TimerLog::TimerLog(std::ostream& os) : StreamLog( os , StopTimer | StartTimer )
{
m_work.precision(8);
}
void TimerLog::addMessage(int64_t messageType , const std::string& msg ) {
if (messageType == StopTimer) {
clock_t stop = clock();
double secondsElapsed = 1.0 * (m_start - stop) / CLOCKS_PER_SEC ;
m_work.str("");
m_work << std::fixed << msg << ": " << secondsElapsed << " seconds ";
StreamLog::addMessage( messageType , m_work.str());
} else {
if (messageType == StartTimer)
m_start = clock();
}
}
} // namespace Opm

View File

@ -0,0 +1,60 @@
/*
Copyright 2014 Statoil ASA.
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_TIMERLOG_HPP
#define OPM_TIMERLOG_HPP
#include <time.h>
#include <memory>
#include <sstream>
#include <string>
#include <opm/common/OpmLog/StreamLog.hpp>
/*
This class is a simple demonstration of how the logging framework
can be used to create a simple very special case logging facility.
*/
namespace Opm {
class TimerLog : public StreamLog {
public:
static const int64_t StartTimer = 4096;
static const int64_t StopTimer = 8192;
TimerLog(const std::string& logFile);
TimerLog(std::ostream& os);
void addMessage(int64_t messageFlag ,
const std::string& message);
void clear();
~TimerLog() {};
private:
clock_t m_start;
std::ostringstream m_work;
};
typedef std::shared_ptr<TimerLog> TimerLogPtr;
typedef std::shared_ptr<const TimerLog> TimerLogConstPtr;
} // namespace Opm
#endif

251
tests/test_OpmLog.cpp Normal file
View File

@ -0,0 +1,251 @@
/*
Copyright 2013 Statoil ASA.
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/>.
*/
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE LogTests
#include <opm/common/utility/platform_dependent/disable_warnings.h>
#include <boost/test/unit_test.hpp>
#include <opm/common/utility/platform_dependent/reenable_warnings.h>
#include <stdexcept>
#include <iostream>
#include <sstream>
#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;
BOOST_AUTO_TEST_CASE(DoLogging) {
OpmLog::addMessage(Log::MessageType::Warning , "Warning1");
OpmLog::addMessage(Log::MessageType::Warning , "Warning2");
}
BOOST_AUTO_TEST_CASE(Test_Format) {
BOOST_CHECK_EQUAL( "/path/to/file:100: There is a mild fuckup here?" , Log::fileMessage("/path/to/file" , 100 , "There is a mild fuckup here?"));
BOOST_CHECK_EQUAL( "error: This is the error" , Log::prefixMessage(Log::MessageType::Error , "This is the error"));
BOOST_CHECK_EQUAL( "warning: This is the warning" , Log::prefixMessage(Log::MessageType::Warning , "This is the warning"));
BOOST_CHECK_EQUAL( "info: This is the info" , Log::prefixMessage(Log::MessageType::Info , "This is the info"));
}
BOOST_AUTO_TEST_CASE(Test_AbstractBackend) {
int64_t mask = 1+4+16;
LogBackend backend(mask);
BOOST_CHECK_EQUAL(false , backend.includeMessage(0 ));
BOOST_CHECK_EQUAL(true , backend.includeMessage(1 ));
BOOST_CHECK_EQUAL(false , backend.includeMessage(2 ));
BOOST_CHECK_EQUAL(true , backend.includeMessage(4 ));
BOOST_CHECK_EQUAL(false , backend.includeMessage(8 ));
BOOST_CHECK_EQUAL(true , backend.includeMessage(16 ));
BOOST_CHECK_EQUAL(false, backend.includeMessage(6 ));
BOOST_CHECK_EQUAL(true , backend.includeMessage(5 ));
}
BOOST_AUTO_TEST_CASE(Test_Logger) {
Logger logger;
std::ostringstream log_stream;
std::shared_ptr<CounterLog> counter = std::make_shared<CounterLog>();
std::shared_ptr<StreamLog> streamLog = std::make_shared<StreamLog>( log_stream , Log::MessageType::Warning );
BOOST_CHECK_EQUAL( false , logger.hasBackend("NO"));
logger.addBackend("COUNTER" , counter);
logger.addBackend("STREAM" , streamLog);
BOOST_CHECK_EQUAL( true , logger.hasBackend("COUNTER"));
BOOST_CHECK_EQUAL( true , logger.hasBackend("STREAM"));
logger.addMessage( Log::MessageType::Error , "Error");
logger.addMessage( Log::MessageType::Warning , "Warning");
BOOST_CHECK_EQUAL( 1U , counter->numMessages(Log::MessageType::Error) );
BOOST_CHECK_EQUAL( 1U , counter->numMessages(Log::MessageType::Warning) );
BOOST_CHECK_EQUAL( 0U , counter->numMessages(Log::MessageType::Info) );
BOOST_CHECK_EQUAL( log_stream.str() , "Warning\n");
BOOST_CHECK_THROW( logger.getBackend<LogBackend>("No") , std::invalid_argument );
{
auto counter2 = logger.getBackend<CounterLog>("COUNTER");
BOOST_CHECK_EQUAL( 1U , counter2->numMessages( Log::MessageType::Warning));
BOOST_CHECK_EQUAL( 1U , counter2->numMessages( Log::MessageType::Error));
BOOST_CHECK_EQUAL( 0 , counter2->numMessages( Log::MessageType::Info));
}
BOOST_CHECK_EQUAL( false , logger.removeBackend("NO-not-found"));
BOOST_CHECK_EQUAL( true , logger.removeBackend("COUNTER"));
BOOST_CHECK_EQUAL( false , logger.hasBackend("COUNTER") );
{
auto stream2 = logger.popBackend<StreamLog>("STREAM");
BOOST_CHECK_EQUAL( false , logger.hasBackend("STREAM") );
BOOST_CHECK_THROW( logger.popBackend<StreamLog>("STREAM") , std::invalid_argument );
}
}
BOOST_AUTO_TEST_CASE(LoggerAddTypes_PowerOf2) {
Logger logger;
int64_t not_power_of2 = 13;
int64_t power_of2 = 4096;
BOOST_CHECK_THROW( logger.addMessageType( not_power_of2 , "Prefix") , std::invalid_argument);
BOOST_CHECK_THROW( logger.enabledMessageType( not_power_of2 ) , std::invalid_argument);
logger.addMessageType( power_of2 , "Prefix");
BOOST_CHECK( logger.enabledMessageType( power_of2 ));
BOOST_CHECK_EQUAL( false , logger.enabledMessageType( 2*power_of2 ));
}
class TestLog: public LogBackend {
public:
TestLog( int64_t messageMask ) : LogBackend( messageMask )
{
m_defaultMessages = 0;
m_specialMessages = 0;
}
void addMessage(int64_t messageType , const std::string& /* message */) {
if (messageType & Log::DefaultMessageTypes)
m_defaultMessages +=1;
else
m_specialMessages += 1;
}
int m_defaultMessages;
int m_specialMessages;
};
/*
Testing that the logger frontend does not let unknown message types
pass through; even though the backend has shown interest in the
phony 4096 messagetype.
*/
BOOST_AUTO_TEST_CASE(LoggerMasksTypes) {
Logger logger;
int64_t power_of2 = 4096;
std::shared_ptr<TestLog> testLog = std::make_shared<TestLog>(Log::DefaultMessageTypes + power_of2);
logger.addBackend("TEST" , testLog);
BOOST_CHECK_EQUAL( false , logger.enabledMessageType( power_of2 ));
logger.addMessage( Log::MessageType::Error , "Error");
logger.addMessage( Log::MessageType::Warning , "Warning");
logger.addMessage( Log::MessageType::Info , "Info");
BOOST_CHECK_THROW( logger.addMessage( power_of2 , "Blocked message") , std::invalid_argument );
BOOST_CHECK_EQUAL( testLog->m_defaultMessages , 3 );
BOOST_CHECK_EQUAL( testLog->m_specialMessages , 0 );
logger.addMessageType( power_of2 , "Phony");
logger.addMessage( power_of2 , "Passing through");
BOOST_CHECK_EQUAL( testLog->m_specialMessages , 1 );
}
BOOST_AUTO_TEST_CASE(LoggerDefaultTypesEnabled) {
Logger logger;
BOOST_CHECK_EQUAL( logger.enabledMessageTypes() , Log::DefaultMessageTypes);
}
BOOST_AUTO_TEST_CASE( CounterLogTesting) {
CounterLog counter(Log::DefaultMessageTypes);
counter.addMessage( Log::MessageType::Error , "This is an error ...");
counter.addMessage( Log::MessageType::Warning , "This is a warning");
BOOST_CHECK_EQUAL(1U , counter.numMessages( Log::MessageType::Error ));
BOOST_CHECK_EQUAL(1U , counter.numMessages( Log::MessageType::Warning ));
BOOST_CHECK_EQUAL(0 , counter.numMessages( Log::MessageType::Info ));
{
int64_t not_enabled = 4096;
int64_t not_power2 = 4095;
BOOST_CHECK_EQUAL( 0 , counter.numMessages( not_enabled ));
BOOST_CHECK_THROW( counter.numMessages( not_power2 ) , std::invalid_argument);
}
}
BOOST_AUTO_TEST_CASE(TestTimerLog) {
Logger logger;
std::ostringstream sstream;
std::shared_ptr<TimerLog> timer = std::make_shared<TimerLog>(sstream);
logger.addBackend( "TIMER" , timer );
logger.addMessageType( TimerLog::StartTimer , "Start");
logger.addMessageType( TimerLog::StopTimer , "Stop");
logger.addMessage( TimerLog::StartTimer , "");
logger.addMessage( TimerLog::StopTimer , "This was fast");
std::cout << sstream.str() << std::endl;
}
/*****************************************************************/
void initLogger(std::ostringstream& log_stream);
void initLogger(std::ostringstream& log_stream) {
std::shared_ptr<CounterLog> counter = std::make_shared<CounterLog>();
std::shared_ptr<StreamLog> streamLog = std::make_shared<StreamLog>( log_stream , Log::MessageType::Warning );
BOOST_CHECK_EQUAL( false , OpmLog::hasBackend("NO"));
OpmLog::addBackend("COUNTER" , counter);
OpmLog::addBackend("STREAM" , streamLog);
BOOST_CHECK_EQUAL( true , OpmLog::hasBackend("COUNTER"));
BOOST_CHECK_EQUAL( true , OpmLog::hasBackend("STREAM"));
}
BOOST_AUTO_TEST_CASE(TestOpmLog) {
std::ostringstream log_stream;
initLogger(log_stream);
OpmLog::addMessage( Log::MessageType::Warning , "Warning");
OpmLog::addMessage( Log::MessageType::Error , "Error");
{
auto counter = OpmLog::getBackend<CounterLog>("COUNTER");
BOOST_CHECK_EQUAL( 1 , counter->numMessages(Log::MessageType::Error) );
BOOST_CHECK_EQUAL( 1 , counter->numMessages(Log::MessageType::Warning) );
BOOST_CHECK_EQUAL( 0 , counter->numMessages(Log::MessageType::Info) );
}
BOOST_CHECK_EQUAL( log_stream.str() , "Warning\n");
}