Added simple static format functions to OpmLog

This commit is contained in:
Joakim Hove 2015-01-06 19:01:25 +01:00
parent e18123b7f3
commit e2a11f620c
3 changed files with 48 additions and 1 deletions

View File

@ -16,6 +16,8 @@
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/parser/eclipse/Log/Logger.hpp>
#include <opm/parser/eclipse/OpmLog/OpmLog.hpp>
@ -29,7 +31,35 @@ namespace Opm {
return m_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();
}
std::string OpmLog::prefixMessage(Logger::MessageType messageType, const std::string& message) {
std::string prefix;
switch (messageType) {
case Logger::Note:
prefix = "note";
break;
case Logger::Warning:
prefix = "warning";
break;
case Logger::Error:
prefix = "error";
break;
default:
throw std::invalid_argument("Unhandled messagetype");
}
return prefix + ": " + message;
}
void OpmLog::addMessage(Logger::MessageType messageType , const std::string& message) {
auto logger = OpmLog::getLogger();
@ -37,5 +67,9 @@ namespace Opm {
}
/******************************************************************/
std::shared_ptr<Logger> OpmLog::m_logger;
}

View File

@ -33,8 +33,11 @@ namespace Opm {
*/
class OpmLog {
public:
static void addMessage(Logger::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(Logger::MessageType messageType , const std::string& msg);
private:
static std::shared_ptr<Logger> getLogger();
static std::shared_ptr<Logger> m_logger;

View File

@ -32,3 +32,13 @@ BOOST_AUTO_TEST_CASE(DoLogging) {
OpmLog::addMessage(Logger::MessageType::Warning , "Warning1");
OpmLog::addMessage(Logger::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( "error: This is the error" , OpmLog::prefixMessage(Logger::MessageType::Error , "This is the error"));
BOOST_CHECK_EQUAL( "warning: This is the warning" , OpmLog::prefixMessage(Logger::MessageType::Warning , "This is the warning"));
BOOST_CHECK_EQUAL( "note: This is the note" , OpmLog::prefixMessage(Logger::MessageType::Note , "This is the note"));
}