Spicy
logger.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <map>
6 #include <memory>
7 #include <string>
8 #include <utility>
9 #include <vector>
10 
11 #include <hilti/ast/node.h>
12 #include <hilti/base/util.h>
13 
15 #define HILTI_DEBUG(dbg, ...) \
16  { \
17  if ( ::hilti::logger().isEnabled(dbg) ) \
18  ::hilti::logger()._debug(dbg, __VA_ARGS__); \
19  }
20 
21 namespace hilti {
22 namespace logging {
23 
28 class DebugStream {
29 public:
33  explicit DebugStream(const std::string& name);
34  bool operator<(const DebugStream& other) const { return _id < other._id; }
35  auto name() const { return _name; }
36 
38  static std::vector<std::string> all();
39 
41  static const auto& streamForName(const std::string& s) { return _streams().at(s); }
42 
43 private:
44  uint64_t _id;
45  std::string _name;
46  static std::map<std::string, DebugStream>& _streams();
47 };
48 
49 namespace debug {} // namespace debug
50 
52 enum class Level { Debug, Info, Warning, Error, FatalError, InternalError };
53 
54 namespace detail {
55 constexpr util::enum_::Value<Level> levels[] = {
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"},
62 };
63 } // namespace detail
64 
65 constexpr auto to_string(Level m) { return util::enum_::to_string(m, detail::levels); }
66 
67 namespace level {
68 constexpr auto from_string(const std::string_view& s) { return util::enum_::from_string<Level>(s, detail::levels); }
69 } // namespace level
70 
72 class Stream : public std::ostream {
73 private:
74  class Buffer : public std::stringbuf {
75  public:
76  Buffer(logging::Level level);
77  Buffer(logging::DebugStream dbg);
78 
79  private:
80  int overflow(int ch) final;
81  int sync() final;
82 
83  Level _level;
84  std::optional<logging::DebugStream> _dbg;
85  std::string _buffer;
86  };
87 
88 public:
90  Stream(logging::Level level) : std::ostream(&_buf), _buf(level) {}
91 
93  Stream(logging::DebugStream dbg) : std::ostream(&_buf), _buf(std::move(dbg)) {}
94 
95 private:
96  Buffer _buf;
97 };
98 
99 } // namespace logging
100 
101 class Logger;
102 
107 inline Logger& logger();
108 
112 extern std::unique_ptr<Logger> setLogger(std::unique_ptr<Logger> logger);
113 
115 class Logger {
116 public:
117  Logger(std::ostream& output_std = std::cerr, std::ostream& output_debug = std::cerr)
118  : _output_std(output_std), _output_debug(output_debug) {}
119 
120  void log(logging::Level level, const std::string& msg, const Location& l = location::None);
121 
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 error(const std::string& msg, const Location& l = location::None);
125  void error(const std::string& msg, const std::vector<std::string>& context, const Location& l = location::None);
126  void fatalError(const std::string& msg, const Location& l = location::None) __attribute__((noreturn));
127  void internalError(const std::string& msg, const Location& l = location::None) __attribute__((noreturn));
128 
130  void _debug(const logging::DebugStream& dbg, const std::string& msg, const Location& l = location::None);
131 
132  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
133  void log(std::string msg, const T& n) {
134  log(msg, to_node(n).location());
135  }
136 
137  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
138  void info(std::string msg, const T& n) {
139  info(msg, to_node(n).location());
140  }
141 
142  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
143  void warning(std::string msg, const T& n) {
144  warning(msg, to_node(n).location());
145  }
146 
147  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
148  void error(std::string msg, const T& n) {
149  error(msg, to_node(n).location());
150  }
151 
152  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
153  void error(std::string msg, std::vector<std::string> context, const T& n) {
154  error(msg, context, to_node(n).location());
155  }
156 
157  template<typename R, typename T, IF_DERIVED_FROM(T, trait::isNode)>
158  void error(Result<R> r, const T& n) {
159  error(r.error().description(), to_node(n).location());
160  }
161 
162  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
163  __attribute__((noreturn)) void fatalError(std::string msg, const T& n) {
164  fatalError(msg, to_node(n).location());
165  }
166 
167  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
168  __attribute__((noreturn)) void internalError(std::string msg, const T& n) {
169  internalError(msg, to_node(n).location());
170  }
171 
172  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
173  void debug(const logging::DebugStream& dbg, std::string msg, const T& n) {
174  debug(dbg, msg, to_node(n).location());
175  }
176 
177  void debugEnable(const logging::DebugStream& dbg);
178  bool debugEnable(const std::string& dbg);
179  void debugDisable(const logging::DebugStream& dbg) { _debug_streams.erase(dbg); }
180  bool debugDisable(const std::string& dbg);
181 
182  bool isEnabled(const logging::DebugStream& dbg) { return _debug_streams.find(dbg) != _debug_streams.end(); }
183 
184  void debugPushIndent(const logging::DebugStream& dbg) {
185  if ( isEnabled(dbg) )
186  _debug_streams[dbg] += 1;
187  }
188 
189  void debugPopIndent(const logging::DebugStream& dbg) {
190  if ( isEnabled(dbg) )
191  _debug_streams[dbg] -= 1;
192  }
193 
194  int errors() const { return _errors; }
195  int warnings() const { return _warnings; }
196 
197  void reset() { _errors = _warnings = 0; }
198 
199 protected:
200  void report(std::ostream& output, logging::Level level, int indent, const std::string& addl, const std::string& msg,
201  const Location& l) const;
202 
203 private:
204  friend Logger& logger(); // NOLINT
205  friend std::unique_ptr<Logger> setLogger(std::unique_ptr<Logger> logger); // NOLINT
206 
207  std::ostream& _output_std = std::cerr;
208  std::ostream& _output_debug = std::cerr;
209 
210  int _warnings = 0;
211  int _errors = 0;
212 
213  std::map<logging::DebugStream, int> _debug_streams;
214 
215  static std::unique_ptr<Logger> _singleton;
216 };
217 
218 inline Logger& logger() {
219  if ( ! Logger::_singleton )
220  Logger::_singleton = std::make_unique<Logger>();
221 
222  return *Logger::_singleton;
223 }
224 
225 namespace logging {
226 
232 public:
233  DebugPushIndent(const logging::DebugStream& dbg) : dbg(dbg) { logger().debugPushIndent(dbg); }
234  ~DebugPushIndent() { logger().debugPopIndent(dbg); }
235 
236  DebugPushIndent() = delete;
237  DebugPushIndent(const DebugPushIndent&) = delete;
238  DebugPushIndent(DebugPushIndent&&) noexcept = delete;
239  DebugPushIndent& operator=(const DebugPushIndent&) = delete;
240  DebugPushIndent& operator=(DebugPushIndent&&) noexcept = delete;
241 
242 private:
243  const logging::DebugStream& dbg;
244 };
245 
246 } // namespace logging
247 
248 } // namespace hilti
Definition: util.h:637
const result::Error & error() const
Definition: result.h:132
Definition: optional.h:79
static const auto & streamForName(const std::string &s)
Definition: logger.h:41
const Location & location() const
Definition: node.h:246
Definition: logger.h:72
Definition: logger.h:115
Definition: logger.h:231
static std::vector< std::string > all()
Definition: logger.cc:27
Stream(logging::DebugStream dbg)
Definition: logger.h:93
Definition: location.h:17
Stream(logging::Level level)
Definition: logger.h:90
Definition: location.h:86
Definition: result.h:67
DebugStream(const std::string &name)
Definition: logger.cc:17
Definition: logger.h:28