./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
13 / 13
100.0%
Branches:
3 / 4
75.0%
Functions:
4 / 4
100.0%

src/internal/worker_start_log.hpp
Line Branch Exec Source
1 #ifndef DETOURMODKIT_INTERNAL_WORKER_START_LOG_HPP
2 #define DETOURMODKIT_INTERNAL_WORKER_START_LOG_HPP
3
4 #include <source_location>
5 #include <string_view>
6
7 namespace DetourModKit::detail
8 {
9 /**
10 * @class WorkerStartLogDeferral
11 * @brief Defers the StoppableWorker constructor record on the current thread.
12 * @details The context must remain alive until scope destruction. The sink must accept that context type and must
13 * not throw.
14 */
15 class WorkerStartLogDeferral
16 {
17 public:
18 using Sink = void (*)(void *, std::string_view, std::source_location) noexcept;
19
20 /**
21 * @brief Enters one nested deferral scope.
22 * @param context The sink context for this scope.
23 * @param sink The no-throw adapter that records one canonical start line.
24 */
25 238 WorkerStartLogDeferral(void *context, Sink sink) noexcept
26 238 : m_context(context), m_sink(sink), m_previous(current())
27 {
28 238 current() = this;
29 238 }
30
31 /// Leaves one nested deferral scope.
32 238 ~WorkerStartLogDeferral() noexcept { current() = m_previous; }
33
34 WorkerStartLogDeferral(const WorkerStartLogDeferral &) = delete;
35 WorkerStartLogDeferral &operator=(const WorkerStartLogDeferral &) = delete;
36
37 /**
38 * @brief Routes one start line through the current deferral scope.
39 * @param name The worker name.
40 * @param where The original record source.
41 * @return true if a scope consumed the record, even when its sink recorded a drop.
42 */
43 274 [[nodiscard]] static bool defer_start(std::string_view name, std::source_location where) noexcept
44 {
45 274 WorkerStartLogDeferral *scope = current();
46
3/4
✓ Branch 3 → 4 taken 235 times.
✓ Branch 3 → 5 taken 39 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 235 times.
274 if (scope == nullptr || scope->m_sink == nullptr)
47 {
48 39 return false;
49 }
50 235 scope->m_sink(scope->m_context, name, where);
51 235 return true;
52 }
53
54 private:
55 988 [[nodiscard]] static WorkerStartLogDeferral *&current() noexcept
56 {
57 static thread_local WorkerStartLogDeferral *s_current = nullptr;
58 988 return s_current;
59 }
60
61 void *m_context;
62 Sink m_sink;
63 WorkerStartLogDeferral *m_previous;
64 };
65 } // namespace DetourModKit::detail
66
67 #endif // DETOURMODKIT_INTERNAL_WORKER_START_LOG_HPP
68