Sen API
Sen Libraries
Loading...
Searching...
No Matches
tracer.h
Go to the documentation of this file.
1// === tracer.h ========================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_KERNEL_TRACER_H
9#define SEN_KERNEL_TRACER_H
10
11// sen
16
17// std
18#include <functional>
19#include <memory>
20#include <string_view>
21
22namespace sen::kernel
23{
24
27class Tracer
28{
29 SEN_NOCOPY_NOMOVE(Tracer)
30
31public:
32 Tracer() = default;
33 virtual ~Tracer() = default;
34
35public: // for scoped (RAII) zones
58 [[nodiscard]] auto makeScopedZone(const SourceLocation& location);
59
81 [[nodiscard]] auto makeScopedZone(std::string_view name, const SourceLocation& location);
82
83public: // for frame-based tracers
88 virtual void frameStart(std::string_view name) = 0;
89
94 virtual void frameEnd(std::string_view name) = 0;
95
96public: // messages
99 virtual void message(std::string_view name) = 0;
100
105 virtual void plot(std::string_view name, float64_t value) = 0;
106
111 virtual void plot(std::string_view name, int64_t value) = 0;
112
113protected:
114 virtual void zoneStart(std::string_view name, const SourceLocation& location) = 0;
115 virtual void zoneEnd() = 0;
116};
117
119using TracerFactory = std::function<std::unique_ptr<Tracer>(std::string_view)>;
120
123
124// clang-format off
136#define SEN_TRACE_ZONE_NAMED(tracer, name) static constexpr auto __senSourceLocation ## __LINE__ = SEN_SL(); auto __senScopedZone ## __LINE__ = (tracer).makeScopedZone(name, __senSourceLocation ## __LINE__)
137
149#define SEN_TRACE_ZONE(tracer) static constexpr auto __senSourceLocation ## __LINE__ = SEN_SL(); auto __senScopedZone ## __LINE__ = (tracer).makeScopedZone(__senSourceLocation ## __LINE__)
150// clang-format on
151
152//--------------------------------------------------------------------------------------------------------------
153// Inline method implementation
154//--------------------------------------------------------------------------------------------------------------
155
156inline auto Tracer::makeScopedZone(std::string_view name, const SourceLocation& location)
157{
158 zoneStart(std::move(name), location);
159 return makeScopeGuard([this]() { zoneEnd(); });
160}
161
162inline auto Tracer::makeScopedZone(const SourceLocation& location) { return makeScopedZone({}, location); }
163
164} // namespace sen::kernel
165
166#endif // SEN_KERNEL_TRACER_H
virtual void frameEnd(std::string_view name)=0
Marks the end of a (named) frame. Needs to be paired with a corresponding call to frameStart.
virtual void frameStart(std::string_view name)=0
Marks the start of a (named) frame. Needs to be paired with a corresponding call to frameEnd.
virtual void message(std::string_view name)=0
Sends a message that will be traced.
auto makeScopedZone(const SourceLocation &location)
Create a scoped zone. You can use scoped zones to trace the lifetime of a given block (the typical ex...
Definition tracer.h:162
virtual ~Tracer()=default
virtual void zoneStart(std::string_view name, const SourceLocation &location)=0
virtual void plot(std::string_view name, float64_t value)=0
Sends a value that will be plotted.
virtual void plot(std::string_view name, int64_t value)=0
Sends a value that will be plotted.
virtual void zoneEnd()=0
auto makeScopeGuard(F &&f)
Makes scope guard from a callable taking no arguments.
double float64_t
Definition numbers.h:17
Represents a location in source code.
Definition source_location.h:54
Strong type for the unique identifier for a transport timer.
Definition native_object_impl.h:39
std::function< std::unique_ptr< Tracer >(std::string_view)> TracerFactory
A factory function for tracers.
Definition tracer.h:119
TracerFactory getDefaultTracerFactory()
A function that returns a no-op tracer.