Refactored logging interface.

This commit is contained in:
Patrick Lühne 2017-05-30 17:19:26 +02:00
parent 59fbc473df
commit 7aad8380d1
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
9 changed files with 134 additions and 21 deletions

View File

@ -47,7 +47,7 @@ int main(int argc, char **argv)
} }
catch (const po::error &e) catch (const po::error &e)
{ {
context.logger.log(anthem::output::Priority::Error, e.what()); context.logger.log(anthem::output::Priority::Error) << e.what();
printHelp(); printHelp();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -74,7 +74,7 @@ int main(int argc, char **argv)
context.logger.setColorPolicy(anthem::output::ColorStream::ColorPolicy::Always); context.logger.setColorPolicy(anthem::output::ColorStream::ColorPolicy::Always);
else else
{ {
context.logger.log(anthem::output::Priority::Error, ("unknown color policy “" + colorPolicyString + "").c_str()); context.logger.log(anthem::output::Priority::Error) << "unknown color policy “" << colorPolicyString << "";
context.logger.errorStream() << std::endl; context.logger.errorStream() << std::endl;
printHelp(); printHelp();
return EXIT_FAILURE; return EXIT_FAILURE;
@ -89,7 +89,7 @@ int main(int argc, char **argv)
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
context.logger.log(anthem::output::Priority::Error, ("unknown log priorty “" + logPriorityString + "").c_str()); context.logger.log(anthem::output::Priority::Error) << "unknown log priorty “" << logPriorityString << "";
context.logger.errorStream() << std::endl; context.logger.errorStream() << std::endl;
printHelp(); printHelp();
return EXIT_FAILURE; return EXIT_FAILURE;
@ -107,7 +107,7 @@ int main(int argc, char **argv)
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
context.logger.log(anthem::output::Priority::Error, e.what()); context.logger.log(anthem::output::Priority::Error) << e.what();
return EXIT_FAILURE; return EXIT_FAILURE;
} }

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) 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) << "[program] " << program.name;
context.logger.log(output::Priority::Debug, (std::string("[program] ") + program.name).c_str());
if (!program.parameters.empty()) if (!program.parameters.empty())
throwErrorAtLocation(statement.location, "program parameters currently unsupported", context); throwErrorAtLocation(statement.location, "program parameters currently unsupported", context);
@ -76,7 +75,7 @@ struct StatementVisitor
if (!consequent) if (!consequent)
{ {
// TODO: think about throwing an exception instead // 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; 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, inline void throwErrorAtLocation(const Clingo::Location &clingoLocation, const char *errorMessage,
Context &context) Context &context)
{ {
const auto location = location_cast<input::Location>(clingoLocation); const auto location = location_cast<input::Location>(clingoLocation);
// TODO: think about removing this to avoid double error messages // 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); 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/input/Location.h>
#include <anthem/output/ColorStream.h> #include <anthem/output/ColorStream.h>
#include <anthem/output/FormatScope.h>
#include <anthem/output/Priority.h> #include <anthem/output/Priority.h>
namespace anthem namespace anthem
@ -32,8 +33,8 @@ class Logger
void setLogPriority(Priority logPriority); void setLogPriority(Priority logPriority);
void setColorPolicy(ColorStream::ColorPolicy colorPolicy); void setColorPolicy(ColorStream::ColorPolicy colorPolicy);
void log(Priority priority, const char *message); FormatScope log(Priority priority);
void log(Priority priority, const input::Location &location, const char *message); FormatScope log(Priority priority, const input::Location &location);
private: private:
ColorStream m_outputStream; 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

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

View File

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