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 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 };
56 {Level::Debug,
"debug"},
57 {Level::Info,
"info"},
58 {Level::Warning,
"warning"},
59 {Level::Error,
"error"},
60 {Level::FatalError,
"fatal-error"},
61 {Level::InternalError,
"internal-error"},
65 constexpr
auto to_string(Level m) {
return util::enum_::to_string(m, detail::levels); }
68 constexpr
auto from_string(
const 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) {}
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));
133 template<
typename T, IF_DERIVED_FROM(T, trait::isNode)>
134 void log(std::string msg,
const T& n) {
138 template<
typename T, IF_DERIVED_FROM(T, trait::isNode)>
139 void info(std::string msg,
const T& n) {
143 template<
typename T, IF_DERIVED_FROM(T, trait::isNode)>
144 void warning(std::string msg,
const T& n) {
145 warning(msg, to_node(n).
location());
148 template<
typename T, IF_DERIVED_FROM(T, trait::isNode)>
149 void error(std::string msg,
const T& n) {
153 template<
typename T, IF_DERIVED_FROM(T, trait::isNode)>
154 void error(std::string msg, std::vector<std::string> context,
const T& n) {
155 error(msg, context, to_node(n).
location());
158 template<
typename R,
typename T, IF_DERIVED_FROM(T, trait::isNode)>
163 template<
typename T, IF_DERIVED_FROM(T, trait::isNode)>
164 __attribute__((noreturn))
void fatalError(std::string msg,
const T& n) {
165 fatalError(msg, to_node(n).
location());
168 template<
typename T, IF_DERIVED_FROM(T, trait::isNode)>
169 __attribute__((noreturn))
void internalError(std::string msg,
const T& n) {
170 internalError(msg, to_node(n).
location());
173 template<
typename T, IF_DERIVED_FROM(T, trait::isNode)>
175 debug(dbg, msg, to_node(n).
location());
179 bool debugEnable(
const std::string& dbg);
181 bool debugDisable(
const std::string& dbg);
183 bool isEnabled(
const logging::DebugStream& dbg) {
return _debug_streams.find(dbg) != _debug_streams.end(); }
186 if ( isEnabled(dbg) )
187 _debug_streams[dbg] += 1;
191 if ( isEnabled(dbg) )
192 _debug_streams[dbg] -= 1;
196 if ( isEnabled(dbg) )
197 _debug_streams[dbg] = indent;
200 int errors()
const {
return _errors; }
201 int warnings()
const {
return _warnings; }
203 void reset() { _errors = _warnings = 0; }
206 void report(std::ostream& output, logging::Level level,
size_t indent,
const std::string& addl,
207 const std::string& msg,
const Location& l)
const;
211 friend std::unique_ptr<Logger> setLogger(std::unique_ptr<Logger> logger);
213 std::ostream& _output_std = std::cerr;
214 std::ostream& _output_debug = std::cerr;
219 std::map<logging::DebugStream, size_t> _debug_streams;
221 static std::unique_ptr<Logger> _singleton;
225 if ( ! Logger::_singleton )
226 Logger::_singleton = std::make_unique<Logger>();
228 return *Logger::_singleton;
const result::Error & error() const
Definition: result.h:136
Definition: optional.h:79
static const auto & streamForName(const std::string &s)
Definition: logger.h:41
const Location & location() const
Definition: node.h:289
static std::vector< std::string > all()
Definition: logger.cc:27
Stream(logging::DebugStream dbg)
Definition: logger.h:93
Definition: location.h:18
Stream(logging::Level level)
Definition: logger.h:90
Definition: location.h:94
DebugStream(const std::string &name)
Definition: logger.cc:17