Spicy
debug-logger.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <fstream>
6 #include <map>
7 #include <memory>
8 #include <optional>
9 #include <string>
10 
11 #include <hilti/rt/filesystem.h>
12 #include <hilti/rt/util.h>
13 
14 namespace hilti::rt::detail {
15 
17 class DebugLogger {
18 public:
19  DebugLogger(hilti::rt::filesystem::path output);
20 
21  void print(const std::string& stream, const std::string& msg);
22  void enable(const std::string& streams);
23 
24  bool isEnabled(const std::string& stream) { return _streams.find(stream) != _streams.end(); }
25 
26  void indent(const std::string& stream) {
27  if ( isEnabled(stream) )
28  _streams[stream] += 1;
29  }
30 
31  void dedent(const std::string& stream) {
32  if ( isEnabled(stream) ) {
33  auto& indent = _streams[stream];
34 
35  if ( indent > 0 )
36  indent -= 1;
37  }
38  }
39 
40 private:
41  hilti::rt::filesystem::path _path;
42  std::ostream* _output = nullptr;
43  std::unique_ptr<std::ofstream> _output_file;
44  std::map<std::string, integer::safe<uint64_t>> _streams;
45 };
46 
47 } // namespace hilti::rt::detail
Definition: debug-logger.h:17
Definition: debug-logger.h:14