MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
logging.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mbgl/util/event.hpp>
4 
6 
7 #include <memory>
8 #include <string>
9 #include <optional>
10 
11 namespace mbgl {
12 
13 class Log {
14 public:
15  class Observer : private util::noncopyable {
16  public:
17  virtual ~Observer() = default;
18 
21  virtual bool onRecord(EventSeverity severity, Event event, int64_t code, const std::string &msg) = 0;
22  };
23 
24  class NullObserver : public Observer {
25  bool onRecord(EventSeverity, Event, int64_t, const std::string&) override {
26  return true;
27  }
28  };
29 
30  static void setObserver(std::unique_ptr<Observer> Observer);
31  static std::unique_ptr<Observer> removeObserver();
32 
33 private:
34  template <typename T, size_t N>
35  constexpr static bool includes(const T e, const T (&l)[N], const size_t i = 0) {
36  return i < N && (l[i] == e || includes(e, l, i + 1));
37  }
38 
39 public:
40  Log();
41  ~Log();
42 
43  static void useLogThread(bool enable);
44 
45  template <typename ...Args>
46  static void Debug(Event event, Args&& ...args) {
47  Record(EventSeverity::Debug, event, ::std::forward<Args>(args)...);
48  }
49 
50  template <typename ...Args>
51  static void Info(Event event, Args&& ...args) {
52  Record(EventSeverity::Info, event, ::std::forward<Args>(args)...);
53  }
54 
55  template <typename ...Args>
56  static void Warning(Event event, Args&& ...args) {
57  Record(EventSeverity::Warning, event, ::std::forward<Args>(args)...);
58  }
59 
60  template <typename ...Args>
61  static void Error(Event event, Args&& ...args) {
62  Record(EventSeverity::Error, event, ::std::forward<Args>(args)...);
63  }
64 
65  template <typename ...Args>
66  static void Record(EventSeverity severity, Event event, Args&& ...args) {
67  if (!includes(severity, disabledEventSeverities) &&
68  !includes(event, disabledEvents) &&
69  !includes({ severity, event }, disabledEventPermutations)) {
70  record(severity, event, ::std::forward<Args>(args)...);
71  }
72  }
73 
74 private:
75  static Log* get() noexcept;
76 
77  static void record(EventSeverity severity, Event event, const std::string &msg);
78  static void record(EventSeverity severity, Event event, int64_t code, const std::string &msg);
79  static void record(EventSeverity severity,
80  Event event,
81  int64_t code,
82  const std::string& msg,
83  const std::optional<std::string>& threadName);
84 
85  // This method is the data sink that must be implemented by each platform we
86  // support. It should ideally output the error message in a human readable
87  // format to the developer.
88  static void platformRecord(EventSeverity severity, const std::string &msg);
89  class Impl;
90  const std::unique_ptr<Impl> impl;
91 };
92 
93 } // namespace mbgl
virtual ~Observer()=default
virtual bool onRecord(EventSeverity severity, Event event, int64_t code, const std::string &msg)=0
static void Debug(Event event, Args &&...args)
Definition: logging.hpp:46
static void Record(EventSeverity severity, Event event, Args &&...args)
Definition: logging.hpp:66
static void Error(Event event, Args &&...args)
Definition: logging.hpp:61
static std::unique_ptr< Observer > removeObserver()
static void Warning(Event event, Args &&...args)
Definition: logging.hpp:56
static void useLogThread(bool enable)
static void setObserver(std::unique_ptr< Observer > Observer)
static void Info(Event event, Args &&...args)
Definition: logging.hpp:51
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
Definition: actor.hpp:15
Event
Definition: event.hpp:14
constexpr Event disabledEvents[]
Definition: event.hpp:51
constexpr EventPermutation disabledEventPermutations[]
Definition: event.hpp:55
constexpr EventSeverity disabledEventSeverities[]
Definition: event.hpp:43
EventSeverity
Definition: event.hpp:7
Definition: tile_id.hpp:256