summaryrefslogtreecommitdiff
path: root/src/mbgl/util/stopwatch.cpp
blob: 77779338aef9a3ede8bdc90a8ecf35e14b664e1e (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
#ifndef DISABLE_STOPWATCH
#include <mbgl/util/stopwatch.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/platform/log.hpp>

#include <iostream>
#include <atomic>

using namespace mbgl::util;

stopwatch::stopwatch(Event event_)
    : event(event_), start(Clock::now()) {}

stopwatch::stopwatch(EventSeverity severity_, Event event_)
    : severity(severity_), event(event_), start(Clock::now()) {}

stopwatch::stopwatch(const std::string &name_, Event event_)
    : name(name_), event(event_), start(Clock::now()) {}

stopwatch::stopwatch(const std::string &name_, EventSeverity severity_, Event event_)
    : name(name_), severity(severity_), event(event_), start(Clock::now()) {}

void stopwatch::report(const std::string &name_) {
    Duration duration = Clock::now() - start;

    Log::Record(severity, event, "%s: %fms", name_.c_str(), std::chrono::duration<float, std::chrono::milliseconds::period>(duration).count());
    start += duration;
}

stopwatch::~stopwatch() {
    if (!name.empty()) {
        report(name);
    }
}
#endif