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 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));
129 
131  void _debug(const logging::DebugStream& dbg, const std::string& msg, const Location& l = location::None);
132 
133  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
134  void log(std::string msg, const T& n) {
135  log(msg, to_node(n).location());
136  }
137 
138  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
139  void info(std::string msg, const T& n) {
140  info(msg, to_node(n).location());
141  }
142 
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());
146  }
147 
148  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
149  void error(std::string msg, const T& n) {
150  error(msg, to_node(n).location());
151  }
152 
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());
156  }
157 
158  template<typename R, typename T, IF_DERIVED_FROM(T, trait::isNode)>
159  void error(Result<R> r, const T& n) {
160  error(r.error().description(), to_node(n).location());
161  }
162 
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());
166  }
167 
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());
171  }
172 
173  template<typename T, IF_DERIVED_FROM(T, trait::isNode)>
174  void debug(const logging::DebugStream& dbg, std::string msg, const T& n) {
175  debug(dbg, msg, to_node(n).location());
176  }
177 
178  void debugEnable(const logging::DebugStream& dbg);
179  bool debugEnable(const std::string& dbg);
180  void debugDisable(const logging::DebugStream& dbg) { _debug_streams.erase(dbg); }
181  bool debugDisable(const std::string& dbg);
182 
183  bool isEnabled(const logging::DebugStream& dbg) { return _debug_streams.find(dbg) != _debug_streams.end(); }
184 
185  void debugPushIndent(const logging::DebugStream& dbg) {
186  if ( isEnabled(dbg) )
187  _debug_streams[dbg] += 1;
188  }
189 
190  void debugPopIndent(const logging::DebugStream& dbg) {
191  if ( isEnabled(dbg) )
192  _debug_streams[dbg] -= 1;
193  }
194 
195  void debugSetIndent(const logging::DebugStream& dbg, size_t indent) {
196  if ( isEnabled(dbg) )
197  _debug_streams[dbg] = indent;
198  }
199 
200  int errors() const { return _errors; }
201  int warnings() const { return _warnings; }
202 
203  void reset() { _errors = _warnings = 0; }
204 
205 protected:
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;
208 
209 private:
210  friend Logger& logger(); // NOLINT
211  friend std::unique_ptr<Logger> setLogger(std::unique_ptr<Logger> logger); // NOLINT
212 
213  std::ostream& _output_std = std::cerr;
214  std::ostream& _output_debug = std::cerr;
215 
216  int _warnings = 0;
217  int _errors = 0;
218 
219  std::map<logging::DebugStream, size_t> _debug_streams;
220 
221  static std::unique_ptr<Logger> _singleton;
222 };
223 
224 inline Logger& logger() {
225  if ( ! Logger::_singleton )
226  Logger::_singleton = std::make_unique<Logger>();
227 
228  return *Logger::_singleton;
229 }
230 
231 namespace logging {
232 
238 public:
239  DebugPushIndent(const logging::DebugStream& dbg) : dbg(dbg) { logger().debugPushIndent(dbg); }
240  ~DebugPushIndent() { logger().debugPopIndent(dbg); }
241 
242  DebugPushIndent() = delete;
243  DebugPushIndent(const DebugPushIndent&) = delete;
244  DebugPushIndent(DebugPushIndent&&) noexcept = delete;
245  DebugPushIndent& operator=(const DebugPushIndent&) = delete;
246  DebugPushIndent& operator=(DebugPushIndent&&) noexcept = delete;
247 
248 private:
249  const logging::DebugStream& dbg;
250 };
251 
252 } // namespace logging
253 
254 } // namespace hilti
Definition: util.h:596
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:288
Definition: logger.h:72
Definition: logger.h:115
Definition: logger.h:237
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
Definition: result.h:67
DebugStream(const std::string &name)
Definition: logger.cc:17
Definition: logger.h:28