summaryrefslogtreecommitdiff
path: root/src/mbgl/util/stopwatch.hpp
blob: daa313f702435002404a0092592806e069875f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once

#include <mbgl/util/event.hpp>
#include <mbgl/util/chrono.hpp>

#include <string>

#ifdef MBGL_TIMING
#include <sstream>
#endif

namespace mbgl {
namespace util {
    
#ifdef MBGL_TIMING
// Declare 'watch' as a shared_ptr so it can be captured by value in a lambda function    
#define MBGL_TIMING_START(watch)  std::shared_ptr<util::stopwatch> watch = std::make_unique<util::stopwatch>(Event::Timing);
#define MBGL_TIMING_FINISH(watch, message) \
    do { \
        std::stringstream messageStream;  \
        messageStream << message; \
        watch->report(messageStream.str()); \
    } while (0);
#else
#define MBGL_TIMING_START(watch)
#define MBGL_TIMING_FINISH(watch, message)
#endif

#ifndef DISABLE_STOPWATCH
class stopwatch {
public:
    stopwatch(Event event = Event::General);
    stopwatch(EventSeverity severity, Event event = Event::General);
    stopwatch(std::string name, Event event = Event::General);
    stopwatch(std::string name, EventSeverity severity, Event event = Event::General);
    void report(const std::string &name);
    ~stopwatch();

private:
    const std::string name;
    EventSeverity severity = EventSeverity::Debug;
    Event event = Event::General;
    TimePoint start;
};
#else
class stopwatch {
    stopwatch(Event event = Event::General);
    stopwatch(EventSeverity severity, Event event = Event::General);
    stopwatch(const std::string &name, Event event = Event::General);
    stopwatch(const std::string &name, EventSeverity severity, Event event = Event::General);
    void report(const std::string &name) {}
    ~stopwatch() {}
};
#endif
} // namespace util
} // namespace mbgl