12 #include <hilti/ast/node.h>
13 #include <hilti/base/util.h>
16 #define HILTI_DEBUG(dbg, ...) \
18 if ( ::hilti::logger().isEnabled(dbg) ) \
19 ::hilti::logger()._debug(dbg, __VA_ARGS__); \
35 bool operator<(
const DebugStream& other)
const {
return _id < other._id; }
36 const auto& name()
const {
return _name; }
39 static std::vector<std::string>
all();
42 static const auto&
streamForName(
const std::string& s) {
return _streams().at(s); }
47 static std::map<std::string, DebugStream>& _streams();
53 enum class Level { Debug, Info, Warning, Error, FatalError, InternalError };
56 constexpr util::enum_::Value<Level> Levels[] = {
57 {.value = Level::Debug, .name =
"debug"},
58 {.value = Level::Info, .name =
"info"},
59 {.value = Level::Warning, .name =
"warning"},
60 {.value = Level::Error, .name =
"error"},
61 {.value = Level::FatalError, .name =
"fatal-error"},
62 {.value = Level::InternalError, .name =
"internal-error"},
66 constexpr
auto to_string(Level m) {
return util::enum_::to_string(m, detail::Levels); }
69 constexpr
auto from_string(std::string_view s) {
return util::enum_::from_string<Level>(s, detail::Levels); }
75 class Buffer :
public std::stringbuf {
77 Buffer(logging::Level level);
81 int overflow(
int ch)
final;
85 std::optional<logging::DebugStream> _dbg;
91 Stream(logging::Level level) : std::ostream(&_buf), _buf(level) {}
108 inline Logger& logger();
113 extern std::unique_ptr<Logger> setLogger(std::unique_ptr<Logger> logger);
118 Logger(std::ostream& output_std = std::cerr, std::ostream& output_debug = std::cerr)
119 : _output_std(output_std), _output_debug(output_debug) {}
121 void log(logging::Level level, std::string_view msg,
const Location& l = location::None);
123 void info(std::string_view msg,
const Location& l = location::None);
124 void warning(std::string_view msg,
const Location& l = location::None);
125 void deprecated(std::string_view msg,
const Location& l = location::None);
126 void error(std::string_view msg,
const Location& l = location::None);
127 void error(std::string_view msg,
const std::vector<std::string>& context,
const Location& l = location::None);
128 [[noreturn]]
void fatalError(std::string_view msg,
const Location& l = location::None);
129 [[noreturn]]
void internalError(std::string_view msg,
const Location& l = location::None);
131 void info(std::string_view msg,
const Node* n) { info(msg, n->
location()); }
132 void warning(std::string_view msg,
const Node* n) { warning(msg, n->
location()); }
133 void deprecated(std::string_view msg,
const Node* n) { deprecated(msg, n->
location()); }
134 void error(std::string_view msg,
const Node* n) { error(msg, n->
location()); }
135 [[noreturn]]
void fatalError(std::string_view msg,
const Node* n) { fatalError(msg, n->
location()); }
136 [[noreturn]]
void internalError(std::string_view msg,
const Node* n) { internalError(msg, n->
location()); }
142 void log(std::string msg,
const std::shared_ptr<T>& n) {
143 log(msg, n->location());
147 void info(std::string msg,
const std::shared_ptr<T>& n) {
148 info(msg, n->location());
152 void warning(std::string msg,
const std::shared_ptr<T>& n) {
153 warning(msg, n->location());
157 void error(std::string msg,
const std::shared_ptr<T>& n) {
158 error(msg, n->location());
162 void error(std::string msg, std::vector<std::string> context,
const std::shared_ptr<T>& n) {
163 error(msg, context, n.location());
166 template<
typename R,
typename T>
167 void error(
Result<R> r,
const std::shared_ptr<T>& n) {
168 error(r.
error().description(), n.location());
172 [[noreturn]]
void fatalError(std::string msg,
const std::shared_ptr<T>& n) {
173 fatalError(msg, n->location());
177 [[noreturn]]
void internalError(std::string msg,
const std::shared_ptr<T>& n) {
178 internalError(msg, n->location());
183 debug(dbg, msg, n->location());
187 bool debugEnable(
const std::string& dbg);
189 bool debugDisable(
const std::string& dbg);
194 if ( isEnabled(dbg) )
195 _debug_streams[dbg] += 1;
199 if ( isEnabled(dbg) )
200 _debug_streams[dbg] -= 1;
204 if ( isEnabled(dbg) )
205 _debug_streams[dbg] = indent;
208 int errors()
const {
return _errors; }
209 int warnings()
const {
return _warnings; }
211 void reset() { _errors = _warnings = 0; }
214 void report(std::ostream& output,
215 logging::Level level,
217 std::string_view addl,
218 std::string_view msg,
225 std::ostream& _output_std = std::cerr;
226 std::ostream& _output_debug = std::cerr;
231 std::map<logging::DebugStream, size_t> _debug_streams;
233 constinit
inline static std::unique_ptr<Logger> _singleton;
237 if ( ! Logger::_singleton )
238 Logger::_singleton = std::make_unique<Logger>();
240 return *Logger::_singleton;
Definition: location.h:17
friend Logger & logger()
Definition: logger.h:236
friend std::unique_ptr< Logger > setLogger(std::unique_ptr< Logger > logger)
void _debug(const logging::DebugStream &dbg, std::string_view msg, const Location &l=location::None)
Definition: logger.cc:132
const auto & location() const
Definition: node.h:327
static std::vector< std::string > all()
Definition: logger.cc:25
DebugStream(const std::string &name)
Definition: logger.cc:15
static const auto & streamForName(const std::string &s)
Definition: logger.h:42
Stream(logging::Level level)
Definition: logger.h:91
Stream(logging::DebugStream dbg)
Definition: logger.h:94
const result::Error & error() const &
Definition: result.h:165