forked from qdrvm/kagome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiling_logger.hpp
More file actions
78 lines (58 loc) · 1.88 KB
/
profiling_logger.hpp
File metadata and controls
78 lines (58 loc) · 1.88 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
74
75
76
77
78
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef KAGOME_CORE_LOG_PROFILING_LOGGER_HPP
#define KAGOME_CORE_LOG_PROFILING_LOGGER_HPP
#include "log/logger.hpp"
#include "clock/impl/clock_impl.hpp"
namespace kagome::log {
extern Logger profiling_logger;
struct ProfileScope {
using Clock = ::kagome::clock::SteadyClockImpl;
ProfileScope(std::string_view scope, log::Logger logger)
: scope{scope}, logger{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_END_L(logger, scope) _profiling_scope_##scope.end();
#define KAGOME_PROFILE_START(scope) \
KAGOME_PROFILE_START_L(::kagome::log::profiling_logger, scope)
#define KAGOME_PROFILE_END(scope) \
KAGOME_PROFILE_END_L(::kagome::log::profiling_logger, scope)
#else
#define KAGOME_PROFILE_START(scope)
#define KAGOME_PROFILE_END(scope)
#define KAGOME_PROFILE_START_L(logger, scope)
#define KAGOME_PROFILE_END_L(logger, scope)
#endif
#endif // KAGOME_CORE_LOG_PROFILING_LOGGER_HPP