Refactored logging interface.

This commit is contained in:
2017-05-30 17:19:26 +02:00
parent 59fbc473df
commit 7aad8380d1
9 changed files with 134 additions and 21 deletions

View File

@@ -35,8 +35,7 @@ void translate(const std::vector<std::string> &fileNames, Context &context)
void translate(const char *fileName, std::istream &stream, Context &context)
{
// TODO: refactor
context.logger.log(output::Priority::Info, (std::string("reading ") + fileName).c_str());
context.logger.log(output::Priority::Info) << "reading " << fileName;
auto fileContent = std::string(std::istreambuf_iterator<char>(stream), {});
@@ -51,7 +50,7 @@ void translate(const char *fileName, std::istream &stream, Context &context)
const auto logger =
[&context](const Clingo::WarningCode, const char *text)
{
context.logger.log(output::Priority::Error, text);
context.logger.log(output::Priority::Error) << text;
};
Clingo::parse_program(fileContent.c_str(), translateStatement, logger);

View File

@@ -1,6 +1,7 @@
#include <anthem/output/Logger.h>
#include <anthem/output/Formatting.h>
#include <anthem/output/NullStream.h>
namespace anthem
{
@@ -92,28 +93,29 @@ void Logger::setColorPolicy(ColorStream::ColorPolicy colorPolicy)
////////////////////////////////////////////////////////////////////////////////////////////////////
void Logger::log(Priority priority, const char *message)
FormatScope Logger::log(Priority priority)
{
const auto priorityID = static_cast<int>(priority);
if (priorityID < static_cast<int>(m_logPriority))
return;
return FormatScope(detail::nullStream);
m_errorStream
<< priorityFormat(priority) << priorityName(priority) << ":"
<< ResetFormat() << " "
<< MessageBodyFormat << message
<< ResetFormat() << std::endl;
<< MessageBodyFormat;
return FormatScope(m_errorStream);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Logger::log(Priority priority, const input::Location &location, const char *message)
FormatScope Logger::log(Priority priority, const input::Location &location)
{
const auto priorityID = static_cast<int>(priority);
if (priorityID < static_cast<int>(m_logPriority))
return;
return FormatScope(detail::nullStream);
m_errorStream
<< LocationFormat
@@ -121,8 +123,9 @@ void Logger::log(Priority priority, const input::Location &location, const char
<< ResetFormat() << " "
<< priorityFormat(priority) << priorityName(priority) << ":"
<< ResetFormat() << " "
<< MessageBodyFormat << message
<< ResetFormat() << std::endl;
<< MessageBodyFormat;
return FormatScope(m_errorStream);
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -0,0 +1,24 @@
#include <anthem/output/NullStream.h>
namespace anthem
{
namespace output
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// NullStream
//
////////////////////////////////////////////////////////////////////////////////////////////////////
NullBuffer nullBuffer;
std::ostream nullOStream(&nullBuffer);
ColorStream nullStream(nullOStream);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}