-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_duration.h
35 lines (28 loc) · 1.08 KB
/
log_duration.h
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
#pragma once
#include <chrono>
#include <iostream>
#define PROFILE_CONCAT_INTERNAL(X, Y) X##Y
#define PROFILE_CONCAT(X, Y) PROFILE_CONCAT_INTERNAL(X, Y)
#define UNIQUE_VAR_NAME_PROFILE PROFILE_CONCAT(profileGuard, __LINE__)
#define LOG_DURATION(x) LogDuration UNIQUE_VAR_NAME_PROFILE(x)
#define LOG_DURATION_STREAM(prefix, stream) LogDuration UNIQUE_VAR_NAME_PROFILE(prefix, stream)
class LogDuration {
public:
// заменим имя типа std::chrono::steady_clock
// с помощью using для удобства
using Clock = std::chrono::steady_clock;
LogDuration(const std::string_view id, std::ostream& out = std::cerr)
: id_(id), out_(out) {
}
~LogDuration() {
using namespace std::chrono;
using namespace std::literals;
const auto end_time = Clock::now();
const auto dur = end_time - start_time_;
out_ << id_ << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << std::endl;
}
private:
const std::string_view id_;
const Clock::time_point start_time_ = Clock::now();
std::ostream& out_;
};