-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScopeTimer.hpp
More file actions
65 lines (55 loc) · 1.92 KB
/
Copy pathScopeTimer.hpp
File metadata and controls
65 lines (55 loc) · 1.92 KB
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
57
58
59
60
61
62
63
64
65
#pragma once
#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_DETAIL_SCOPETIMER_HPP_INCLUDED
#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_DETAIL_SCOPETIMER_HPP_INCLUDED
/// \file ScopeTimer.hpp
/// \brief RAII timer that logs the duration of a scope.
#include <chrono>
#include <string>
namespace logit { namespace detail {
class ScopeTimer {
public:
ScopeTimer(LogLevel level,
std::string phase,
const char* file,
int line,
const char* function,
int logger_index,
int64_t threshold_ms = 0)
: level_(level)
, phase_(std::move(phase))
, file_(file)
, line_(line)
, function_(function)
, logger_index_(logger_index)
, threshold_ms_(threshold_ms)
, t0_(std::chrono::steady_clock::now()) {}
~ScopeTimer() {
const auto t1 = std::chrono::steady_clock::now();
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0_).count();
if (ms < threshold_ms_) return;
std::string msg = phase_;
msg += " | duration_ms=";
msg += std::to_string(ms);
Logger::get_instance().log_and_return(LogRecord{
level_,
LOGIT_WALLCLOCK_MS(),
logit::make_relative(file_, LOGIT_BASE_PATH),
line_,
function_,
msg, std::string(),
logger_index_,
false
});
}
private:
LogLevel level_;
std::string phase_;
const char* file_;
int line_;
const char* function_;
int logger_index_;
int64_t threshold_ms_;
std::chrono::steady_clock::time_point t0_;
};
}} // namespace logit::detail
#endif // LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_DETAIL_SCOPETIMER_HPP_INCLUDED