13#define isatty(X) false
22static constexpr size_t default_message_limit = 20;
41 trace =
static_cast<uint8_t
>(MessageCode::trace),
68 Logger(
size_t limit = default_message_limit) :
Logger{nullptr, limit} {}
71 : p_(std::move(p)), limit_{limit}, cur_limit_(limit), color_{p_ == nullptr && isatty(fileno(stderr)) == 1} {}
117 [[nodiscard]]
auto out() -> std::ostringstream & {
return out_; }
120 std::ostringstream out_;
127 if (
static_cast<uint8_t
>(code) <
static_cast<uint8_t
>(level_) || disabled_[
static_cast<int>(code)]) {
135 if (cur_limit_ > 0) {
136 if (cur_limit_ != std::numeric_limits<size_t>::max()) {
149 if (code <
static_cast<MessageCode>(level_) || disabled_[
static_cast<int>(code)]) {
152 return code < MessageCode::info || cur_limit_ > 0;
156 disabled_[
static_cast<int>(code)] = !
enabled;
171 }
catch (std::exception
const &e) {
172 fprintf(stderr,
"logging failed: %s\n", e.what());
177 fprintf(stderr,
"%.*s\n",
static_cast<int>(msg.size()), msg.data());
187 auto const *prefix = color_ ?
"\033[31m"
192 prefix = color_ ?
"\033[32m"
197 prefix = color_ ?
"\033[34m"
202 prefix = color_ ?
"\033[35m"
207 prefix = color_ ?
"\033[33m"
218#define CLINGO_REPORT(p, id) \
219 if ((p).check(::CppClingo::MessageCode::id)) \
220 CppClingo::Report(p, ::CppClingo::MessageCode::id).out()
223#define CLINGO_REPORT_LOC(p, id, loc) \
224 if ((p).check(::CppClingo::MessageCode::id)) \
225 CppClingo::Report(p, ::CppClingo::MessageCode::id, loc).out()
228#define CLINGO_REPORT_STR(p, id, msg) \
229 if ((p).check(::CppClingo::MessageCode::id)) { \
230 (p).print(::CppClingo::MessageCode::id, msg); \
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
Logger(Printer p, size_t limit=default_message_limit)
Construct a logger reporting messages via the given callback.
Definition logger.hh:70
void enable_color(bool color)
Explicitly enable or disable coloring.
Definition logger.hh:94
std::function< void(MessageCode, std::string_view)> Printer
Callback to report messages.
Definition logger.hh:66
Logger(size_t limit=default_message_limit)
Construct a logger reporting messages to stderr.
Definition logger.hh:68
Helper class to ease logging.
Definition logger.hh:106
auto out() -> std::ostringstream &
Get message sink.
Definition logger.hh:117
Report(Logger &p, MessageCode code, Loc const &loc)
Construct reporter with additional location information.
Definition logger.hh:111
Report(Logger &p, MessageCode code)
Construct reporter.
Definition logger.hh:109
~Report() noexcept(false)
Destroy the reporter and output message.
Definition logger.hh:115
Exception to indicate that parsing failed.
Definition logger.hh:49
parse_error()
Default constructor.
Definition logger.hh:52
Exception to indicate that parsing failed.
Definition logger.hh:56
rewrite_error()
Default constructor.
Definition logger.hh:59
void reset()
Reset the logger to the constructed state.
Definition logger.hh:182
void set_level(LogLevel level)
Set the log level.
Definition logger.hh:159
void set_limit(size_t limit)
Set the message limit.
Definition logger.hh:163
auto message_prefix(MessageCode code) const -> std::string_view
Get a string representation of the message category.
Definition logger.hh:186
auto enabled(MessageCode code) const -> bool
Check if the given message code is enabled.
Definition logger.hh:145
MessageCode
Codes of messages.
Definition logger.hh:27
void enable(MessageCode code, bool enable)
Enable or disable a message code.
Definition logger.hh:155
void print(MessageCode code, std::string_view msg)
Unconditionally output a message with a given code.
Definition logger.hh:167
LogLevel
Log levels for coarse-grained configuration of logging.
Definition logger.hh:40
auto check(MessageCode code) -> bool
Check if a message with the given code should be reported.
Definition logger.hh:125
@ info_operation_undefined
Generic info messages.
@ warn
Info message for global variables.
@ info_atom_undefined
Info message for undefined operations.
@ info_global_variable
Info message for duplicate includes.
@ info_file_included
Info message for undefined atoms.
@ info
Output info messages.