From 245b91d0c2f0f55808ecd84eb46a05afd44e024f Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Tue, 6 Jan 2015 21:18:55 +0100 Subject: [PATCH] Introduced LogUtil file + Logger class - The new LogUtil.cpp / LogUtil.hpp files contain the enum definitions for MessageType and formatting functions. - The Logger class is meant to be the instance which is held by the static OpmLog singleton class. --- opm/parser/eclipse/CMakeLists.txt | 2 + opm/parser/eclipse/Deck/tests/DeckTests.cpp | 6 +- opm/parser/eclipse/OpmLog/LogUtil.cpp | 61 +++++++++++++++++++ opm/parser/eclipse/OpmLog/LogUtil.hpp | 44 +++++++++++++ opm/parser/eclipse/OpmLog/Logger.hpp | 1 + opm/parser/eclipse/OpmLog/MessageCounter.cpp | 55 +++++++++-------- opm/parser/eclipse/OpmLog/MessageCounter.hpp | 13 ++-- opm/parser/eclipse/OpmLog/OpmLog.cpp | 49 +++------------ opm/parser/eclipse/OpmLog/OpmLog.hpp | 22 ++----- .../eclipse/OpmLog/tests/OpmLogTests.cpp | 18 ++++-- 10 files changed, 175 insertions(+), 96 deletions(-) create mode 100644 opm/parser/eclipse/OpmLog/LogUtil.cpp create mode 100644 opm/parser/eclipse/OpmLog/LogUtil.hpp diff --git a/opm/parser/eclipse/CMakeLists.txt b/opm/parser/eclipse/CMakeLists.txt index 337198479..e2b37a876 100644 --- a/opm/parser/eclipse/CMakeLists.txt +++ b/opm/parser/eclipse/CMakeLists.txt @@ -108,7 +108,9 @@ EclipseState/SimulationConfig/ThresholdPressure.cpp) set( HEADER_FILES OpmLog/LogBackend.hpp OpmLog/MessageCounter.hpp +OpmLog/Logger.hpp OpmLog/OpmLog.hpp +OpmLog/LogUtil.hpp # RawDeck/RawConsts.hpp RawDeck/RawKeyword.hpp diff --git a/opm/parser/eclipse/Deck/tests/DeckTests.cpp b/opm/parser/eclipse/Deck/tests/DeckTests.cpp index 18e0d50c7..2cb278e9c 100644 --- a/opm/parser/eclipse/Deck/tests/DeckTests.cpp +++ b/opm/parser/eclipse/Deck/tests/DeckTests.cpp @@ -139,17 +139,17 @@ BOOST_AUTO_TEST_CASE(DECKAddWarning) { logger.addError("FILE3", 300U, "ERROR"); BOOST_CHECK_EQUAL(3U, logger.size()); - BOOST_CHECK_EQUAL(logger.getMessageType(0), OpmLog::Note); + BOOST_CHECK_EQUAL(logger.getMessageType(0), Log::Note); BOOST_CHECK_EQUAL(logger.getDescription(0), "NOTE"); BOOST_CHECK_EQUAL(logger.getFileName(0), "FILE"); BOOST_CHECK_EQUAL(logger.getLineNumber(0), 100U); - BOOST_CHECK_EQUAL(logger.getMessageType(1), OpmLog::Warning); + BOOST_CHECK_EQUAL(logger.getMessageType(1), Log::Warning); BOOST_CHECK_EQUAL(logger.getDescription(1), "WARNING"); BOOST_CHECK_EQUAL(logger.getFileName(1), "FILE2"); BOOST_CHECK_EQUAL(logger.getLineNumber(1), 200U); - BOOST_CHECK_EQUAL(logger.getMessageType(2), OpmLog::Error); + BOOST_CHECK_EQUAL(logger.getMessageType(2), Log::Error); BOOST_CHECK_EQUAL(logger.getDescription(2), "ERROR"); BOOST_CHECK_EQUAL(logger.getFileName(2), "FILE3"); BOOST_CHECK_EQUAL(logger.getLineNumber(2), 300U); diff --git a/opm/parser/eclipse/OpmLog/LogUtil.cpp b/opm/parser/eclipse/OpmLog/LogUtil.cpp new file mode 100644 index 000000000..86df34077 --- /dev/null +++ b/opm/parser/eclipse/OpmLog/LogUtil.cpp @@ -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 . +*/ + +#include +#include +#include + + +namespace Opm { + +namespace Log { + + std::string fileMessage(const std::string& filename , size_t line , const std::string& message) { + std::ostringstream oss; + + oss << filename << ":" << line << ": " << message; + + return oss.str(); + } + + std::string fileMessage(MessageType messageType , const std::string& filename , size_t line , const std::string& message) { + return fileMessage( filename , line , prefixMessage( messageType , message )); + } + + + std::string prefixMessage(MessageType messageType, const std::string& message) { + std::string prefix; + switch (messageType) { + case Note: + prefix = "note"; + break; + case Warning: + prefix = "warning"; + break; + case Error: + prefix = "error"; + break; + default: + throw std::invalid_argument("Unhandled messagetype"); + } + + return prefix + ": " + message; + } +} +} diff --git a/opm/parser/eclipse/OpmLog/LogUtil.hpp b/opm/parser/eclipse/OpmLog/LogUtil.hpp new file mode 100644 index 000000000..9a828aa41 --- /dev/null +++ b/opm/parser/eclipse/OpmLog/LogUtil.hpp @@ -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 . +*/ + +#ifndef OPM_LOG_UTIL_HPP +#define OPM_LOG_UTIL_HPP + +#include +#include + +namespace Opm { +namespace Log { + + enum MessageType { + Note = 0x01, + Warning = 0x02, + Error = 0x04 + }; + + const int64_t AllMessageTypes = 0xff; + + std::string fileMessage(const std::string& path, size_t line , const std::string& msg); + std::string fileMessage(Log::MessageType messageType , const std::string& path, size_t line , const std::string& msg); + std::string prefixMessage(Log::MessageType messageType , const std::string& msg); + +} +} + +#endif diff --git a/opm/parser/eclipse/OpmLog/Logger.hpp b/opm/parser/eclipse/OpmLog/Logger.hpp index f3086434b..7e4ecfed7 100644 --- a/opm/parser/eclipse/OpmLog/Logger.hpp +++ b/opm/parser/eclipse/OpmLog/Logger.hpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace Opm { diff --git a/opm/parser/eclipse/OpmLog/MessageCounter.cpp b/opm/parser/eclipse/OpmLog/MessageCounter.cpp index 97cbaa316..9310c17d4 100644 --- a/opm/parser/eclipse/OpmLog/MessageCounter.cpp +++ b/opm/parser/eclipse/OpmLog/MessageCounter.cpp @@ -21,6 +21,7 @@ #include #include +#include #include @@ -66,49 +67,53 @@ size_t MessageCounter::numNotes() const { void MessageCounter::addMessage(const std::string& fileName, int lineNumber, - OpmLog::MessageType messageType, + Log::MessageType messageType, const std::string& description) { - switch (messageType) { - case OpmLog::Note: - ++m_numNotes; - break; - case OpmLog::Warning: - ++m_numWarnings; - break; + if (includeMessage( messageType )) { + switch (messageType) { + case Log::Note: + ++m_numNotes; + break; - case OpmLog::Error: - ++m_numErrors; - break; + case Log::Warning: + ++m_numWarnings; + break; - default: - throw std::invalid_argument("Log messages must be of type Note, Warning or Error"); - } + case Log::Error: + ++m_numErrors; + break; - m_messages.push_back(MessageTuple(fileName, lineNumber, messageType, description)); + default: + throw std::invalid_argument("Log messages must be of type Note, Warning or Error"); + } - if (m_outStream) { - (*m_outStream) << getFormattedMessage(size() - 1) << "\n"; - (*m_outStream) << (std::flush); + m_messages.push_back(MessageTuple(fileName, lineNumber, messageType, description)); + + if (m_outStream) { + (*m_outStream) << getFormattedMessage(size() - 1) << "\n"; + (*m_outStream) << (std::flush); + } } } + void MessageCounter::addNote(const std::string& fileName, int lineNumber, const std::string& description) { - addMessage(fileName, lineNumber, OpmLog::Note, description); + addMessage(fileName, lineNumber, Log::Note, description); } void MessageCounter::addWarning(const std::string& fileName, int lineNumber, const std::string& description) { - addMessage(fileName, lineNumber, OpmLog::Warning, description); + addMessage(fileName, lineNumber, Log::Warning, description); } void MessageCounter::addError(const std::string& fileName, int lineNumber, const std::string& description) { - addMessage(fileName, lineNumber, OpmLog::Error, description); + addMessage(fileName, lineNumber, Log::Error, description); } void MessageCounter::clear() @@ -140,7 +145,7 @@ int MessageCounter::getLineNumber(size_t msgIdx) const { return std::get<1>(m_messages[msgIdx]); } -OpmLog::MessageType MessageCounter::getMessageType(size_t msgIdx) const { +Log::MessageType MessageCounter::getMessageType(size_t msgIdx) const { assert(msgIdx < size()); return std::get<2>(m_messages[msgIdx]); } @@ -158,13 +163,13 @@ const std::string MessageCounter::getFormattedMessage(size_t msgIdx) const { } switch (getMessageType(msgIdx)) { - case OpmLog::Note: + case Log::Note: oss << " note:"; break; - case OpmLog::Warning: + case Log::Warning: oss << " warning:"; break; - case OpmLog::Error: + case Log::Error: oss << " error:"; break; } diff --git a/opm/parser/eclipse/OpmLog/MessageCounter.hpp b/opm/parser/eclipse/OpmLog/MessageCounter.hpp index 315811678..bccd4dad9 100644 --- a/opm/parser/eclipse/OpmLog/MessageCounter.hpp +++ b/opm/parser/eclipse/OpmLog/MessageCounter.hpp @@ -25,6 +25,7 @@ #include #include + #include #include @@ -48,7 +49,7 @@ public: void addMessage(const std::string& fileName, int lineNumber, - OpmLog::MessageType messageType, + Log::MessageType messageType, const std::string& description); void addNote(const std::string& fileName, @@ -63,7 +64,7 @@ public: const std::string& getFileName(size_t msgIdx) const; int getLineNumber(size_t msgIdx) const; - OpmLog::MessageType getMessageType(size_t msgIdx) const; + Log::MessageType getMessageType(size_t msgIdx) const; const std::string& getDescription(size_t msgIdx) const; void clear(); @@ -87,13 +88,13 @@ public: * This is just another convenience method... */ void printAll(std::ostream &os = std::cerr, - size_t enabledTypes = OpmLog::AllMessageTypes) const; + size_t enabledTypes = Log::AllMessageTypes) const; void close(); ~MessageCounter() {}; private: - typedef std::tuple MessageTuple; std::vector m_messages; diff --git a/opm/parser/eclipse/OpmLog/OpmLog.cpp b/opm/parser/eclipse/OpmLog/OpmLog.cpp index 04134ab64..2f927e5ed 100644 --- a/opm/parser/eclipse/OpmLog/OpmLog.cpp +++ b/opm/parser/eclipse/OpmLog/OpmLog.cpp @@ -20,57 +20,26 @@ #include #include +#include namespace Opm { + std::shared_ptr OpmLog::getLogger() { + if (!m_logger) + m_logger.reset( new Logger() ); - std::string OpmLog::fileMessage(const std::string& filename , size_t line , const std::string& message) { - std::ostringstream oss; - - oss << filename << ":" << line << ": " << message; - - return oss.str(); + return m_logger; } - std::string OpmLog::prefixMessage(MessageType messageType, const std::string& message) { - std::string prefix; - switch (messageType) { - case Note: - prefix = "note"; - break; - case Warning: - prefix = "warning"; - break; - case Error: - prefix = "error"; - break; - default: - throw std::invalid_argument("Unhandled messagetype"); - } - - return prefix + ": " + message; - } - - /* - std::shared_ptr OpmLog::getMessageCounter() { - if (!m_logger) - m_logger.reset( new MessageCounter() ); - - return m_logger; - } - */ - - void OpmLog::addMessage(MessageType messageType , const std::string& message) { - /* - auto logger = OpmLog::getMessageCounter(); - logger->addMessage( "" , -1 , messageType , message ); - */ + void OpmLog::addMessage(int64_t messageFlag , const std::string& message) { + auto logger = OpmLog::getLogger(); + logger->addMessage( messageFlag , message ); } /******************************************************************/ - //std::shared_ptr OpmLog::m_logger; + std::shared_ptr OpmLog::m_logger; } diff --git a/opm/parser/eclipse/OpmLog/OpmLog.hpp b/opm/parser/eclipse/OpmLog/OpmLog.hpp index d1a8963c1..853e29e39 100644 --- a/opm/parser/eclipse/OpmLog/OpmLog.hpp +++ b/opm/parser/eclipse/OpmLog/OpmLog.hpp @@ -23,33 +23,23 @@ #include #include +#include +#include namespace Opm { /* The OpmLog class is a fully static class which manages a proper - MessageCounter instance. + Logger instance. */ class OpmLog { public: - enum MessageType { - Note = 0x01, - Warning = 0x02, - Error = 0x04 - }; - - static const int64_t AllMessageTypes = 0xff; - - static void addMessage(MessageType messageType , const std::string& message); - static std::string fileMessage(const std::string& path, size_t line , const std::string& msg); - static std::string prefixMessage(MessageType messageType , const std::string& msg); + static void addMessage(int64_t messageFlag , const std::string& message); private: - /* - static std::shared_ptr getMessageCounter(); - static std::shared_ptr m_logger; - */ + static std::shared_ptr getLogger(); + static std::shared_ptr m_logger; }; diff --git a/opm/parser/eclipse/OpmLog/tests/OpmLogTests.cpp b/opm/parser/eclipse/OpmLog/tests/OpmLogTests.cpp index bc5c0fbcf..9b9eac107 100644 --- a/opm/parser/eclipse/OpmLog/tests/OpmLogTests.cpp +++ b/opm/parser/eclipse/OpmLog/tests/OpmLogTests.cpp @@ -31,17 +31,17 @@ using namespace Opm; BOOST_AUTO_TEST_CASE(DoLogging) { - OpmLog::addMessage(OpmLog::MessageType::Warning , "Warning1"); - OpmLog::addMessage(OpmLog::MessageType::Warning , "Warning2"); + 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?" , OpmLog::fileMessage("/path/to/file" , 100 , "There is a mild fuckup here?")); + 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" , OpmLog::prefixMessage(OpmLog::MessageType::Error , "This is the error")); - BOOST_CHECK_EQUAL( "warning: This is the warning" , OpmLog::prefixMessage(OpmLog::MessageType::Warning , "This is the warning")); - BOOST_CHECK_EQUAL( "note: This is the note" , OpmLog::prefixMessage(OpmLog::MessageType::Note , "This is the note")); + 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( "note: This is the note" , Log::prefixMessage(Log::MessageType::Note , "This is the note")); } @@ -60,3 +60,9 @@ BOOST_AUTO_TEST_CASE(Test_AbstractBackend) { BOOST_CHECK_EQUAL(false, backend.includeMessage(6 )); BOOST_CHECK_EQUAL(true , backend.includeMessage(5 )); } + + + +BOOST_AUTO_TEST_CASE(Test_Logger) { + Logger logger; +}