-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathprofiling_logger.hpp
More file actions
73 lines (55 loc) · 1.66 KB
/
profiling_logger.hpp
File metadata and controls
73 lines (55 loc) · 1.66 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
66
67
68
69
70
71
72
73
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "log/logger.hpp"
#include "clock/impl/clock_impl.hpp"
namespace kagome::log {
Logger profiling_logger();
struct ProfileScope {
using Clock = ::kagome::clock::SteadyClockImpl;
explicit ProfileScope(std::string_view scope,
log::Logger logger = profiling_logger())
: scope{scope}, logger{std::move(logger)} {
BOOST_ASSERT(logger != nullptr);
start = Clock{}.now();
}
ProfileScope(ProfileScope &&) = delete;
ProfileScope(const ProfileScope &) = delete;
ProfileScope &operator=(const ProfileScope &) = delete;
ProfileScope &operator=(ProfileScope &&) = delete;
~ProfileScope() {
if (!done) {
end();
}
}
void end() {
done = true;
auto end = Clock{}.now();
SL_DEBUG(
logger,
"{} took {} ms",
scope,
::std::chrono::duration_cast<::std::chrono::milliseconds>(end - start)
.count());
}
private:
bool done = false;
std::string_view scope;
Clock::TimePoint start;
log::Logger logger;
};
} // namespace kagome::log
#ifdef KAGOME_PROFILING
#define KAGOME_PROFILE_START_L(logger, scope) \
auto _profiling_scope_##scope = ::kagome::log::ProfileScope{#scope, logger};
#define KAGOME_PROFILE_START(scope) \
KAGOME_PROFILE_START_L(::kagome::log::profiling_logger(), scope)
#define KAGOME_PROFILE_END(scope) _profiling_scope_##scope.end();
#else
#define KAGOME_PROFILE_START(scope)
#define KAGOME_PROFILE_END(scope)
#define KAGOME_PROFILE_START_L(logger, scope)
#endif