2014-10-01 14:22:08 +02:00
|
|
|
/*
|
|
|
|
|
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>
|
|
|
|
|
|
2015-01-06 19:31:52 +01:00
|
|
|
#include <opm/parser/eclipse/OpmLog/OpmLog.hpp>
|
2015-01-06 21:18:55 +01:00
|
|
|
#include <opm/parser/eclipse/OpmLog/LogUtil.hpp>
|
2015-01-06 19:46:16 +01:00
|
|
|
#include <opm/parser/eclipse/OpmLog/MessageCounter.hpp>
|
2014-12-12 20:56:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-01 14:22:08 +02:00
|
|
|
namespace Opm {
|
2015-01-06 20:24:41 +01:00
|
|
|
MessageCounter::MessageCounter() : LogBackend(Log::AllMessageTypes)
|
|
|
|
|
{
|
2014-10-01 14:22:08 +02:00
|
|
|
m_numErrors = 0;
|
|
|
|
|
m_numWarnings = 0;
|
|
|
|
|
m_numNotes = 0;
|
2014-10-07 15:35:44 +02:00
|
|
|
|
|
|
|
|
setOutStream(NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 20:24:41 +01:00
|
|
|
MessageCounter::MessageCounter(std::ostream* os) : LogBackend(Log::AllMessageTypes)
|
|
|
|
|
{
|
2014-10-07 15:35:44 +02:00
|
|
|
m_numErrors = 0;
|
|
|
|
|
m_numWarnings = 0;
|
|
|
|
|
m_numNotes = 0;
|
|
|
|
|
|
|
|
|
|
setOutStream(os);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
void MessageCounter::setOutStream(std::ostream* os) {
|
2014-10-07 15:35:44 +02:00
|
|
|
m_outStream = os;
|
2014-10-01 14:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
size_t MessageCounter::size() const {
|
2014-10-01 14:22:08 +02:00
|
|
|
return m_messages.size();
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
size_t MessageCounter::numErrors() const {
|
2014-10-01 14:22:08 +02:00
|
|
|
return m_numErrors;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
size_t MessageCounter::numWarnings() const {
|
2014-10-01 14:22:08 +02:00
|
|
|
return m_numWarnings;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
size_t MessageCounter::numNotes() const {
|
2014-10-01 14:22:08 +02:00
|
|
|
return m_numNotes;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
void MessageCounter::addMessage(const std::string& fileName,
|
2015-01-06 19:31:52 +01:00
|
|
|
int lineNumber,
|
2015-01-06 21:18:55 +01:00
|
|
|
Log::MessageType messageType,
|
2015-01-06 19:31:52 +01:00
|
|
|
const std::string& description) {
|
2014-10-01 14:22:08 +02:00
|
|
|
|
2015-01-06 21:18:55 +01:00
|
|
|
if (includeMessage( messageType )) {
|
|
|
|
|
switch (messageType) {
|
|
|
|
|
case Log::Note:
|
|
|
|
|
++m_numNotes;
|
|
|
|
|
break;
|
2014-10-01 14:22:08 +02:00
|
|
|
|
2015-01-06 21:18:55 +01:00
|
|
|
case Log::Warning:
|
|
|
|
|
++m_numWarnings;
|
|
|
|
|
break;
|
2014-10-01 14:22:08 +02:00
|
|
|
|
2015-01-06 21:18:55 +01:00
|
|
|
case Log::Error:
|
|
|
|
|
++m_numErrors;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw std::invalid_argument("Log messages must be of type Note, Warning or Error");
|
|
|
|
|
}
|
2014-10-01 14:22:08 +02:00
|
|
|
|
2015-01-06 21:18:55 +01:00
|
|
|
m_messages.push_back(MessageTuple(fileName, lineNumber, messageType, description));
|
2014-10-07 15:35:44 +02:00
|
|
|
|
2015-01-06 21:18:55 +01:00
|
|
|
if (m_outStream) {
|
|
|
|
|
(*m_outStream) << getFormattedMessage(size() - 1) << "\n";
|
|
|
|
|
(*m_outStream) << (std::flush);
|
|
|
|
|
}
|
2014-10-07 15:35:44 +02:00
|
|
|
}
|
2014-10-01 14:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-07 22:31:02 +01:00
|
|
|
bool MessageCounter::addMessage(int64_t messageFlag , const std::string& message) {
|
|
|
|
|
if (includeMessage( messageFlag )) {
|
|
|
|
|
addMessage("???" , -1 , static_cast<Log::MessageType>(messageFlag) , message);
|
|
|
|
|
return true;
|
|
|
|
|
} else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-06 21:18:55 +01:00
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
void MessageCounter::addNote(const std::string& fileName,
|
2014-10-07 15:35:44 +02:00
|
|
|
int lineNumber,
|
|
|
|
|
const std::string& description) {
|
2015-01-06 21:18:55 +01:00
|
|
|
addMessage(fileName, lineNumber, Log::Note, description);
|
2014-10-01 14:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
void MessageCounter::addWarning(const std::string& fileName,
|
2014-10-01 14:22:08 +02:00
|
|
|
int lineNumber,
|
|
|
|
|
const std::string& description) {
|
2015-01-06 21:18:55 +01:00
|
|
|
addMessage(fileName, lineNumber, Log::Warning, description);
|
2014-10-01 14:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
void MessageCounter::addError(const std::string& fileName,
|
2014-10-01 14:22:08 +02:00
|
|
|
int lineNumber,
|
|
|
|
|
const std::string& description) {
|
2015-01-06 21:18:55 +01:00
|
|
|
addMessage(fileName, lineNumber, Log::Error, description);
|
2014-10-01 14:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
void MessageCounter::clear()
|
2014-10-17 17:07:09 +02:00
|
|
|
{
|
|
|
|
|
m_numErrors = 0;
|
|
|
|
|
m_numWarnings = 0;
|
|
|
|
|
m_numNotes = 0;
|
|
|
|
|
|
|
|
|
|
m_messages.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
void MessageCounter::append(const MessageCounter &other)
|
2014-10-17 17:07:09 +02:00
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < other.size(); ++i) {
|
|
|
|
|
addMessage(other.getFileName(i),
|
|
|
|
|
other.getLineNumber(i),
|
|
|
|
|
other.getMessageType(i),
|
|
|
|
|
other.getDescription(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
const std::string& MessageCounter::getFileName(size_t msgIdx) const {
|
2014-10-01 14:22:08 +02:00
|
|
|
assert(msgIdx < size());
|
|
|
|
|
return std::get<0>(m_messages[msgIdx]);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
int MessageCounter::getLineNumber(size_t msgIdx) const {
|
2014-10-01 14:22:08 +02:00
|
|
|
assert(msgIdx < size());
|
|
|
|
|
return std::get<1>(m_messages[msgIdx]);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 21:18:55 +01:00
|
|
|
Log::MessageType MessageCounter::getMessageType(size_t msgIdx) const {
|
2014-10-01 14:22:08 +02:00
|
|
|
assert(msgIdx < size());
|
|
|
|
|
return std::get<2>(m_messages[msgIdx]);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
const std::string& MessageCounter::getDescription(size_t msgIdx) const {
|
2014-10-01 14:22:08 +02:00
|
|
|
assert(msgIdx < size());
|
|
|
|
|
return std::get<3>(m_messages[msgIdx]);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
const std::string MessageCounter::getFormattedMessage(size_t msgIdx) const {
|
2015-01-06 21:30:17 +01:00
|
|
|
const std::string& description = getDescription( msgIdx );
|
|
|
|
|
Log::MessageType messageType = getMessageType( msgIdx );
|
|
|
|
|
std::string prefixedMessage = Log::prefixMessage( messageType , description);
|
|
|
|
|
int lineNumber = getLineNumber(msgIdx);
|
2014-10-01 14:22:08 +02:00
|
|
|
|
2015-01-06 21:30:17 +01:00
|
|
|
if (lineNumber > 0) {
|
|
|
|
|
const std::string& filename = getFileName(msgIdx);
|
|
|
|
|
return Log::fileMessage( filename , getLineNumber(msgIdx) , prefixedMessage);
|
|
|
|
|
} else
|
|
|
|
|
return prefixedMessage;
|
2014-10-01 14:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-06 21:30:17 +01:00
|
|
|
|
|
|
|
|
|
2015-01-06 19:46:16 +01:00
|
|
|
void MessageCounter::printAll(std::ostream& os, size_t enabledTypes) const {
|
2014-10-01 14:22:08 +02:00
|
|
|
for (size_t i = 0; i < size(); ++i)
|
|
|
|
|
if (enabledTypes & getMessageType(i))
|
|
|
|
|
os << getFormattedMessage(i) << "\n";
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 20:24:41 +01:00
|
|
|
void MessageCounter::close() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-01 14:22:08 +02:00
|
|
|
} // namespace Opm
|