Files

149 lines
3.8 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/Tracing/ScopedTracePoint.h
*
* @brief Header for the ScopedTracePoint class.
*/
#if (!defined ACTIV_BASE_TRACING_SCOPED_TRACE_POINT_H)
#define ACTIV_BASE_TRACING_SCOPED_TRACE_POINT_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/IntervalTimer.h"
#include "ActivMiddleware/ActivBase/Tracing/TraceBufferBase.h"
#include "ActivMiddleware/ActivBase/Tracing/TraceEvent.h"
//! Activ namespace.
namespace Activ
{
//! Class to manage the lifetime of a trace point.
class ScopedTracePoint
{
public:
//! Default constructor.
ScopedTracePoint();
/**
* @brief Constructs from a buffer and an id of a trace point that is valid.
*
* @param buffer
* @param id
*/
ScopedTracePoint(TraceBufferBase &buffer, const TracePointId id);
//! Destructor removes the trace point.
ACTIV_BASE_API ~ScopedTracePoint();
//! Release the id.
TracePointId Release();
//! TracePointId conversion operator.
operator TracePointId() const;
//! Get the trace buffer.
TraceBufferBase &GetBuffer();
/**
* @brief Record an event.
*
* @tparam The trace event type to record.
*
* @return User structure to record the event data.
*/
template <typename T>
T &Record();
/**
* @brief Record an event with a timestamp.
*
* @tparam The trace event type to record.
*
* @param timestamp provided timestamp to record.
*
* @return User structure to record the event data.
*/
template <typename T>
T &Record(const uint64_t timestamp);
/**
* @brief Record a timestamp event.
*
* @param timestamp the timestamp to record.
*/
void RecordTimestamp(const uint64_t timestamp = IntervalTimer::GetNanosecondTimeStamp());
private:
TraceBufferBase * m_pBuffer;
const TracePointId m_id;
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline ScopedTracePoint::ScopedTracePoint() :
m_pBuffer(ACTIV_NULLPTR),
m_id(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline ScopedTracePoint::ScopedTracePoint(TraceBufferBase &buffer, const TracePointId id) :
m_pBuffer(&buffer),
m_id(id)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline TracePointId ScopedTracePoint::Release()
{
m_pBuffer = ACTIV_NULLPTR;
return m_id;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline ScopedTracePoint::operator TracePointId() const
{
return m_id;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline TraceBufferBase &ScopedTracePoint::GetBuffer()
{
assert(m_pBuffer);
return *m_pBuffer;
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename T>
inline T &ScopedTracePoint::Record()
{
assert(m_pBuffer);
return m_pBuffer->Record<T>(m_id);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename T>
inline T &ScopedTracePoint::Record(const uint64_t timestamp)
{
assert(m_pBuffer);
return m_pBuffer->Record<T>(m_id, timestamp);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void ScopedTracePoint::RecordTimestamp(const uint64_t timestamp)
{
assert(m_pBuffer);
m_pBuffer->RecordTimestamp(m_id, timestamp);
}
} // namespace Activ
#endif // (!defined ACTIV_BASE_TRACING_SCOPED_TRACE_POINT_H)