1 #pragma once
2 
3 #include <mbgl/util/event.hpp>
4 #include <mbgl/util/chrono.hpp>
5 
6 #include <string>
7 #include <sstream>
8 
9 namespace mbgl {
10 namespace util {
11 
12 #ifdef MBGL_TIMING
13 // Declare 'watch' as a shared_ptr so it can be captured by value in a lambda function
14 #define MBGL_TIMING_START(watch)  std::shared_ptr<util::stopwatch> watch = std::make_unique<util::stopwatch>(Event::Timing);
15 #define MBGL_TIMING_FINISH(watch, message) \
16     do { \
17         std::stringstream messageStream;  \
18         messageStream << message; \
19         watch->report(messageStream.str()); \
20     } while (0);
21 #else
22 #define MBGL_TIMING_START(watch)
23 #define MBGL_TIMING_FINISH(watch, message)
24 #endif
25 
26 #ifndef DISABLE_STOPWATCH
27 class stopwatch {
28 public:
29     stopwatch(Event event = Event::General);
30     stopwatch(EventSeverity severity, Event event = Event::General);
31     stopwatch(std::string name, Event event = Event::General);
32     stopwatch(std::string name, EventSeverity severity, Event event = Event::General);
33     void report(const std::string &name);
34     ~stopwatch();
35 
36 private:
37     const std::string name;
38     EventSeverity severity = EventSeverity::Debug;
39     Event event = Event::General;
40     TimePoint start;
41 };
42 #else
43 class stopwatch {
44     stopwatch(Event event = Event::General);
45     stopwatch(EventSeverity severity, Event event = Event::General);
46     stopwatch(const std::string &name, Event event = Event::General);
47     stopwatch(const std::string &name, EventSeverity severity, Event event = Event::General);
48     void report(const std::string &name) {}
49     ~stopwatch() {}
50 };
51 #endif
52 } // namespace util
53 } // namespace mbgl
54