TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::Tracer< Duration > Class Template Reference

A thread-safe tracer for logging entry, exit, and custom messages. More...

#include <tec_trace.hpp>

Public Types

using Lock = std::lock_guard< std::mutex >
 Alias for lock guard used for thread safety.
 

Public Member Functions

 Tracer (const char *name, std::ostream *out)
 Constructs a tracer with a name and output stream.
 
 ~Tracer ()
 Destructor that logs the exit of the tracer.
 
void enter ()
 Logs an entry message for the tracer.
 
template<typename T >
void trace (const T &arg)
 Logs a single argument as a trace message.
 
template<typename T , typename... Targs>
void trace (const char *fmt, const T &value, Targs &&... Args)
 Logs variadic arguments as a formatted trace message.
 

Detailed Description

template<typename Duration = _TEC_TRACE_RES>
class tec::Tracer< Duration >

A thread-safe tracer for logging entry, exit, and custom messages.

Logs messages to an output stream with timestamps, typically for debugging. Tracing is enabled only when the _TEC_TRACE_ON macro is defined. The tracer is thread-safe, using a global mutex for synchronized output.

Template Parameters
DurationThe duration type for timestamps (defaults to MilliSec).
void do_something() {
TEC_ENTER("do_something");
tec::Status status;
//
// Call some function that modifies `status` variable, like
// status = open_file(filename);
//
TEC_TRACE("open_file returned {}.", status)
}
// If _TEC_TRACE macro is defined, you get the following
// in the console if the file opened successfully:
//
// [681236363] * do_something entered.
// [681236365] do_something: open_files returned [Success].
#define TEC_ENTER(name)
Logs an entry message for a named context (e.g., function).
Definition tec_trace.hpp:211
#define TEC_TRACE(...)
Logs a formatted trace message.
Definition tec_trace.hpp:222
Defines error handling types and utilities for the tec namespace.
Provides a thread-safe tracing utility for debugging in the tec namespace.

Constructor & Destructor Documentation

◆ Tracer()

template<typename Duration = _TEC_TRACE_RES>
tec::Tracer< Duration >::Tracer ( const char name,
std::ostream *  out 
)
inline

Constructs a tracer with a name and output stream.

Initializes the tracer with a name and an output stream for logging.

Parameters
nameThe name of the tracer (e.g., function name).
outPointer to the output stream (e.g., std::cout).

◆ ~Tracer()

template<typename Duration = _TEC_TRACE_RES>
tec::Tracer< Duration >::~Tracer ( )
inline

Destructor that logs the exit of the tracer.

Logs a message with the current timestamp and tracer name when the tracer is destroyed. The output is thread-safe, using the global trace mutex.

Member Function Documentation

◆ enter()

template<typename Duration = _TEC_TRACE_RES>
void tec::Tracer< Duration >::enter ( )
inline

Logs an entry message for the tracer.

Outputs a message with the current timestamp and tracer name to indicate the start of a context. The output is thread-safe.

◆ trace() [1/2]

template<typename Duration = _TEC_TRACE_RES>
template<typename T , typename... Targs>
void tec::Tracer< Duration >::trace ( const char fmt,
const T value,
Targs &&...  Args 
)
inline

Logs variadic arguments as a formatted trace message.

Outputs a timestamped message with the tracer name and formatted arguments using the provided format string with "{}" placeholders. The output is thread-safe.

Template Parameters
TThe type of the first argument.
TargsThe types of additional arguments.
Parameters
fmtThe format string containing "{}" placeholders.
valueThe first argument to include in the trace message.
ArgsThe remaining arguments to include.

◆ trace() [2/2]

template<typename Duration = _TEC_TRACE_RES>
template<typename T >
void tec::Tracer< Duration >::trace ( const T arg)
inline

Logs a single argument as a trace message.

Outputs a timestamped message with the tracer name and the provided argument. The output is thread-safe.

Template Parameters
TThe type of the argument to log.
Parameters
argThe argument to include in the trace message.

The documentation for this class was generated from the following file: