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

@@ -39,8 +39,7 @@ struct StatementVisitor
{
void visit(const Clingo::AST::Program &program, const Clingo::AST::Statement &statement, std::vector<ast::ScopedFormula> &, Context &context)
{
// TODO: refactor
context.logger.log(output::Priority::Debug, (std::string("[program] ") + program.name).c_str());
context.logger.log(output::Priority::Debug) << "[program] " << program.name;
if (!program.parameters.empty())
throwErrorAtLocation(statement.location, "program parameters currently unsupported", context);
@@ -76,7 +75,7 @@ struct StatementVisitor
if (!consequent)
{
// TODO: think about throwing an exception instead
context.logger.log(output::Priority::Error, "could not translate formula consequent");
context.logger.log(output::Priority::Error) << "could not translate formula consequent";
return;
}

View File

@@ -31,13 +31,14 @@ inline input::Location location_cast(const Clingo::Location &location)
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: refactor
inline void throwErrorAtLocation(const Clingo::Location &clingoLocation, const char *errorMessage,
Context &context)
{
const auto location = location_cast<input::Location>(clingoLocation);
// TODO: think about removing this to avoid double error messages
context.logger.log(output::Priority::Error, location, errorMessage);
context.logger.log(output::Priority::Error, location) << errorMessage;
throw std::runtime_error(errorMessage);
}

View File

@@ -0,0 +1,48 @@
#ifndef __ANTHEM__OUTPUT__FORMAT_SCOPE_H
#define __ANTHEM__OUTPUT__FORMAT_SCOPE_H
#include <anthem/output/ColorStream.h>
#include <anthem/output/Formatting.h>
namespace anthem
{
namespace output
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// FormatScope
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class FormatScope
{
public:
explicit FormatScope(ColorStream &colorStream)
: m_colorStream{colorStream}
{
}
~FormatScope()
{
m_colorStream << output::ResetFormat() << std::endl;
}
template<class T>
inline FormatScope &operator<<(T &&value)
{
m_colorStream << std::forward<T>(value);
return *this;
}
private:
ColorStream &m_colorStream;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -5,6 +5,7 @@
#include <anthem/input/Location.h>
#include <anthem/output/ColorStream.h>
#include <anthem/output/FormatScope.h>
#include <anthem/output/Priority.h>
namespace anthem
@@ -32,8 +33,8 @@ class Logger
void setLogPriority(Priority logPriority);
void setColorPolicy(ColorStream::ColorPolicy colorPolicy);
void log(Priority priority, const char *message);
void log(Priority priority, const input::Location &location, const char *message);
FormatScope log(Priority priority);
FormatScope log(Priority priority, const input::Location &location);
private:
ColorStream m_outputStream;

View File

@@ -0,0 +1,38 @@
#ifndef __ANTHEM__OUTPUT__NULL_STREAM_H
#define __ANTHEM__OUTPUT__NULL_STREAM_H
#include <anthem/output/ColorStream.h>
namespace anthem
{
namespace output
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// NullStream
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class NullBuffer : public std::streambuf
{
public:
int overflow(int c)
{
return c;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
extern ColorStream nullStream;
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif