11 #include <hilti/ast/node.h>
12 #include <hilti/base/util.h>
15 #define HILTI_DEBUG(dbg, ...) \
17 if ( ::hilti::logger().isEnabled(dbg) ) \
18 ::hilti::logger()._debug(dbg, __VA_ARGS__); \
34 bool operator<(
const DebugStream& other)
const {
return _id < other._id; }
35 const auto& name()
const {
return _name; }
38 static std::vector<std::string>
all();
41 static const auto&
streamForName(
const std::string& s) {
return _streams().at(s); }
46 static std::map<std::string, DebugStream>& _streams();
52 enum class Level { Debug, Info, Warning, Error, FatalError, InternalError };
55 constexpr util::enum_::Value<Level> Levels[] = {
56 {.value = Level::Debug, .name =
"debug"},
57 {.value = Level::Info, .name =
"info"},
58 {.value = Level::Warning, .name =
"warning"},
59 {.value = Level::Error, .name =
"error"},
60 {.value = Level::FatalError, .name =
"fatal-error"},
61 {.value = Level::InternalError, .name =
"internal-error"},
65 constexpr
auto to_string(Level m) {
return util::enum_::to_string(m, detail::Levels); }
68 constexpr
auto from_string(std::string_view s) {
return util::enum_::from_string<Level>(s, detail::Levels); }
74 class Buffer :
public std::stringbuf {
76 Buffer(logging::Level level);
80 int overflow(
int ch)
final;
84 std::optional<logging::DebugStream> _dbg;
90 Stream(logging::Level level) : std::ostream(&_buf), _buf(level) {}
107 inline Logger& logger();
112 extern std::unique_ptr<Logger> setLogger(std::unique_ptr<Logger> logger);
117 Logger(std::ostream& output_std = std::cerr, std::ostream& output_debug = std::cerr)
118 : _output_std(output_std), _output_debug(output_debug) {}
120 void log(logging::Level level,
const std::string& msg,
const Location& l = location::None);
122 void info(
const std::string& msg,
const Location& l = location::None);
123 void warning(
const std::string& msg,
const Location& l = location::None);
124 void deprecated(
const std::string& msg,
const Location& l = location::None);
125 void error(
const std::string& msg,
const Location& l = location::None);
126 void error(
const std::string& msg,
const std::vector<std::string>& context,
const Location& l = location::None);
127 void fatalError(
const std::string& msg,
const Location& l = location::None) __attribute__((noreturn));
128 void internalError(
const std::string& msg,
const Location& l = location::None) __attribute__((noreturn));
130 void info(
const std::string& msg,
const Node* n) { info(msg, n->
location()); }
131 void warning(
const std::string& msg,
const Node* n) { warning(msg, n->
location()); }
132 void deprecated(
const std::string& msg,
const Node* n) { deprecated(msg, n->
location()); }
133 void error(
const std::string& msg,
const Node* n) { error(msg, n->
location()); }
134 void fatalError(
const std::string& msg,
const Node* n) __attribute__((noreturn)) { fatalError(msg, n->
location()); }
135 void internalError(
const std::string& msg,
const Node* n) __attribute__((noreturn)) {
143 void log(std::string msg,
const std::shared_ptr<T>& n) {
144 log(msg, n->location());
148 void info(std::string msg,
const std::shared_ptr<T>& n) {
149 info(msg, n->location());
153 void warning(std::string msg,
const std::shared_ptr<T>& n) {
154 warning(msg, n->location());
158 void error(std::string msg,
const std::shared_ptr<T>& n) {
159 error(msg, n->location());
163 void error(std::string msg, std::vector<std::string> context,
const std::shared_ptr<T>& n) {
164 error(msg, context, n.location());
167 template<
typename R,
typename T>
168 void error(
Result<R> r,
const std::shared_ptr<T>& n) {
169 error(r.
error().description(), n.location());
173 __attribute__((noreturn))
void fatalError(std::string msg,
const std::shared_ptr<T>& n) {
174 fatalError(msg, n->location());
178 __attribute__((noreturn))
void internalError(std::string msg,
const std::shared_ptr<T>& n) {
179 internalError(msg, n->location());
184 debug(dbg, msg, n->location());
188 bool debugEnable(
const std::string& dbg);
190 bool debugDisable(
const std::string& dbg);
195 if ( isEnabled(dbg) )
196 _debug_streams[dbg] += 1;
200 if ( isEnabled(dbg) )
201 _debug_streams[dbg] -= 1;
205 if ( isEnabled(dbg) )
206 _debug_streams[dbg] = indent;
209 int errors()
const {
return _errors; }
210 int warnings()
const {
return _warnings; }
212 void reset() { _errors = _warnings = 0; }
215 void report(std::ostream& output, logging::Level level,
size_t indent,
const std::string& addl,
216 const std::string& msg,
const Location& l)
const;
222 std::ostream& _output_std = std::cerr;
223 std::ostream& _output_debug = std::cerr;
228 std::map<logging::DebugStream, size_t> _debug_streams;
230 static std::unique_ptr<Logger> _singleton;
234 if ( ! Logger::_singleton )
235 Logger::_singleton = std::make_unique<Logger>();
237 return *Logger::_singleton;
Definition: location.h:17
void _debug(const logging::DebugStream &dbg, const std::string &msg, const Location &l=location::None)
Definition: logger.cc:136
friend Logger & logger()
Definition: logger.h:233
friend std::unique_ptr< Logger > setLogger(std::unique_ptr< Logger > logger)
const auto & location() const
Definition: node.h:309
static std::vector< std::string > all()
Definition: logger.cc:27
DebugStream(const std::string &name)
Definition: logger.cc:17
static const auto & streamForName(const std::string &s)
Definition: logger.h:41
Stream(logging::Level level)
Definition: logger.h:90
Stream(logging::DebugStream dbg)
Definition: logger.h:93
const result::Error & error() const
Definition: result.h:140