Finish revision of logging.
This commit is contained in:
parent
35514c0aa0
commit
0283536ac4
@ -48,9 +48,8 @@ size_t CounterLog::numMessages(int64_t messageType) const {
|
||||
|
||||
|
||||
|
||||
void CounterLog::addTaggedMessage(int64_t messageType, const std::string& messageTag, const std::string& ) {
|
||||
if (includeMessage( messageType, messageTag ))
|
||||
m_count[messageType]++;
|
||||
void CounterLog::addMessageUnconditionally(int64_t messageType, const std::string& ) {
|
||||
m_count[messageType]++;
|
||||
}
|
||||
|
||||
|
||||
|
@ -30,28 +30,23 @@ 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:
|
||||
class CounterLog : public LogBackend
|
||||
{
|
||||
public:
|
||||
CounterLog(int64_t messageMask);
|
||||
CounterLog();
|
||||
|
||||
CounterLog(int64_t messageMask);
|
||||
CounterLog();
|
||||
size_t numMessages(int64_t messageType) const;
|
||||
|
||||
size_t numMessages(int64_t messageType) const;
|
||||
void clear();
|
||||
|
||||
protected:
|
||||
void addMessageUnconditionally(int64_t messageFlag,
|
||||
const std::string& message) override;
|
||||
private:
|
||||
std::map<int64_t , size_t> m_count;
|
||||
};
|
||||
|
||||
void addTaggedMessage(int64_t messageFlag,
|
||||
const std::string& messageTag,
|
||||
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
|
||||
|
@ -23,9 +23,9 @@
|
||||
namespace Opm {
|
||||
|
||||
|
||||
void EclipsePRTLog::addTaggedMessage(int64_t messageType, const std::string& messageTag, const std::string& message)
|
||||
void EclipsePRTLog::addMessageUnconditionally(int64_t messageType, const std::string& message)
|
||||
{
|
||||
StreamLog::addTaggedMessage(messageType, messageTag, message);
|
||||
StreamLog::addMessageUnconditionally(messageType, message);
|
||||
m_count[messageType]++;
|
||||
}
|
||||
|
||||
|
@ -31,8 +31,6 @@ class EclipsePRTLog : public StreamLog {
|
||||
public:
|
||||
using StreamLog::StreamLog;
|
||||
|
||||
void addTaggedMessage(int64_t messageType, const std::string& messageTag, const std::string& message);
|
||||
|
||||
size_t numMessages(int64_t messageType) const;
|
||||
|
||||
~EclipsePRTLog();
|
||||
@ -54,6 +52,10 @@ public:
|
||||
/// \param print_summary If true print a summary to the PRT file.
|
||||
EclipsePRTLog(std::ostream& os , int64_t messageMask,
|
||||
bool print_summary);
|
||||
|
||||
protected:
|
||||
void addMessageUnconditionally(int64_t messageType, const std::string& message) override;
|
||||
|
||||
private:
|
||||
std::map<int64_t, size_t> m_count;
|
||||
/// \brief Whether to print a summary to the log file.
|
||||
|
@ -47,6 +47,12 @@ namespace Opm {
|
||||
addTaggedMessage(messageFlag, "", message);
|
||||
}
|
||||
|
||||
void LogBackend::addTaggedMessage(int64_t messageType, const std::string& messageTag, const std::string& message) {
|
||||
if (includeMessage( messageType, messageTag )) {
|
||||
addMessageUnconditionally(messageType, message);
|
||||
}
|
||||
}
|
||||
|
||||
int64_t LogBackend::getMask() const
|
||||
{
|
||||
return m_mask;
|
||||
@ -67,12 +73,13 @@ namespace Opm {
|
||||
if (res == MessageLimiter::Response::JustOverTagLimit) {
|
||||
// Special case: add a message to this backend about limit being reached.
|
||||
std::string msg = "Message limit reached for message tag: " + messageTag;
|
||||
addTaggedMessage(messageFlag, "", msg);
|
||||
addMessageUnconditionally(messageFlag, msg);
|
||||
}
|
||||
if (res == MessageLimiter::Response::JustOverCategoryLimit) {
|
||||
// Special case: add a message to this backend about limit being reached.
|
||||
std::string msg = "Message limit reached for message : " + Log::prefixMessage(messageFlag, "");
|
||||
addTaggedMessage(messageFlag, "", msg);
|
||||
std::string prefix = Log::prefixMessage(messageFlag, "");
|
||||
std::string msg = "Message limit reached for message category: " + prefix.substr(0, prefix.size()-2);
|
||||
addMessageUnconditionally(messageFlag, msg);
|
||||
}
|
||||
|
||||
return res == MessageLimiter::Response::PrintMessage;
|
||||
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
Copyright 2015 Statoil ASA.
|
||||
Copyright 2015, 2016 Statoil ASA.
|
||||
Copyright 2016 SINTEF ICT, Applied Mathematics.
|
||||
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
@ -46,32 +47,34 @@ namespace Opm
|
||||
/// Configure how message tags will be used to limit messages.
|
||||
void setMessageLimiter(std::shared_ptr<MessageLimiter> limiter);
|
||||
|
||||
/// Add a message to the backend.
|
||||
///
|
||||
/// Typically a subclass may filter, change, and output
|
||||
/// messages based on configuration and the messageFlag.
|
||||
/// Add a message to the backend if accepted by the message limiter.
|
||||
void addMessage(int64_t messageFlag, const std::string& message);
|
||||
|
||||
/// Add a tagged message to the backend.
|
||||
///
|
||||
/// Typically a subclass may filter, change, and output
|
||||
/// messages based on configuration and the messageFlag.
|
||||
virtual void addTaggedMessage(int64_t messageFlag,
|
||||
const std::string& messageTag,
|
||||
const std::string& message) = 0;
|
||||
/// Add a tagged message to the backend if accepted by the message limiter.
|
||||
void addTaggedMessage(int64_t messageFlag,
|
||||
const std::string& messageTag,
|
||||
const std::string& message);
|
||||
|
||||
/// The message mask types are specified in the
|
||||
/// Opm::Log::MessageType namespace, in file LogUtils.hpp.
|
||||
int64_t getMask() const;
|
||||
|
||||
protected:
|
||||
/// Return true if all bits of messageFlag are also set in our mask.
|
||||
bool includeMessage(int64_t messageFlag, const std::string& messageTag);
|
||||
/// This is the method subclasses should override.
|
||||
///
|
||||
/// Typically a subclass may filter, change, and output
|
||||
/// messages based on configuration and the messageFlag.
|
||||
virtual void addMessageUnconditionally(int64_t messageFlag,
|
||||
const std::string& message) = 0;
|
||||
|
||||
/// Return decorated version of message depending on configureDecoration() arguments.
|
||||
std::string formatMessage(int64_t messageFlag, const std::string& message);
|
||||
|
||||
private:
|
||||
/// Return true if all bits of messageFlag are also set in our mask,
|
||||
/// and the message limiter returns a PrintMessage response.
|
||||
bool includeMessage(int64_t messageFlag, const std::string& messageTag);
|
||||
|
||||
int64_t m_mask;
|
||||
std::shared_ptr<MessageFormatterInterface> m_formatter;
|
||||
std::shared_ptr<MessageLimiter> m_limiter;
|
||||
|
@ -66,6 +66,18 @@ namespace Opm
|
||||
: tag_limit_(tag_limit < 0 ? NoLimit : tag_limit),
|
||||
category_limits_(category_limits)
|
||||
{
|
||||
// Must ensure NoLimit for categories that are not
|
||||
// explicitly specified in the input.
|
||||
for (auto category : { Log::MessageType::Note,
|
||||
Log::MessageType::Info,
|
||||
Log::MessageType::Warning,
|
||||
Log::MessageType::Error,
|
||||
Log::MessageType::Problem,
|
||||
Log::MessageType::Bug }) {
|
||||
if (category_limits_.find(category) == category_limits_.end()) {
|
||||
category_limits_[category] = NoLimit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The tag message limit (same for all tags).
|
||||
|
@ -48,12 +48,11 @@ void StreamLog::close() {
|
||||
}
|
||||
}
|
||||
|
||||
void StreamLog::addTaggedMessage(int64_t messageType, const std::string& messageTag, const std::string& message) {
|
||||
if (includeMessage( messageType, messageTag )) {
|
||||
(*m_ostream) << formatMessage(messageType, message) << std::endl;
|
||||
if (m_ofstream.is_open()) {
|
||||
m_ofstream.flush();
|
||||
}
|
||||
void StreamLog::addMessageUnconditionally(int64_t messageType, const std::string& message)
|
||||
{
|
||||
(*m_ostream) << formatMessage(messageType, message) << std::endl;
|
||||
if (m_ofstream.is_open()) {
|
||||
m_ofstream.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,4 +61,4 @@ StreamLog::~StreamLog() {
|
||||
close();
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace Opm
|
||||
|
@ -33,9 +33,11 @@ class StreamLog : public LogBackend {
|
||||
public:
|
||||
StreamLog(const std::string& logFile , int64_t messageMask, bool append = false);
|
||||
StreamLog(std::ostream& os , int64_t messageMask);
|
||||
virtual void addTaggedMessage(int64_t messageType, const std::string& messageTag, const std::string& message) override;
|
||||
~StreamLog();
|
||||
|
||||
protected:
|
||||
virtual void addMessageUnconditionally(int64_t messageType, const std::string& message) override;
|
||||
|
||||
private:
|
||||
void close();
|
||||
|
||||
|
@ -41,14 +41,14 @@ TimerLog::TimerLog(std::ostream& os) : StreamLog( os , StopTimer | StartTimer )
|
||||
|
||||
|
||||
|
||||
void TimerLog::addTaggedMessage(int64_t messageType, const std::string& messageTag, const std::string& msg ) {
|
||||
void TimerLog::addMessageUnconditionally(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::addTaggedMessage( messageType, messageTag, m_work.str());
|
||||
StreamLog::addMessageUnconditionally( messageType, m_work.str());
|
||||
} else {
|
||||
if (messageType == StartTimer)
|
||||
m_start = clock();
|
||||
|
@ -42,12 +42,12 @@ public:
|
||||
TimerLog(const std::string& logFile);
|
||||
TimerLog(std::ostream& os);
|
||||
|
||||
void addTaggedMessage(int64_t messageFlag,
|
||||
const std::string& messageTag,
|
||||
const std::string& message) override;
|
||||
|
||||
void clear();
|
||||
~TimerLog() {};
|
||||
|
||||
protected:
|
||||
void addMessageUnconditionally(int64_t messageFlag,
|
||||
const std::string& message) override;
|
||||
private:
|
||||
clock_t m_start;
|
||||
std::ostringstream m_work;
|
||||
|
@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(DoLogging) {
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Test_Format) {
|
||||
BOOST_CHECK_EQUAL( "There is a mild fuckup here?\nIn file /path/to/file, line 100\n" , Log::fileMessage("/path/to/file" , 100 , "There is a mild fuckup here?"));
|
||||
BOOST_CHECK_EQUAL( "There is an error here?\nIn file /path/to/file, line 100\n" , Log::fileMessage("/path/to/file" , 100 , "There is an error 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"));
|
||||
@ -118,7 +118,8 @@ public:
|
||||
m_specialMessages = 0;
|
||||
}
|
||||
|
||||
void addTaggedMessage(int64_t messageType , const std::string& /* messageTag */, const std::string& /* message */) {
|
||||
void addMessageUnconditionally(int64_t messageType , const std::string& /* message */) override
|
||||
{
|
||||
if (messageType & Log::DefaultMessageTypes)
|
||||
m_defaultMessages +=1;
|
||||
else
|
||||
@ -328,7 +329,8 @@ BOOST_AUTO_TEST_CASE(TestOpmLogWithLimits)
|
||||
streamLog1->setMessageFormatter(std::make_shared<SimpleMessageFormatter>(false, true));
|
||||
streamLog1->setMessageLimiter(std::make_shared<MessageLimiter>(2));
|
||||
streamLog2->setMessageFormatter(std::make_shared<SimpleMessageFormatter>(false, true));
|
||||
streamLog2->setMessageLimiter(std::make_shared<MessageLimiter>()); // no limit
|
||||
std::shared_ptr<MessageLimiter> lim(new MessageLimiter(MessageLimiter::NoLimit, {{ Log::MessageType::Warning, 2 }}));
|
||||
streamLog2->setMessageLimiter(lim); // no tag limit, but a warning category limit
|
||||
}
|
||||
|
||||
const std::string tag = "ExampleTag";
|
||||
@ -354,8 +356,7 @@ BOOST_AUTO_TEST_CASE(TestOpmLogWithLimits)
|
||||
+ Log::colorCodeMessage(Log::MessageType::Info, "Info") + "\n"
|
||||
+ Log::colorCodeMessage(Log::MessageType::Bug, "Bug") + "\n"
|
||||
+ Log::colorCodeMessage(Log::MessageType::Warning, "Warning") + "\n"
|
||||
+ Log::colorCodeMessage(Log::MessageType::Warning, "Warning") + "\n"
|
||||
+ Log::colorCodeMessage(Log::MessageType::Warning, "Warning") + "\n";
|
||||
+ Log::colorCodeMessage(Log::MessageType::Warning, "Message limit reached for message category: Warning") + "\n";
|
||||
|
||||
BOOST_CHECK_EQUAL(log_stream2.str(), expected2);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user